fix(content): refine installed update workflow
This commit is contained in:
1 parent
ed7387beb3
commit
bfd8be6abe
19 files changed
+815
-216
No files matched your search
@@ -1656,6 +1656,7 @@ pub(crate) fn provider_project_entry(
|
||||
world_details: None,
|
||||
title_suffix: installed_path.is_some().then(|| "Installed".to_owned()),
|
||||
footer_label: Some(format!("{} downloads", format_downloads(project.downloads))),
|
||||
footer_change: None,
|
||||
description: project.description,
|
||||
enabled: true,
|
||||
icon_bytes: project.icon_bytes,
|
||||
|
||||
+158
-35
@@ -148,6 +148,8 @@ struct PaginationState {
|
||||
hits: Vec<(Rect, usize)>,
|
||||
}
|
||||
|
||||
const REMOVAL_GRACE: std::time::Duration = std::time::Duration::from_millis(500);
|
||||
|
||||
// result from the notify-triggered background diff
|
||||
struct WatcherDiff {
|
||||
toggled: Vec<(String, bool, std::path::PathBuf)>,
|
||||
@@ -198,6 +200,7 @@ pub struct ContentListState {
|
||||
scan_one_fn: Option<ScanOneFn>,
|
||||
content_ext: Option<&'static str>,
|
||||
pagination: Option<PaginationState>,
|
||||
pending_removals: HashMap<String, std::time::Instant>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -235,11 +238,25 @@ impl Default for ContentListState {
|
||||
scan_one_fn: None,
|
||||
content_ext: None,
|
||||
pagination: None,
|
||||
pending_removals: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ContentListState {
|
||||
pub(crate) fn set_entries(&mut self, entries: Vec<ContentEntry>) {
|
||||
self.entries = entries;
|
||||
self.list_state = TuiListState::default();
|
||||
self.list_state.selected = (!self.entries.is_empty()).then_some(0);
|
||||
self.image_protocols.clear();
|
||||
self.requested_images.clear();
|
||||
self.pending_entry_images.clear();
|
||||
self.pending_removals.clear();
|
||||
self.images_dirty = true;
|
||||
self.rebuild_display_metadata();
|
||||
self.update_scrollbar();
|
||||
}
|
||||
|
||||
pub fn apply_manifest(
|
||||
&mut self,
|
||||
manifest: &crate::instance::ContentManifest,
|
||||
@@ -500,6 +517,7 @@ impl ContentListState {
|
||||
self.image_protocols.clear();
|
||||
self.requested_images.clear();
|
||||
self.pending_entry_images.clear();
|
||||
self.pending_removals.clear();
|
||||
self.requested_provider_icons.clear();
|
||||
self.entries.clear();
|
||||
self.display_metadata.clear();
|
||||
@@ -788,16 +806,21 @@ impl ContentListState {
|
||||
return ContentWatcherUpdate::default();
|
||||
}
|
||||
|
||||
let expired_removals = self.expire_pending_removals();
|
||||
|
||||
let diff = match self.watcher_diff.lock() {
|
||||
Ok(mut slot) => slot.take(),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let Some(diff) = diff else {
|
||||
return ContentWatcherUpdate::default();
|
||||
return ContentWatcherUpdate {
|
||||
requires_reconcile: expired_removals,
|
||||
..ContentWatcherUpdate::default()
|
||||
};
|
||||
};
|
||||
let mut update = ContentWatcherUpdate {
|
||||
requires_reconcile: !diff.removed.is_empty() || !diff.added.is_empty(),
|
||||
requires_reconcile: expired_removals || !diff.added.is_empty(),
|
||||
..ContentWatcherUpdate::default()
|
||||
};
|
||||
self.images_dirty |= update.requires_reconcile;
|
||||
@@ -811,6 +834,7 @@ impl ContentListState {
|
||||
diff.added.len()
|
||||
);
|
||||
for (stem, enabled, path) in &diff.toggled {
|
||||
self.pending_removals.remove(stem);
|
||||
if let Some(entry) = self.entries.iter_mut().find(|e| &e.file_stem == stem) {
|
||||
let old_path = if entry.path == *path {
|
||||
opposite_toggle_path(path, *enabled)
|
||||
@@ -829,17 +853,46 @@ impl ContentListState {
|
||||
}
|
||||
}
|
||||
|
||||
// apply removals
|
||||
if !diff.removed.is_empty() {
|
||||
self.entries
|
||||
.retain(|e| !diff.removed.contains(&e.file_stem));
|
||||
for stem in &diff.removed {
|
||||
self.display_metadata.remove(stem);
|
||||
}
|
||||
// A version change is reported by the filesystem as a removal followed
|
||||
// by an addition. Keep the old row briefly so the replacement can take
|
||||
// its place without flashing out of the list.
|
||||
for stem in diff.removed {
|
||||
self.pending_removals
|
||||
.entry(stem)
|
||||
.or_insert_with(std::time::Instant::now);
|
||||
}
|
||||
|
||||
// insert new entries in sorted position
|
||||
for entry in diff.added {
|
||||
for mut entry in diff.added {
|
||||
let replacement = self.entries.iter().position(|existing| {
|
||||
self.pending_removals.contains_key(&existing.file_stem)
|
||||
&& existing.name.eq_ignore_ascii_case(&entry.name)
|
||||
});
|
||||
if let Some(index) = replacement {
|
||||
let old_stem = self.entries[index].file_stem.clone();
|
||||
self.pending_removals.remove(&old_stem);
|
||||
preserve_visual_metadata(&mut entry, &mut self.entries[index]);
|
||||
self.display_metadata.remove(&old_stem);
|
||||
let protocol = self.image_protocols.remove(&old_stem);
|
||||
let image_requested = self.requested_images.remove(&old_stem);
|
||||
let image_pending = self.pending_entry_images.remove(&old_stem);
|
||||
self.entries[index] = entry;
|
||||
let entry = &self.entries[index];
|
||||
if let Some(protocol) = protocol {
|
||||
self.image_protocols
|
||||
.insert(entry.file_stem.clone(), protocol);
|
||||
}
|
||||
if image_requested {
|
||||
self.requested_images.insert(entry.file_stem.clone());
|
||||
}
|
||||
if image_pending {
|
||||
self.pending_entry_images.insert(entry.file_stem.clone());
|
||||
}
|
||||
self.display_metadata
|
||||
.insert(entry.file_stem.clone(), display_metadata(entry));
|
||||
self.images_dirty = true;
|
||||
continue;
|
||||
}
|
||||
if entry.icon_bytes.is_some() {
|
||||
self.pending_entry_images.insert(entry.file_stem.clone());
|
||||
}
|
||||
@@ -865,6 +918,39 @@ impl ContentListState {
|
||||
update
|
||||
}
|
||||
|
||||
fn expire_pending_removals(&mut self) -> bool {
|
||||
let expired = self
|
||||
.pending_removals
|
||||
.iter()
|
||||
.filter(|(_, since)| since.elapsed() >= REMOVAL_GRACE)
|
||||
.map(|(stem, _)| stem.clone())
|
||||
.collect::<Vec<_>>();
|
||||
let mut removed = false;
|
||||
for stem in expired {
|
||||
self.pending_removals.remove(&stem);
|
||||
let restored = self
|
||||
.entries
|
||||
.iter()
|
||||
.find(|entry| entry.file_stem == stem)
|
||||
.is_some_and(|entry| entry.path.exists());
|
||||
if restored {
|
||||
continue;
|
||||
}
|
||||
let before = self.entries.len();
|
||||
self.entries.retain(|entry| entry.file_stem != stem);
|
||||
removed |= self.entries.len() != before;
|
||||
self.display_metadata.remove(&stem);
|
||||
self.image_protocols.remove(&stem);
|
||||
self.requested_images.remove(&stem);
|
||||
self.pending_entry_images.remove(&stem);
|
||||
}
|
||||
if removed {
|
||||
self.images_dirty = true;
|
||||
self.update_scrollbar();
|
||||
}
|
||||
removed
|
||||
}
|
||||
|
||||
// starts a notify file watcher on the given directory. changes trigger
|
||||
// a background diff that lands in watcher_diff for drain_watcher to apply.
|
||||
pub fn watch_dir(&mut self, dir: std::path::PathBuf) {
|
||||
@@ -1081,6 +1167,7 @@ impl ContentListState {
|
||||
self.image_protocols.clear();
|
||||
self.requested_images.clear();
|
||||
self.pending_entry_images.clear();
|
||||
self.pending_removals.clear();
|
||||
|
||||
// save current entries to cache
|
||||
if let Some(prev) = self.loaded_for.take()
|
||||
@@ -1571,7 +1658,7 @@ pub fn render(
|
||||
.and_then(|details| details.game_mode)
|
||||
.map(world_game_mode_color)
|
||||
.unwrap_or_else(|| {
|
||||
if title_suffix == Some("Update") {
|
||||
if matches!(title_suffix, Some("Update" | "Skipped")) {
|
||||
theme.warning()
|
||||
} else {
|
||||
theme.success()
|
||||
@@ -1586,6 +1673,16 @@ pub fn render(
|
||||
} else {
|
||||
theme.text()
|
||||
});
|
||||
let footer_spans = entry.footer_change.as_ref().map_or_else(
|
||||
|| {
|
||||
footer_label.map_or_else(Vec::new, |label| {
|
||||
vec![Span::styled(label.to_owned(), footer_label_style)]
|
||||
})
|
||||
},
|
||||
|(from, to)| version_change_spans(from, to),
|
||||
);
|
||||
let footer_width = footer_spans.iter().map(Span::width).sum();
|
||||
let has_footer = !footer_spans.is_empty();
|
||||
|
||||
let world_descriptions = world_details.map(world_descriptions);
|
||||
let has_icon = icon_pixels.is_some();
|
||||
@@ -1627,7 +1724,7 @@ pub fn render(
|
||||
ellipsize(
|
||||
description,
|
||||
if index == 0 {
|
||||
description_text_width(description_width, footer_label, has_description)
|
||||
description_text_width(description_width, footer_width, has_description)
|
||||
} else {
|
||||
description_width
|
||||
},
|
||||
@@ -1635,7 +1732,7 @@ pub fn render(
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let visible_description = visible_descriptions.first().map_or("", String::as_str);
|
||||
let compact = !has_icon && !has_description && footer_label.is_none();
|
||||
let compact = !has_icon && !has_description && !has_footer;
|
||||
|
||||
let selector = if show_selected {
|
||||
Span::styled("\u{258c}", Style::default().fg(theme.accent()))
|
||||
@@ -1657,9 +1754,7 @@ pub fn render(
|
||||
(item, 1)
|
||||
} else if has_icon {
|
||||
let icon_row_count = icon_pixels.as_ref().map(|r| r.len()).unwrap_or(0);
|
||||
let text_rows = 1 + visible_descriptions
|
||||
.len()
|
||||
.max(usize::from(footer_label.is_some()));
|
||||
let text_rows = 1 + visible_descriptions.len().max(usize::from(has_footer));
|
||||
let height = icon_row_count.max(text_rows) as u16;
|
||||
|
||||
let pad = if show_selected {
|
||||
@@ -1697,15 +1792,12 @@ pub fn render(
|
||||
if let Some(description) = visible_descriptions.get(r - 1) {
|
||||
row.extend(search.highlight_spans(description, description_style));
|
||||
}
|
||||
if r == 1
|
||||
&& let Some(footer_label) = footer_label
|
||||
{
|
||||
if r == 1 && has_footer {
|
||||
row.extend(right_aligned_footer_spans(
|
||||
description_width,
|
||||
visible_description,
|
||||
has_description,
|
||||
footer_label,
|
||||
footer_label_style,
|
||||
footer_spans.clone(),
|
||||
));
|
||||
}
|
||||
lines.push(Line::from(row));
|
||||
@@ -1736,7 +1828,7 @@ pub fn render(
|
||||
|
||||
let mut lines = vec![Line::from(line_0)];
|
||||
|
||||
if has_description || footer_label.is_some() {
|
||||
if has_description || has_footer {
|
||||
let pad = if show_selected {
|
||||
Span::styled("\u{258c}", Style::default().fg(theme.accent()))
|
||||
} else {
|
||||
@@ -1747,13 +1839,12 @@ pub fn render(
|
||||
description
|
||||
.extend(search.highlight_spans(visible_description, description_style));
|
||||
}
|
||||
if let Some(footer_label) = footer_label {
|
||||
if has_footer {
|
||||
description.extend(right_aligned_footer_spans(
|
||||
description_width,
|
||||
visible_description,
|
||||
has_description,
|
||||
footer_label,
|
||||
footer_label_style,
|
||||
footer_spans.clone(),
|
||||
));
|
||||
}
|
||||
lines.push(Line::from(description));
|
||||
@@ -2083,6 +2174,21 @@ fn display_metadata(entry: &ContentEntry) -> DisplayMetadata {
|
||||
}
|
||||
}
|
||||
|
||||
fn preserve_visual_metadata(entry: &mut ContentEntry, previous: &mut ContentEntry) {
|
||||
if entry.icon_bytes.is_none() {
|
||||
entry.icon_bytes = previous.icon_bytes.take();
|
||||
entry.icon_lines = previous.icon_lines.take();
|
||||
entry.provider_icon = previous.provider_icon;
|
||||
}
|
||||
if entry.description.trim().is_empty() && previous.provider_description {
|
||||
entry.description = std::mem::take(&mut previous.description);
|
||||
entry.provider_description = true;
|
||||
}
|
||||
if entry.provider_project.is_none() {
|
||||
entry.provider_project = previous.provider_project.clone();
|
||||
}
|
||||
}
|
||||
|
||||
// renders one row of a mod icon using half-block characters (U+2584).
|
||||
// each cell packs two vertical pixels via fg/bg colors, giving
|
||||
// double the vertical resolution out of the terminal
|
||||
@@ -2181,32 +2287,49 @@ fn available_description_width(
|
||||
|
||||
fn description_text_width(
|
||||
available_width: usize,
|
||||
footer_label: Option<&str>,
|
||||
footer_width: usize,
|
||||
has_description: bool,
|
||||
) -> usize {
|
||||
let footer_width = footer_label.map_or(0, |footer_label| {
|
||||
Span::raw(footer_label).width() + usize::from(has_description)
|
||||
});
|
||||
available_width.saturating_sub(footer_width)
|
||||
available_width.saturating_sub(footer_width + usize::from(has_description && footer_width > 0))
|
||||
}
|
||||
|
||||
fn right_aligned_footer_spans(
|
||||
available_width: usize,
|
||||
description: &str,
|
||||
has_description: bool,
|
||||
footer_label: &str,
|
||||
footer_style: Style,
|
||||
footer: Vec<Span<'static>>,
|
||||
) -> Vec<Span<'static>> {
|
||||
let description_width = if has_description {
|
||||
Span::raw(description).width()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let footer_width = Span::raw(footer_label).width();
|
||||
let footer_width = footer.iter().map(Span::width).sum::<usize>();
|
||||
let padding = available_width.saturating_sub(description_width + footer_width);
|
||||
let mut spans = vec![Span::raw(" ".repeat(padding))];
|
||||
spans.extend(footer);
|
||||
spans
|
||||
}
|
||||
|
||||
fn version_change_spans(from: &str, to: &str) -> Vec<Span<'static>> {
|
||||
let theme = THEME.as_ref();
|
||||
let old = Style::default()
|
||||
.fg(theme.background())
|
||||
.bg(theme.text_dim())
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let new = Style::default()
|
||||
.fg(theme.background())
|
||||
.bg(theme.success())
|
||||
.add_modifier(Modifier::BOLD);
|
||||
vec![
|
||||
Span::raw(" ".repeat(padding)),
|
||||
Span::styled(footer_label.to_owned(), footer_style),
|
||||
Span::styled(format!(" {from} "), old),
|
||||
Span::styled(
|
||||
" ➜ ",
|
||||
Style::default()
|
||||
.fg(theme.accent())
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::styled(format!(" {to} "), new),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@@ -291,6 +291,16 @@ pub fn render(
|
||||
.is_some(),
|
||||
_ => false,
|
||||
});
|
||||
let has_updates = match tab {
|
||||
ContentTab::Mods => mods_state.entries.iter(),
|
||||
ContentTab::ResourcePacks => resource_packs_state.entries.iter(),
|
||||
ContentTab::Shaders => shaders_state.entries.iter(),
|
||||
ContentTab::Worlds if open_world_datapacks.is_some() => {
|
||||
world_datapacks_state.entries.iter()
|
||||
}
|
||||
_ => worlds_state.entries.iter(),
|
||||
}
|
||||
.any(|entry| entry.title_suffix.as_deref() == Some("Update"));
|
||||
|
||||
// keybinds change depending on which tab is active and whether
|
||||
// the content panel or instances panel has focus
|
||||
@@ -414,9 +424,12 @@ pub fn render(
|
||||
None
|
||||
};
|
||||
|
||||
let keybind_lines = kb.map_or_else(Vec::new, |keybinds| {
|
||||
crate::tui::widgets::popups::keybind_lines_wrapped(keybinds, area.width.saturating_sub(2))
|
||||
});
|
||||
let mut keybinds = kb.map_or_else(Vec::new, <[_]>::to_vec);
|
||||
if is_focused && mode == ContentMode::Installed && !has_updates {
|
||||
keybinds.retain(|(key, _)| *key != "u");
|
||||
}
|
||||
let keybind_lines =
|
||||
crate::tui::widgets::popups::keybind_lines_wrapped(&keybinds, area.width.saturating_sub(2));
|
||||
if let Some(line) = keybind_lines.last() {
|
||||
block = block.title_bottom(line.clone());
|
||||
}
|
||||
@@ -833,14 +846,23 @@ fn render_version_popup(
|
||||
popup
|
||||
.visible_versions()
|
||||
.map(|version| {
|
||||
let style = if current_version_id.as_deref() == Some(version.id.as_str()) {
|
||||
Style::default()
|
||||
.fg(theme.text())
|
||||
.add_modifier(Modifier::ITALIC)
|
||||
} else {
|
||||
Style::default().fg(theme.text())
|
||||
};
|
||||
ListItem::new(discovery_version_label(version)).style(style)
|
||||
let mut spans = vec![Span::styled(
|
||||
discovery_version_label(version),
|
||||
Style::default().fg(theme.text()),
|
||||
)];
|
||||
if current_version_id.as_deref() == Some(version.id.as_str()) {
|
||||
spans.extend([
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
" Installed ",
|
||||
Style::default()
|
||||
.fg(theme.background())
|
||||
.bg(theme.success())
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
]);
|
||||
}
|
||||
ListItem::new(Line::from(spans))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
+207
-112
@@ -1,16 +1,17 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Constraint, Flex, Layout},
|
||||
style::{Modifier, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
|
||||
layout::{Constraint, Margin},
|
||||
};
|
||||
|
||||
use crate::config::theme::{BORDER_STYLE, THEME};
|
||||
use crate::instance::ContentKind;
|
||||
use crate::instance::content::entry::ContentEntry;
|
||||
use crate::instance::content::updates::{BulkUpdatePlan, UpdateSnapshot};
|
||||
use crate::tui::widgets::popups::base::PopupFrame;
|
||||
|
||||
pub enum PendingResult {
|
||||
Prepared(UpdateSnapshot, BulkUpdatePlan),
|
||||
@@ -29,28 +30,35 @@ pub struct State {
|
||||
pub phase: Phase,
|
||||
pub plan: Option<BulkUpdatePlan>,
|
||||
pub snapshot: Option<UpdateSnapshot>,
|
||||
pub selected: usize,
|
||||
pub retry_conflicts: Vec<bool>,
|
||||
pub error: Option<String>,
|
||||
pub pending: Arc<Mutex<Vec<PendingResult>>>,
|
||||
pub kind: ContentKind,
|
||||
pub target_world: Option<(String, std::path::PathBuf)>,
|
||||
pub target_world: Option<(String, PathBuf)>,
|
||||
pub completed: bool,
|
||||
pub applied: bool,
|
||||
pub list: super::list::ContentListState,
|
||||
source_entries: HashMap<PathBuf, ContentEntry>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn checking(kind: ContentKind, target_world: Option<(String, std::path::PathBuf)>) -> Self {
|
||||
pub fn checking(
|
||||
kind: ContentKind,
|
||||
target_world: Option<(String, PathBuf)>,
|
||||
entries: Vec<ContentEntry>,
|
||||
) -> Self {
|
||||
Self {
|
||||
phase: Phase::Checking,
|
||||
plan: None,
|
||||
snapshot: None,
|
||||
selected: 0,
|
||||
retry_conflicts: Vec::new(),
|
||||
error: None,
|
||||
pending: Arc::new(Mutex::new(Vec::new())),
|
||||
kind,
|
||||
target_world,
|
||||
completed: false,
|
||||
applied: false,
|
||||
list: super::list::ContentListState::default(),
|
||||
source_entries: entries
|
||||
.into_iter()
|
||||
.map(|entry| (entry.path.clone(), entry))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,21 +72,30 @@ impl State {
|
||||
changed = true;
|
||||
match result {
|
||||
PendingResult::Prepared(snapshot, plan) => {
|
||||
self.retry_conflicts = vec![false; plan.conflicts.len()];
|
||||
self.phase = if plan.conflicts.is_empty() {
|
||||
self.phase = if !plan.conflicts.is_empty() {
|
||||
Phase::Conflicts
|
||||
} else if !plan.roots.is_empty() {
|
||||
Phase::Review
|
||||
} else {
|
||||
Phase::Conflicts
|
||||
self.completed = true;
|
||||
Phase::Review
|
||||
};
|
||||
self.selected = 0;
|
||||
self.snapshot = Some(snapshot);
|
||||
self.plan = Some(plan);
|
||||
self.error = None;
|
||||
self.rebuild_list();
|
||||
}
|
||||
PendingResult::Applied(Ok(())) => {
|
||||
self.completed = true;
|
||||
self.applied = true;
|
||||
}
|
||||
PendingResult::Applied(Ok(())) => self.completed = true,
|
||||
PendingResult::Applied(Err(error)) => {
|
||||
self.phase = Phase::Review;
|
||||
self.error = Some(error);
|
||||
crate::feedback::errors::push_error(crate::feedback::errors::ErrorEvent {
|
||||
id: 0,
|
||||
level: tracing::Level::ERROR,
|
||||
message: format!("Failed to update content: {error}"),
|
||||
pushed_at: Instant::now(),
|
||||
});
|
||||
self.completed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,111 +109,189 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_retry(&self) -> bool {
|
||||
self.retry_conflicts.iter().any(|retry| *retry)
|
||||
pub fn visible(&self) -> bool {
|
||||
matches!(self.phase, Phase::Conflicts | Phase::Review) && !self.completed
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(frame: &mut Frame, state: &State) {
|
||||
let theme = THEME.as_ref();
|
||||
let item_count = match state.phase {
|
||||
Phase::Conflicts => state.plan.as_ref().map_or(0, |plan| plan.conflicts.len()),
|
||||
Phase::Review => state.plan.as_ref().map_or(0, |plan| plan.roots.len()),
|
||||
_ => 1,
|
||||
};
|
||||
let height = (item_count as u16 + 5).clamp(7, 20);
|
||||
let [area] = Layout::vertical([Constraint::Length(height)])
|
||||
.flex(Flex::Center)
|
||||
.areas(frame.area());
|
||||
let [area] = Layout::horizontal([Constraint::Percentage(62)])
|
||||
.flex(Flex::Center)
|
||||
.areas(area);
|
||||
frame.render_widget(Clear, area);
|
||||
pub fn has_updates(&self) -> bool {
|
||||
self.plan
|
||||
.as_ref()
|
||||
.is_some_and(|plan| !plan.roots.is_empty())
|
||||
}
|
||||
|
||||
let (title, footer) = match state.phase {
|
||||
Phase::Checking => (" Check for updates ", " [Esc] close "),
|
||||
Phase::Conflicts => (
|
||||
" Resolve update conflicts ",
|
||||
" [j/k] select [Space] keep/retry [Enter] continue [Esc] close ",
|
||||
),
|
||||
Phase::Review => (
|
||||
" Update installed content ",
|
||||
" [Enter] update all [Esc] close ",
|
||||
),
|
||||
Phase::Applying => (" Updating installed content ", " Please wait "),
|
||||
};
|
||||
let block = Block::default()
|
||||
.title(title)
|
||||
.title_bottom(Line::from(footer).centered())
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BORDER_STYLE.to_border_type())
|
||||
.border_style(Style::default().fg(theme.accent()))
|
||||
.style(Style::default().fg(theme.text()).bg(theme.surface()));
|
||||
pub fn show_review(&mut self) {
|
||||
self.phase = Phase::Review;
|
||||
self.rebuild_list();
|
||||
}
|
||||
|
||||
match state.phase {
|
||||
Phase::Checking | Phase::Applying => {
|
||||
let message = if state.phase == Phase::Checking {
|
||||
"Checking compatible versions and dependencies…"
|
||||
} else {
|
||||
"Installing the accepted update set…"
|
||||
};
|
||||
frame.render_widget(Paragraph::new(message).block(block), area);
|
||||
pub fn show_conflicts(&mut self) {
|
||||
if self
|
||||
.plan
|
||||
.as_ref()
|
||||
.is_some_and(|plan| !plan.conflicts.is_empty())
|
||||
{
|
||||
self.phase = Phase::Conflicts;
|
||||
self.rebuild_list();
|
||||
}
|
||||
Phase::Conflicts => {
|
||||
let items = state
|
||||
}
|
||||
|
||||
fn rebuild_list(&mut self) {
|
||||
let entries = match self.phase {
|
||||
Phase::Conflicts => self
|
||||
.plan
|
||||
.as_ref()
|
||||
.into_iter()
|
||||
.flat_map(|plan| plan.conflicts.iter())
|
||||
.enumerate()
|
||||
.map(|(index, conflict)| {
|
||||
let action = if state.retry_conflicts.get(index) == Some(&true) {
|
||||
"Retry"
|
||||
} else {
|
||||
"Keep"
|
||||
};
|
||||
ListItem::new(vec![
|
||||
Line::from(vec![
|
||||
Span::styled(
|
||||
conflict.title.clone(),
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::raw(" "),
|
||||
Span::styled(action, Style::default().fg(theme.accent())),
|
||||
]),
|
||||
Line::from(conflict.reason.clone())
|
||||
.style(Style::default().fg(theme.text_dim())),
|
||||
])
|
||||
});
|
||||
let mut selected = ListState::default().with_selected(Some(state.selected));
|
||||
let list = List::new(items)
|
||||
.block(block)
|
||||
.highlight_symbol("▌")
|
||||
.highlight_style(Style::default().bg(theme.stripe()));
|
||||
frame.render_stateful_widget(list, area, &mut selected);
|
||||
}
|
||||
Phase::Review => {
|
||||
let mut items = state
|
||||
.map(|conflict| {
|
||||
let mut entry = self.entry_for(&conflict.installed_path, &conflict.title);
|
||||
entry.description = user_conflict_reason(&conflict.reason);
|
||||
entry.title_suffix = Some("Skipped".to_owned());
|
||||
entry.footer_label = None;
|
||||
entry.footer_change = None;
|
||||
entry
|
||||
})
|
||||
.collect(),
|
||||
Phase::Review => self
|
||||
.plan
|
||||
.as_ref()
|
||||
.into_iter()
|
||||
.flat_map(|plan| plan.roots.iter())
|
||||
.map(|root| {
|
||||
ListItem::new(Line::from(format!(
|
||||
"• {} {} → {}",
|
||||
root.title, root.current_version, root.target.version_number
|
||||
)))
|
||||
let mut entry = self.entry_for(&root.installed_path, &root.title);
|
||||
entry.title_suffix = Some("Update".to_owned());
|
||||
entry.description.clear();
|
||||
entry.footer_label = None;
|
||||
entry.footer_change = Some((
|
||||
root.current_version.clone(),
|
||||
root.target.version_number.clone(),
|
||||
));
|
||||
entry
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if items.is_empty() {
|
||||
items.push(ListItem::new("No compatible updates available."));
|
||||
}
|
||||
if let Some(error) = &state.error {
|
||||
items.push(ListItem::new(
|
||||
Line::from(error.clone()).style(Style::default().fg(theme.error())),
|
||||
));
|
||||
}
|
||||
frame.render_widget(List::new(items).block(block), area);
|
||||
}
|
||||
.collect(),
|
||||
Phase::Checking | Phase::Applying => Vec::new(),
|
||||
};
|
||||
self.list.set_entries(entries);
|
||||
}
|
||||
|
||||
fn entry_for(&self, path: &Path, title: &str) -> ContentEntry {
|
||||
self.source_entries
|
||||
.get(path)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| ContentEntry {
|
||||
file_stem: path
|
||||
.file_stem()
|
||||
.and_then(|stem| stem.to_str())
|
||||
.unwrap_or(title)
|
||||
.to_owned(),
|
||||
name: title.to_owned(),
|
||||
source_slug: None,
|
||||
installed_path: Some(path.to_owned()),
|
||||
provider_project: None,
|
||||
world_details: None,
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
footer_change: None,
|
||||
description: String::new(),
|
||||
enabled: true,
|
||||
icon_bytes: None,
|
||||
provider_icon: false,
|
||||
provider_description: false,
|
||||
path: path.to_owned(),
|
||||
icon_lines: Some(crate::instance::content::fallback_icon()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(frame: &mut Frame, state: &mut State, picker: &ratatui_image::picker::Picker) {
|
||||
if !state.visible() {
|
||||
return;
|
||||
}
|
||||
let count = state.list.entries.len() as u16;
|
||||
let height = count.saturating_mul(3).saturating_add(2).clamp(5, 20);
|
||||
let area = frame.area().centered(
|
||||
Constraint::Percentage(60),
|
||||
Constraint::Length(height.min(frame.area().height.saturating_sub(4))),
|
||||
);
|
||||
let theme = crate::config::theme::THEME.as_ref();
|
||||
let (title, keybinds) = match state.phase {
|
||||
Phase::Conflicts => (
|
||||
"Updates needing attention",
|
||||
if state.has_updates() {
|
||||
crate::tui::widgets::popups::keybind_line(&[
|
||||
("j/k", " navigate"),
|
||||
("Enter", " review updates"),
|
||||
("h/Esc", " close"),
|
||||
])
|
||||
} else {
|
||||
crate::tui::widgets::popups::keybind_line(&[
|
||||
("j/k", " navigate"),
|
||||
("h/Esc", " close"),
|
||||
])
|
||||
},
|
||||
),
|
||||
Phase::Review => (
|
||||
"Update installed content",
|
||||
if state
|
||||
.plan
|
||||
.as_ref()
|
||||
.is_some_and(|plan| !plan.conflicts.is_empty())
|
||||
{
|
||||
crate::tui::widgets::popups::keybind_line(&[
|
||||
("j/k", " navigate"),
|
||||
("h", " back"),
|
||||
("Enter", " update all"),
|
||||
("Esc", " close"),
|
||||
])
|
||||
} else {
|
||||
crate::tui::widgets::popups::keybind_line(&[
|
||||
("j/k", " navigate"),
|
||||
("Enter", " update all"),
|
||||
("Esc", " close"),
|
||||
])
|
||||
},
|
||||
),
|
||||
Phase::Checking | Phase::Applying => return,
|
||||
};
|
||||
let popup = PopupFrame {
|
||||
title: crate::tui::widgets::styled_title(title, false),
|
||||
border_color: theme.accent(),
|
||||
bg: Some(theme.surface()),
|
||||
keybinds: Some(keybinds),
|
||||
search_line: None,
|
||||
content: Box::new(|_, _| {}),
|
||||
};
|
||||
frame.render_widget(popup, area);
|
||||
super::list::render(
|
||||
frame,
|
||||
area.inner(Margin::new(1, 1)),
|
||||
&mut state.list,
|
||||
true,
|
||||
"",
|
||||
"",
|
||||
picker,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
fn user_conflict_reason(reason: &str) -> String {
|
||||
let reason = reason.strip_prefix("Parse error: ").unwrap_or(reason);
|
||||
if let Some(details) = reason
|
||||
.strip_prefix("Conflicting selected versions for '")
|
||||
.or_else(|| reason.strip_prefix("Conflicting required versions for '"))
|
||||
&& let Some((dependency, versions)) = details.split_once("': '")
|
||||
&& let Some((first, second)) = versions.split_once("' and '")
|
||||
{
|
||||
return format!(
|
||||
"Other updates need different {dependency} versions ({first} and {}). This mod was left unchanged; update it separately with v.",
|
||||
second.trim_end_matches('\'')
|
||||
);
|
||||
}
|
||||
format!(
|
||||
"This update could not be prepared: {reason}. The installed version was left unchanged; try updating it separately with v."
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../../tests/widgets/content/update.rs"]
|
||||
mod tests;
|
||||
Reference in new issue
Block a user