Integrate all the progress into master #6
|
@ -2,6 +2,11 @@ use crossterm::event::KeyCode;
|
||||||
use readmap::types::Section;
|
use readmap::types::Section;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tool_helper::Error;
|
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<Event<crossterm::event::KeyEvent>>;
|
pub type EventReceiver = std::sync::mpsc::Receiver<Event<crossterm::event::KeyEvent>>;
|
||||||
pub type Terminal = tui::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>;
|
pub type Terminal = tui::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>;
|
||||||
|
@ -18,12 +23,14 @@ pub enum UIState {
|
||||||
|
|
||||||
pub struct ConsoleUI {
|
pub struct ConsoleUI {
|
||||||
rx: EventReceiver,
|
rx: EventReceiver,
|
||||||
terminal: Terminal
|
terminal: Terminal,
|
||||||
|
frame: Rect,
|
||||||
|
sub_frames: Vec<Rect>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConsoleUI {
|
impl ConsoleUI {
|
||||||
pub fn new(rx: EventReceiver, terminal: Terminal) -> 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<UIState, Error> {
|
pub fn update(&self) -> Result<UIState, Error> {
|
||||||
|
@ -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> {
|
pub fn close(mut self) -> Result<(), Error> {
|
||||||
crossterm::terminal::disable_raw_mode()?;
|
crossterm::terminal::disable_raw_mode()?;
|
||||||
|
@ -47,6 +64,28 @@ impl ConsoleUI {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_sub_frames(sub_frames: &mut Vec<Rect>, 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<PathBuf>) -> Result<Vec<Section>, Error> {
|
pub fn load_memory_map(file_path: Option<PathBuf>) -> Result<Vec<Section>, Error> {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use tui::backend::CrosstermBackend;
|
||||||
#[clap(about = "Opens and scans a MAP file to print extended information", long_about = None)]
|
#[clap(about = "Opens and scans a MAP file to print extended information", long_about = None)]
|
||||||
struct CommandLine {
|
struct CommandLine {
|
||||||
#[clap(value_parser, help="Input MAP file for scannning")]
|
#[clap(value_parser, help="Input MAP file for scannning")]
|
||||||
input: Option<PathBuf>
|
input: PathBuf
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
@ -27,13 +27,13 @@ pub fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_main(cmd: CommandLine) -> Result<(), Error> {
|
fn run_main(cmd: CommandLine) -> Result<(), Error> {
|
||||||
let _sections = load_memory_map(cmd.input)?;
|
let _sections = load_memory_map(Some(cmd.input))?;
|
||||||
let rx = start_event_loop();
|
let rx = start_event_loop();
|
||||||
let terminal = setup_console()?;
|
let terminal = setup_console()?;
|
||||||
let console_ui = ConsoleUI::new(rx, terminal);
|
let mut console_ui = ConsoleUI::new(rx, terminal);
|
||||||
|
|
||||||
while matches!(console_ui.update()?, psxreadmap::UIState::Alive) {
|
while matches!(console_ui.update()?, psxreadmap::UIState::Alive) {
|
||||||
console_ui.render();
|
console_ui.render()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
console_ui.close()
|
console_ui.close()
|
||||||
|
|
Loading…
Reference in New Issue