2023-06-12 05:07:15 +00:00
|
|
|
use std::{error::Error, io, sync::mpsc, thread, time::Duration};
|
|
|
|
|
2023-07-10 22:59:01 +00:00
|
|
|
use ratatui::prelude::*;
|
2021-11-01 21:27:46 +00:00
|
|
|
use termion::{
|
|
|
|
event::Key,
|
|
|
|
input::{MouseTerminal, TermRead},
|
|
|
|
raw::IntoRawMode,
|
2023-02-15 12:59:50 +00:00
|
|
|
screen::IntoAlternateScreen,
|
2021-11-01 21:27:46 +00:00
|
|
|
};
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2023-06-12 05:07:15 +00:00
|
|
|
use crate::{app::App, ui};
|
|
|
|
|
2021-11-11 14:56:37 +00:00
|
|
|
pub fn run(tick_rate: Duration, enhanced_graphics: bool) -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
2023-02-15 12:59:50 +00:00
|
|
|
let stdout = io::stdout()
|
|
|
|
.into_raw_mode()
|
|
|
|
.unwrap()
|
|
|
|
.into_alternate_screen()
|
|
|
|
.unwrap();
|
2019-02-10 21:35:44 +00:00
|
|
|
let stdout = MouseTerminal::from(stdout);
|
|
|
|
let backend = TermionBackend::new(stdout);
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
2021-11-11 14:56:37 +00:00
|
|
|
let app = App::new("Termion demo", enhanced_graphics);
|
2021-11-01 21:27:46 +00:00
|
|
|
run_app(&mut terminal, app, tick_rate)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
|
|
|
let events = events(tick_rate);
|
2019-02-10 21:35:44 +00:00
|
|
|
loop {
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| ui::draw(f, &mut app))?;
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
match events.recv()? {
|
2019-02-10 21:35:44 +00:00
|
|
|
Event::Input(key) => match key {
|
2023-12-15 03:11:48 +00:00
|
|
|
Key::Up | Key::Char('k') => app.on_up(),
|
|
|
|
Key::Down | Key::Char('j') => app.on_down(),
|
|
|
|
Key::Left | Key::Char('h') => app.on_left(),
|
|
|
|
Key::Right | Key::Char('l') => app.on_right(),
|
2021-11-01 21:27:46 +00:00
|
|
|
Key::Char(c) => app.on_key(c),
|
2019-02-10 21:35:44 +00:00
|
|
|
_ => {}
|
|
|
|
},
|
2021-11-01 21:27:46 +00:00
|
|
|
Event::Tick => app.on_tick(),
|
2019-02-10 21:35:44 +00:00
|
|
|
}
|
|
|
|
if app.should_quit {
|
2021-11-01 21:27:46 +00:00
|
|
|
return Ok(());
|
2019-02-10 21:35:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2019-02-10 21:35:44 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
enum Event {
|
|
|
|
Input(Key),
|
|
|
|
Tick,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn events(tick_rate: Duration) -> mpsc::Receiver<Event> {
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let keys_tx = tx.clone();
|
|
|
|
thread::spawn(move || {
|
|
|
|
let stdin = io::stdin();
|
2021-12-23 17:46:25 +00:00
|
|
|
for key in stdin.keys().flatten() {
|
|
|
|
if let Err(err) = keys_tx.send(Event::Input(key)) {
|
2023-05-22 03:46:02 +00:00
|
|
|
eprintln!("{err}");
|
2021-12-23 17:46:25 +00:00
|
|
|
return;
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
thread::spawn(move || loop {
|
|
|
|
if let Err(err) = tx.send(Event::Tick) {
|
2023-05-22 03:46:02 +00:00
|
|
|
eprintln!("{err}");
|
2021-11-01 21:27:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
thread::sleep(tick_rate);
|
|
|
|
});
|
|
|
|
rx
|
2019-02-10 21:35:44 +00:00
|
|
|
}
|