From a803dbbfd4c58a49ccb0618423dfb8c29c43985d Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 12 Aug 2023 12:43:00 +0200 Subject: [PATCH] Fix rollover of list selection --- src/Tools/psxreadmap/src/lib.rs | 59 ++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index 7d6c187c..097f2ee4 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -56,24 +56,38 @@ pub struct ListSelection { } impl ListSelection { - fn change_value(&mut self, max: usize, f: fn(usize)->usize) { - if let Some(cur_selection) = self.selection { - self.selection = Some(f(cur_selection)%max); - } - } - pub fn increment(&mut self, max: usize) { - fn add_one(value: usize) -> usize { - value + 1 + if let Some(cur_selection) = self.selection { + self.selection = Some({ + if cur_selection + 1 >= max { + 0 + } + + else { + cur_selection + 1 + } + }); } - self.change_value(max, add_one) } pub fn decrement(&mut self, max: usize) { - fn sub_one(value: usize) -> usize { - value - 1 + if let Some(cur_selection) = self.selection { + self.selection = Some({ + if cur_selection - 1 >= max { + if max - 1 > 0 { + max - 1 + } + + else { + 0 + } + } + + else { + cur_selection - 1 + } + }); } - self.change_value(max, sub_one) } pub fn activate(&mut self) { @@ -169,7 +183,7 @@ impl ConsoleUI { Ok(UIState::Alive) } }, - _ => Ok(UIState::Alive) + _ => Ok(UIState::Alive) } }, Event::Tick => { @@ -240,9 +254,9 @@ impl ConsoleUI { fn render_stats(frame: &mut ConsoleFrame, layout: Rect, content_selection: &ListSelection, data: &mut ConsoleUIData) { let layout = Layout::default().direction(Direction::Vertical).constraints([ - Constraint::Min(3), - Constraint::Min(3), Constraint::Length(3), + Constraint::Max(3), + Constraint::Max(3), ]).split(layout).to_vec(); let content_list = List::new({ let mut vec = Vec::new(); @@ -315,21 +329,14 @@ impl ConsoleUIData { if let Some(section) = memory_map.sections.last() { return section.adr + section.size as u64; } - 0u64 } self.highest_adr = get_last_section_end_adr(memory_map); - self.list = vec![ - String::from("Schwimmflugel 0"), - String::from("Schwimmflugel 1"), - String::from("Schwimmflugel 2"), - String::from("Schwimmflugel 3"), - String::from("Schwimmflugel 4"), - String::from("Schwimmflugel 5"), - String::from("Schwimmflugel 6"), - String::from("Schwimmflugel 7"), - ]; + self.list = vec![String::from("")]; + for section in &memory_map.sections { + self.list.push(section.name.clone()); + } Ok(()) } }