fix: select the displayed version in the instance wizard

This commit is contained in:
objz committed 2026-10-01 18:48:05 +02:00
1 parent 75c74333c5
commit 4de886fcd0
3 files changed
+29 -30

No files matched your search

@@ -12,7 +12,7 @@ fn key(code: KeyCode) -> KeyEvent {
}
#[test]
fn vanilla_wizard_reaches_confirm_and_returns_parameters() {
fn filtered_vanilla_wizard_returns_the_displayed_version() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|error| error.into_inner());
let mut instances = instances::State {
show_popup: true,
@@ -32,10 +32,17 @@ fn vanilla_wizard_reaches_confirm_and_returns_parameters() {
{
let mut state = WIZARD_STATE.lock().unwrap();
state.step = WizardStep::Version;
state.versions = LoadState::Loaded(vec![GameVersion {
id: "1.21.1".to_owned(),
stable: true,
}]);
state.versions = LoadState::Loaded(vec![
GameVersion {
id: "1.20.1".to_owned(),
stable: true,
},
GameVersion {
id: "1.21.1".to_owned(),
stable: true,
},
]);
state.version_search.query = "1.21.1".to_owned();
}
handle_key(&key(KeyCode::Enter), &mut instances);
assert_eq!(WIZARD_STATE.lock().unwrap().step, WizardStep::Confirm);
@@ -68,10 +75,12 @@ fn version_filter_clamps_a_stale_selection() {
clamp_version_index(&mut state);
assert_eq!(state.version_idx, 0);
assert_eq!(visible_versions(&state).len(), 1);
assert_eq!(visible_versions(&state).count(), 1);
state.show_snapshots = true;
assert_eq!(visible_versions(&state).len(), 2);
assert_eq!(visible_versions(&state).count(), 2);
state.version_search.query = "missing".to_owned();
assert!(state.selected_version().is_none());
}
#[test]
@@ -178,7 +178,6 @@ fn render_version_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe
}
LoadState::Loaded(_) => {
let items: Vec<ListItem> = visible_versions(state)
.into_iter()
.enumerate()
.map(|(index, version)| {
let suffix = if version.stable {
+13 -22
View File
@@ -72,15 +72,7 @@ impl WizardState {
}
pub fn selected_version(&self) -> Option<&GameVersion> {
if let LoadState::Loaded(ref versions) = self.versions {
let visible: Vec<_> = versions
.iter()
.filter(|v| self.show_snapshots || v.stable)
.collect();
visible.get(self.version_idx).copied()
} else {
None
}
visible_versions(self).nth(self.version_idx)
}
pub fn selected_loader(&self) -> ModLoader {
@@ -178,14 +170,14 @@ fn handle_version_key(
KeyCode::Char('k') | KeyCode::Up => {}
KeyCode::Char(c) => {
state.version_search.push(c);
state.version_idx = 0; // reset to top of filtered list
state.version_idx = 0;
return;
}
_ => {}
}
}
let visible_count = visible_versions(state).len();
let visible_count = visible_versions(state).count();
match key_event.code {
KeyCode::Esc => {
@@ -344,21 +336,20 @@ fn close_popup(state: &mut WizardState, instances_state: &mut instances::State)
instances_state.show_popup = false;
}
pub(crate) fn visible_versions(state: &WizardState) -> Vec<GameVersion> {
pub(crate) fn visible_versions(state: &WizardState) -> impl Iterator<Item = &GameVersion> {
let q = state.version_search.query.to_lowercase();
match &state.versions {
LoadState::Loaded(versions) => versions
.iter()
.filter(|v| state.show_snapshots || v.stable)
.filter(|v| q.is_empty() || v.id.to_lowercase().contains(&q))
.cloned()
.collect(),
_ => Vec::new(),
}
let versions = match &state.versions {
LoadState::Loaded(versions) => versions.as_slice(),
_ => &[],
};
versions.iter().filter(move |version| {
(state.show_snapshots || version.stable)
&& (q.is_empty() || version.id.to_lowercase().contains(&q))
})
}
pub(crate) fn clamp_version_index(state: &mut WizardState) {
let count = visible_versions(state).len();
let count = visible_versions(state).count();
if count == 0 {
state.version_idx = 0;
} else if state.version_idx >= count {