jabyengine/src/Tools/psxreadmap/src/lib.rs

54 lines
1.3 KiB
Rust

use crossterm::event::KeyCode;
use readmap::types::Section;
use std::path::PathBuf;
use tool_helper::Error;
pub type EventReceiver = std::sync::mpsc::Receiver<Event<crossterm::event::KeyEvent>>;
pub type Terminal = tui::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>;
pub enum Event<I> {
Input(I),
Tick,
}
pub enum UIState {
Alive,
Terminated
}
pub struct ConsoleUI {
rx: EventReceiver,
terminal: Terminal
}
impl ConsoleUI {
pub fn new(rx: EventReceiver, terminal: Terminal) -> ConsoleUI {
ConsoleUI{rx, terminal}
}
pub fn update(&self) -> Result<UIState, Error> {
match self.rx.recv()? {
Event::Input(event) => {
match event.code {
KeyCode::Char('q') => Ok(UIState::Terminated),
_ => Ok(UIState::Alive)
}
},
Event::Tick => Ok(UIState::Alive)
}
}
pub fn render(&self) {}
pub fn close(mut self) -> Result<(), Error> {
crossterm::terminal::disable_raw_mode()?;
self.terminal.show_cursor()?;
self.terminal.clear()?;
Ok(())
}
}
pub fn load_memory_map(file_path: Option<PathBuf>) -> Result<Vec<Section>, Error> {
readmap::scan(tool_helper::open_input(file_path)?)
}