From f2328601edbf8443e6231cc3ffbaf86b617ce3f6 Mon Sep 17 00:00:00 2001 From: jaby Date: Sat, 12 Aug 2023 19:13:38 +0200 Subject: [PATCH] Introduce new gauge type --- src/Tools/psxreadmap/src/lib.rs | 80 ++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index 097f2ee4..9d6c2628 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -71,20 +71,15 @@ impl ListSelection { } pub fn decrement(&mut self, max: usize) { - if let Some(cur_selection) = self.selection { + if let Some(mut cur_selection) = self.selection { self.selection = Some({ - if cur_selection - 1 >= max { - if max - 1 > 0 { + cur_selection = cur_selection - 1; + if cur_selection > max - 1 { max - 1 - } - - else { - 0 - } } else { - cur_selection - 1 + cur_selection } }); } @@ -124,7 +119,7 @@ impl ConsoleUI { } pub fn update_data(&mut self, memory_map: &MemoryMap) -> Result<(), Error> { - self.data.update(memory_map)?; + self.data = ConsoleUIData::from(memory_map)?; if self.data.list.len() > 0 { if !self.content_selection.is_active() { @@ -273,7 +268,7 @@ impl ConsoleUI { ) .highlight_style(Style::default().bg(Color::Cyan)); let first_mem_gauge = Self::create_mem_gauge("Overall memory usage", data.highest_adr, Color::Cyan); - let second_mem_gauge = Self::create_mem_gauge("Overall memory usage", data.highest_adr, Color::Red); + let second_mem_gauge = Self::create_mem_gauge_from(&data.overall_info, Color::Red); let mut list_state = ListState::default(); list_state.select(content_selection.selection); @@ -295,6 +290,15 @@ impl ConsoleUI { .ratio(adr_ratio).label(format!("{}%", adr_ratio*100.0f64)) } + fn create_mem_gauge_from<'a>(section_info: &SectionInfo, color: Color) -> Gauge<'a> { + let adr_ratio = section_info.get_size_ratio(); + Gauge::default().block(Block::default().borders(Borders::ALL) + .title(format!("{} @0x{:x} - 0x{:x} [{}b/{}b]", + section_info.name, section_info.start_adr, section_info.get_end_adr(), section_info.size, section_info.max_size))) + .gauge_style(Style::default().fg(color).bg(Color::Black).add_modifier(Modifier::BOLD)) + .ratio(adr_ratio).label(format!("{}%", adr_ratio*100.0f64)) + } + fn create_exit_message<'a>() -> Paragraph<'a> { Paragraph::new("Press \"ENTER\" to exit") .style(Style::default().fg(Color::White)) @@ -309,22 +313,17 @@ impl ConsoleUI { #[derive(Default)] struct ConsoleUIData { - highest_adr: u64, - list: Vec + highest_adr: u64, + overall_info: SectionInfo, + list: Vec } impl ConsoleUIData { pub const RAM_BASE_ADDRESS:u64 = 0x80000000; - pub const HIGHEST_RAM_ADDRESS:u64 = Self::RAM_BASE_ADDRESS + (2*1024*1024); + pub const RAM_SIZE:usize = 2*1024*1024; + pub const HIGHEST_RAM_ADDRESS:u64 = Self::RAM_BASE_ADDRESS + Self::RAM_SIZE as u64; - fn _new(memory_map: &MemoryMap) -> Result { - let mut ui_data = ConsoleUIData::default(); - - ui_data.update(memory_map)?; - Ok(ui_data) - } - - pub fn update(&mut self, memory_map: &MemoryMap) -> Result<(), Error> { + pub fn from(memory_map: &MemoryMap) -> Result { fn get_last_section_end_adr(memory_map: &MemoryMap) -> u64 { if let Some(section) = memory_map.sections.last() { return section.adr + section.size as u64; @@ -332,12 +331,41 @@ impl ConsoleUIData { 0u64 } - self.highest_adr = get_last_section_end_adr(memory_map); - self.list = vec![String::from("")]; + let mut new_self = ConsoleUIData::default(); + + new_self.highest_adr = get_last_section_end_adr(memory_map); + new_self.list = vec![String::from("")]; for section in &memory_map.sections { - self.list.push(section.name.clone()); + new_self.list.push(section.name.clone()); } - Ok(()) + + // Testing new data type + new_self.overall_info = SectionInfo{ + name: "Schwimmflügel".to_string(), + start_adr: Self::RAM_BASE_ADDRESS, + size: (new_self.highest_adr - Self::RAM_BASE_ADDRESS) as usize, + max_size: Self::RAM_SIZE + }; + + Ok(new_self) + } +} + +#[derive(Default)] +struct SectionInfo { + pub name: String, + pub start_adr: u64, + pub size: usize, + pub max_size: usize, +} + +impl SectionInfo { + fn get_end_adr(&self) -> u64 { + self.start_adr + self.size as u64 + } + + fn get_size_ratio(&self) -> f64 { + self.size as f64 / self.max_size as f64 } }