Integrate all the progress into master #6

Merged
jaby merged 595 commits from ToolBox into main 2025-01-01 13:17:44 +00:00
1 changed files with 125 additions and 78 deletions
Showing only changes of commit dfd24e4e94 - Show all commits

View File

@ -1,7 +1,7 @@
mod readmap_helper;
use crossterm::event::KeyCode;
use crossterm::event::{KeyCode, KeyEvent};
use readmap::types::MemoryMap;
use std::path::PathBuf;
use std::{convert::From, path::PathBuf};
use tool_helper::Error;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
@ -26,27 +26,40 @@ pub enum UIState {
Terminated
}
#[derive(Copy, Clone)]
enum MenuSelection {
Stats,
List,
Quit,
}
impl MenuSelection {
const ENUM_LENGTH:i32 = 3;
fn next(&self) -> MenuSelection {
match self {
Self::Stats => Self::Quit,
Self::Quit => Self::Stats
}
Self::from(*self as i32 + 1)
}
fn prev(&self) -> MenuSelection {
self.next()
Self::from(*self as i32 - 1)
}
fn get_id(&self) -> usize {
match self {
MenuSelection::Stats => 0,
MenuSelection::Quit => 1,
*self as usize
}
}
impl From<i32> for MenuSelection {
fn from(value: i32) -> Self {
if value < 0 {
return Self::from(value + Self::ENUM_LENGTH);
}
match value%Self::ENUM_LENGTH {
x if x == MenuSelection::Stats as i32 => MenuSelection::Stats,
x if x == MenuSelection::List as i32 => MenuSelection::List,
x if x == MenuSelection::Quit as i32 => MenuSelection::Quit,
_ => MenuSelection::Quit
}
}
}
@ -58,7 +71,7 @@ enum SectionMode {
}
impl SectionMode {
pub fn as_id(&self) -> usize {
pub fn get_id(&self) -> usize {
match self {
SectionMode::UpperSection => 0,
SectionMode::LowerSection => 1,
@ -73,7 +86,7 @@ struct SectionSelection {
impl SectionSelection {
fn increment(&mut self, max: usize) {
let cur_id = &mut self.id[self.mode.as_id()];
let cur_id = &mut self.id[self.mode.get_id()];
let new_selection = *cur_id + 1;
*cur_id = {
@ -88,7 +101,7 @@ impl SectionSelection {
}
fn decrement(&mut self, max: usize) {
let cur_id = &mut self.id[self.mode.as_id()];
let cur_id = &mut self.id[self.mode.get_id()];
let new_selection = *cur_id - 1;
*cur_id = {
@ -107,11 +120,11 @@ impl SectionSelection {
}
fn get_section_mode_id(&self) -> usize {
self.mode.as_id()
self.mode.get_id()
}
fn get_selection_for(&self, section_mode: SectionMode) -> usize {
self.id[section_mode.as_id()]
self.id[section_mode.get_id()]
}
fn get_current_selection(&self) -> usize {
@ -150,51 +163,39 @@ impl ConsoleUI {
let input_result = {
match self.rx.recv()? {
Event::Input(event) => {
if matches!(self.menu_selection, MenuSelection::Stats) {
match event.code {
KeyCode::Down => {
self.section_selection.increment(self.data.section_info.len());
return Ok(UIState::Render);
},
KeyCode::Up => {
self.section_selection.decrement(self.data.section_info.len());
return Ok(UIState::Render);
},
KeyCode::Char('a') => {
self.section_selection.switch_section_to(SectionMode::UpperSection);
return Ok(UIState::Render);
},
KeyCode::Char('s') => {
self.section_selection.switch_section_to(SectionMode::LowerSection);
return Ok(UIState::Render);
},
_ => ()
let state_update = {
match self.menu_selection {
MenuSelection::Stats => self.update_stats(event),
MenuSelection::List => self.update_list(event),
MenuSelection::Quit => self.update_quit(event),
}
};
if let Some(result) = state_update {
Ok(result)
}
match event.code {
KeyCode::Char('q') => Ok(UIState::Terminated),
KeyCode::Char('s') => {
self.menu_selection = MenuSelection::Stats;
Ok(UIState::Render)
},
KeyCode::Left => {
self.menu_selection = self.menu_selection.prev();
Ok(UIState::Render)
},
KeyCode::Right => {
self.menu_selection = self.menu_selection.next();
Ok(UIState::Render)
},
KeyCode::Enter => {
if matches!(self.menu_selection, MenuSelection::Quit) {
Ok(UIState::Terminated)
}
else {
Ok(UIState::Alive)
}
},
_ => Ok(UIState::Alive)
else {
match event.code {
KeyCode::Char('l') => {
self.menu_selection = MenuSelection::List;
Ok(UIState::Render)
},
KeyCode::Char('s') => {
self.menu_selection = MenuSelection::Stats;
Ok(UIState::Render)
},
KeyCode::Char('q') => Ok(UIState::Terminated),
KeyCode::Left => {
self.menu_selection = self.menu_selection.prev();
Ok(UIState::Render)
},
KeyCode::Right => {
self.menu_selection = self.menu_selection.next();
Ok(UIState::Render)
},
_ => Ok(UIState::Alive)
}
}
},
Event::Tick => {
@ -225,7 +226,7 @@ impl ConsoleUI {
// Elements
let menu_bar = {
const MENU_TITLES: &'static [&'static str] = &["Stats", "Quit"];
const MENU_TITLES: &'static [&'static str] = &["Stats", "List", "Quit"];
let menu = MENU_TITLES.iter().map(|t| {
let (first, rest) = t.split_at(1);
@ -250,8 +251,11 @@ impl ConsoleUI {
MenuSelection::Stats => {
Self::render_stats(frame, main_layout[1], &self.section_selection, &mut self.data);
},
MenuSelection::List => {
Self::render_list(frame, main_layout[1]);
},
MenuSelection::Quit => {
frame.render_widget(Self::create_exit_message(), main_layout[1]);
Self::render_quit(frame, main_layout[1]);
}
}
})?;
@ -266,6 +270,39 @@ impl ConsoleUI {
Ok(())
}
fn update_stats(&mut self, key: KeyEvent) -> Option<UIState> {
match key.code {
KeyCode::Down => {
self.section_selection.increment(self.data.section_info.len());
return Some(UIState::Render);
},
KeyCode::Up => {
self.section_selection.decrement(self.data.section_info.len());
return Some(UIState::Render);
},
KeyCode::Char('a') => {
self.section_selection.switch_section_to(SectionMode::UpperSection);
return Some(UIState::Render);
},
KeyCode::Char('s') => {
self.section_selection.switch_section_to(SectionMode::LowerSection);
return Some(UIState::Render);
},
_ => None
}
}
fn update_list(&mut self, _key: KeyEvent) -> Option<UIState> {
None
}
fn update_quit(&mut self, key: KeyEvent) -> Option<UIState> {
match key.code {
KeyCode::Enter => Some(UIState::Terminated),
_ => None,
}
}
fn render_stats(frame: &mut ConsoleFrame, layout: Rect, section_selection: &SectionSelection, data: &mut ConsoleUIData) {
const HIGHLIGHT_COLOR:[Color;2] = [Color::Cyan, Color::Red];
let layout = Layout::default().direction(Direction::Vertical).constraints([
@ -273,14 +310,9 @@ impl ConsoleUI {
Constraint::Max(3),
Constraint::Max(3),
]).split(layout).to_vec();
let content_list = List::new({
let mut vec = Vec::new();
for section in &data.section_info {
vec.push(ListItem::new(section.name.clone()))
}
vec
}).style(Style::default().fg(Color::White))
let content_list = List::new(data.section_info.iter().map(|section| {
ListItem::new(section.name.clone())
}).collect::<Vec<_>>()).style(Style::default().fg(Color::White))
.block(Block::default()
.borders(Borders::ALL)
.style(Style::default().fg(Color::White))
@ -299,6 +331,32 @@ impl ConsoleUI {
frame.render_widget(second_mem_gauge, layout[2]);
}
fn render_list(frame: &mut ConsoleFrame, layout: Rect) {
let text = Paragraph::new("Comming soon")
.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")
);
frame.render_widget(text, layout);
}
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")
);
frame.render_widget(text, layout);
}
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 = {
@ -332,17 +390,6 @@ impl ConsoleUI {
}
})
}
fn create_exit_message<'a>() -> Paragraph<'a> {
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")
)
}
}
#[derive(Default)]