From fc6704dc767dc5025dde737429f54916cebf340e Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 9 Jul 2023 15:38:55 +0200 Subject: [PATCH] Display titel --- src/Tools/psxreadmap/src/lib.rs | 47 +++++++++++++++++++++++++++++--- src/Tools/psxreadmap/src/main.rs | 12 ++++---- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/Tools/psxreadmap/src/lib.rs b/src/Tools/psxreadmap/src/lib.rs index 5b3ba1ae..a65b3c43 100644 --- a/src/Tools/psxreadmap/src/lib.rs +++ b/src/Tools/psxreadmap/src/lib.rs @@ -2,6 +2,11 @@ use crossterm::event::KeyCode; use readmap::types::Section; use std::path::PathBuf; use tool_helper::Error; +use tui::{ + layout::{Alignment, Constraint, Direction, Layout, Rect}, + style::{Color, Style}, + widgets::{Block, Borders, BorderType, Paragraph} +}; pub type EventReceiver = std::sync::mpsc::Receiver>; pub type Terminal = tui::Terminal>; @@ -17,13 +22,15 @@ pub enum UIState { } pub struct ConsoleUI { - rx: EventReceiver, - terminal: Terminal + rx: EventReceiver, + terminal: Terminal, + frame: Rect, + sub_frames: Vec } impl ConsoleUI { pub fn new(rx: EventReceiver, terminal: Terminal) -> ConsoleUI { - ConsoleUI{rx, terminal} + ConsoleUI{rx, terminal, frame: Rect::default(), sub_frames: Vec::default()} } pub fn update(&self) -> Result { @@ -38,7 +45,17 @@ impl ConsoleUI { } } - pub fn render(&self) {} + pub fn render(&mut self) -> Result<(), Error> { + self.terminal.draw(|frame| { + Self::update_sub_frames(&mut self.sub_frames, &mut self.frame, frame.size()); + + frame.render_widget(Self::create_titel(), self.sub_frames[0]); + frame.render_widget(Self::create_titel(), self.sub_frames[1]); + frame.render_widget(Self::create_titel(), self.sub_frames[2]); + })?; + + Ok(()) + } pub fn close(mut self) -> Result<(), Error> { crossterm::terminal::disable_raw_mode()?; @@ -47,6 +64,28 @@ impl ConsoleUI { Ok(()) } + + fn update_sub_frames(sub_frames: &mut Vec, frame: &mut Rect, new_frame: Rect) { + if new_frame != *frame { + *sub_frames = Layout::default().direction(Direction::Vertical).constraints([ + Constraint::Length(3), + Constraint::Length(3), + Constraint::Min(3) + ]).split(new_frame); + *frame = new_frame; + } + } + + fn create_titel<'a>() -> Paragraph<'a> { + Paragraph::new("psxreadmap") + .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) + ) + } } pub fn load_memory_map(file_path: Option) -> Result, Error> { diff --git a/src/Tools/psxreadmap/src/main.rs b/src/Tools/psxreadmap/src/main.rs index 9fc61015..2e1a742f 100644 --- a/src/Tools/psxreadmap/src/main.rs +++ b/src/Tools/psxreadmap/src/main.rs @@ -9,7 +9,7 @@ use tui::backend::CrosstermBackend; #[clap(about = "Opens and scans a MAP file to print extended information", long_about = None)] struct CommandLine { #[clap(value_parser, help="Input MAP file for scannning")] - input: Option + input: PathBuf } pub fn main() { @@ -27,13 +27,13 @@ pub fn main() { } fn run_main(cmd: CommandLine) -> Result<(), Error> { - let _sections = load_memory_map(cmd.input)?; - let rx = start_event_loop(); - let terminal = setup_console()?; - let console_ui = ConsoleUI::new(rx, terminal); + let _sections = load_memory_map(Some(cmd.input))?; + let rx = start_event_loop(); + let terminal = setup_console()?; + let mut console_ui = ConsoleUI::new(rx, terminal); while matches!(console_ui.update()?, psxreadmap::UIState::Alive) { - console_ui.render(); + console_ui.render()?; } console_ui.close()