From 8a6ebe779fecf840c955f26c7ae251c793e0941c Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 20 Aug 2023 15:29:21 +0200 Subject: [PATCH] Preear Symbol list --- src/Tools/psxreadmap/src/lib.rs | 112 ++++++++++++++++++++----------- src/Tools/psxreadmap/src/main.rs | 2 +- 2 files changed, 74 insertions(+), 40 deletions(-) diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index 311fcc26..03afd65e 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -1,13 +1,13 @@ mod readmap_helper; use crossterm::event::{KeyCode, KeyEvent}; -use readmap::types::MemoryMap; +use readmap::types::{MemoryMap, Symbol}; use std::{convert::From, path::PathBuf}; use tool_helper::Error; use ratatui::{ layout::{Alignment, Constraint, Direction, Layout, Rect}, style::{Color, Modifier, Style}, text::{Line, Span}, - widgets::{Block, Borders, BorderType, Gauge, List, ListItem, ListState, Paragraph, Tabs} + widgets::{Block, Borders, BorderType, Gauge, List, ListItem, ListState, Paragraph, Tabs, block::Title} }; pub type EventReceiver = std::sync::mpsc::Receiver>; @@ -136,7 +136,7 @@ impl DualSelection { } } - fn switch_section_to(&mut self, section_mode: T) { + fn switch_mode_to(&mut self, section_mode: T) { self.mode = section_mode; } @@ -148,12 +148,16 @@ impl DualSelection { self.id[section_mode.get_id()] } - fn get_current_selection(&self) -> ListState { + fn get_selection_state_for(&self, section_mode: T) -> ListState { let mut state = ListState::default(); - state.select(Some(self.get_selection_for(self.mode))); + state.select(Some(self.get_selection_for(section_mode))); state } + + fn get_current_selection_state(&self) -> ListState { + self.get_selection_state_for(self.mode) + } } impl Default for DualSelection { @@ -181,7 +185,7 @@ impl ConsoleUI { ConsoleUI{data: ConsoleUIData::default(), rx, terminal, console_size: Rect::default(), menu_selection: MenuSelection::Stats, section_selection: DualSelection::default(), list_section_selection: DualSelection::default()} } - pub fn update_data(&mut self, memory_map: &MemoryMap) -> Result<(), Error> { + pub fn update_data(&mut self, memory_map: MemoryMap) -> Result<(), Error> { self.data = ConsoleUIData::from(memory_map)?; self.section_selection = DualSelection::default(); self.list_section_selection = DualSelection::default(); @@ -311,11 +315,11 @@ impl ConsoleUI { return Some(UIState::Render); }, KeyCode::Char('a') => { - self.section_selection.switch_section_to(SectionMode::UpperSection); + self.section_selection.switch_mode_to(SectionMode::UpperSection); return Some(UIState::Render); }, KeyCode::Char('s') => { - self.section_selection.switch_section_to(SectionMode::LowerSection); + self.section_selection.switch_mode_to(SectionMode::LowerSection); return Some(UIState::Render); }, _ => None @@ -333,6 +337,14 @@ impl ConsoleUI { self.list_section_selection.decrement(length); return Some(UIState::Render); }, + KeyCode::Enter => { + self.list_section_selection.switch_mode_to(ListSectionMode::Section); + return Some(UIState::Render); + } + KeyCode::Esc => { + self.list_section_selection.switch_mode_to(ListSectionMode::TopSection); + return Some(UIState::Render); + } _ => None } } @@ -354,17 +366,13 @@ impl ConsoleUI { let content_list = List::new(data.section_info.iter().map(|section| { ListItem::new(section.name.clone()) }).collect::>()).style(Style::default().fg(Color::White)) - .block(Block::default() - .borders(Borders::ALL) - .style(Style::default().fg(Color::White)) - .border_type(BorderType::Plain) - ) + .block(Self::create_default_border("")) .highlight_style(Style::default().bg(HIGHLIGHT_COLOR[section_selection.get_section_mode_id()])); let first_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::UpperSection)], "a", HIGHLIGHT_COLOR[0]); let second_mem_gauge = Self::create_mem_gauge_from(&data.section_info[section_selection.get_selection_for(SectionMode::LowerSection)], "s", HIGHLIGHT_COLOR[1]); - frame.render_stateful_widget(content_list, layout[0], &mut section_selection.get_current_selection()); + frame.render_stateful_widget(content_list, layout[0], &mut section_selection.get_current_selection_state()); frame.render_widget(first_mem_gauge, layout[1]); frame.render_widget(second_mem_gauge, layout[2]); } @@ -382,45 +390,68 @@ impl ConsoleUI { ]).split(layout[0])) }; - let section_list = List::new(data.section_info.iter().skip(1).map(|section| { + let section_list = List::new(data.section_info.iter().skip(1).map( + |section| { ListItem::new(section.name.clone()) }).collect::>()).style(Style::default().fg(Color::White)) - .block(Block::default() - .borders(Borders::ALL) - .style(Style::default().fg(Color::White)) - .border_type(BorderType::Plain) - .title("Section") - ) + .block(Self::create_default_border("Section")) .highlight_style(Style::default().bg(Color::White)); - let top_section = &data.section_info[section_selection.get_selection_for(ListSectionMode::TopSection) + 1]; - let info_text = Paragraph::new(format!("Name: {}\nAdr: 0x{:X} - 0x{:X}\nSize: {} Bytes", top_section.name, top_section.start_adr, top_section.get_end_adr(), top_section.size)) + let current_selection = section_selection.get_selection_for(ListSectionMode::TopSection); + let top_section = &data.section_info[current_selection + 1]; + let info_text = Paragraph::new(format!("Name: {}\nAdr: 0x{:X} - 0x{:X}\nSize: {} Bytes", top_section.name, top_section.start_adr, top_section.get_end_adr(), top_section.size)) .style(Style::default().fg(Color::White)) .alignment(Alignment::Left) - .block(Block::default() - .borders(Borders::ALL) - .style(Style::default().fg(Color::White)) - .border_type(BorderType::Plain).title("Information") - ); + .block(Self::create_default_border("Information")); frame.render_widget(info_text, info_layout); - frame.render_stateful_widget(section_list, list_layout[0], &mut section_selection.get_current_selection()); - //frame.render_widget(text, list_layout[1]); + frame.render_stateful_widget(section_list, list_layout[0], &mut section_selection.get_selection_state_for(ListSectionMode::TopSection)); + + if matches!(section_selection.mode, ListSectionMode::TopSection) { + let text = Paragraph::new("Press \"ENTER\" to display") + .style(Style::default().fg(Color::White)) + .alignment(Alignment::Center) + .block(Self::create_default_border("Symbols")); + + frame.render_widget(text, list_layout[1]); + } + + else { + let text = Paragraph::new("Press \"ESC\" to leave") + .style(Style::default().fg(Color::White)) + .alignment(Alignment::Center) + .block(Self::create_default_border(format!("Symbols in {}", top_section.name))); + + frame.render_widget(text, list_layout[1]); + + + /*let symbol_list = List::new(data.section_info.iter().skip(1).map( + |section| { + ListItem::new(section.name.clone()) + }).collect::>()).style(Style::default().fg(Color::White)) + .block(Self::create_default_border("Section")) + .highlight_style(Style::default().bg(Color::White));*/ + + } } fn render_quit(frame: &mut ConsoleFrame, layout: Rect) { let text = Paragraph::new("Press \"ENTER\" to exit") .style(Style::default().fg(Color::White)) .alignment(Alignment::Center) - .block(Block::default() - .borders(Borders::ALL) - .style(Style::default().fg(Color::White)) - .border_type(BorderType::Plain).title("QUIT") - ); + .block(Self::create_default_border("QUIT")); frame.render_widget(text, layout); } + fn create_default_border<'a, T: Into>>(title: T) -> Block<'a> { + Block::default() + .borders(Borders::ALL) + .style(Style::default().fg(Color::White)) + .border_type(BorderType::Plain) + .title(title) + } + fn create_mem_gauge_from<'a>(section_info: &SectionInfo, string: &'a str, color: Color) -> Gauge<'a> { const PERCISION:usize = 5; const FLOAT_DISPLAY_LIMIT:f64 = { @@ -465,7 +496,7 @@ impl ConsoleUIData { pub const RAM_BASE_ADDRESS:u64 = 0x80000000; pub const RAM_SIZE:usize = 2*1024*1024; - pub fn from(memory_map: &MemoryMap) -> Result { + 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; @@ -474,19 +505,21 @@ impl ConsoleUIData { } let mut new_self = ConsoleUIData::default(); - let highest_adr = get_last_section_end_adr(memory_map); + let highest_adr = get_last_section_end_adr(&memory_map); let program_size = (highest_adr - Self::RAM_BASE_ADDRESS) as usize; new_self.section_info.push(SectionInfo{ name: String::from(""), + symbols: memory_map.global, start_adr: Self::RAM_BASE_ADDRESS, size: program_size, max_size: Self::RAM_SIZE }); - for section in &memory_map.sections { + for section in memory_map.sections { new_self.section_info.push(SectionInfo{ - name: section.name.clone(), + name: section.name, + symbols: section.symbols, start_adr: section.adr, size: section.size, max_size: program_size @@ -500,6 +533,7 @@ impl ConsoleUIData { #[derive(Default)] struct SectionInfo { pub name: String, + pub symbols: Vec, pub start_adr: u64, pub size: usize, pub max_size: usize, diff --git a/src/Tools/psxreadmap/src/main.rs b/src/Tools/psxreadmap/src/main.rs index d75e7d3e..69bb489f 100644 --- a/src/Tools/psxreadmap/src/main.rs +++ b/src/Tools/psxreadmap/src/main.rs @@ -36,7 +36,7 @@ fn run_main(cmd: CommandLine) -> Result<(), Error> { let terminal = setup_console()?; let mut console_ui = ConsoleUI::new(rx, terminal); - console_ui.update_data(&memory_map)?; + console_ui.update_data(memory_map)?; loop { match console_ui.update()? { UIState::Alive => (),