Fix rollover of list selection

This commit is contained in:
jaby 2023-08-12 12:43:00 +02:00
parent 331c55f572
commit 3a73641951
1 changed files with 33 additions and 26 deletions

View File

@ -56,24 +56,38 @@ pub struct ListSelection {
} }
impl 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) { pub fn increment(&mut self, max: usize) {
fn add_one(value: usize) -> usize { if let Some(cur_selection) = self.selection {
value + 1 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) { pub fn decrement(&mut self, max: usize) {
fn sub_one(value: usize) -> usize { if let Some(cur_selection) = self.selection {
value - 1 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) { pub fn activate(&mut self) {
@ -169,7 +183,7 @@ impl ConsoleUI {
Ok(UIState::Alive) Ok(UIState::Alive)
} }
}, },
_ => Ok(UIState::Alive) _ => Ok(UIState::Alive)
} }
}, },
Event::Tick => { Event::Tick => {
@ -240,9 +254,9 @@ impl ConsoleUI {
fn render_stats(frame: &mut ConsoleFrame, layout: Rect, content_selection: &ListSelection, data: &mut ConsoleUIData) { fn render_stats(frame: &mut ConsoleFrame, layout: Rect, content_selection: &ListSelection, data: &mut ConsoleUIData) {
let layout = Layout::default().direction(Direction::Vertical).constraints([ let layout = Layout::default().direction(Direction::Vertical).constraints([
Constraint::Min(3),
Constraint::Min(3),
Constraint::Length(3), Constraint::Length(3),
Constraint::Max(3),
Constraint::Max(3),
]).split(layout).to_vec(); ]).split(layout).to_vec();
let content_list = List::new({ let content_list = List::new({
let mut vec = Vec::new(); let mut vec = Vec::new();
@ -315,21 +329,14 @@ impl ConsoleUIData {
if let Some(section) = memory_map.sections.last() { if let Some(section) = memory_map.sections.last() {
return section.adr + section.size as u64; return section.adr + section.size as u64;
} }
0u64 0u64
} }
self.highest_adr = get_last_section_end_adr(memory_map); self.highest_adr = get_last_section_end_adr(memory_map);
self.list = vec![ self.list = vec![String::from("<overall usage>")];
String::from("Schwimmflugel 0"), for section in &memory_map.sections {
String::from("Schwimmflugel 1"), self.list.push(section.name.clone());
String::from("Schwimmflugel 2"), }
String::from("Schwimmflugel 3"),
String::from("Schwimmflugel 4"),
String::from("Schwimmflugel 5"),
String::from("Schwimmflugel 6"),
String::from("Schwimmflugel 7"),
];
Ok(()) Ok(())
} }
} }