panorama/src/ui/mod.rs

78 lines
1.8 KiB
Rust
Raw Normal View History

2021-02-12 13:21:00 +00:00
mod table;
2021-02-12 08:12:43 +00:00
use std::io::Write;
2021-02-12 08:54:19 +00:00
use std::time::Duration;
2021-02-12 08:12:43 +00:00
use anyhow::Result;
2021-02-12 08:54:19 +00:00
use chrono::Local;
2021-02-12 08:12:43 +00:00
use crossterm::{
2021-02-12 08:54:19 +00:00
cursor,
event::{self, Event, KeyCode, KeyEvent},
2021-02-12 13:21:00 +00:00
style::{self, Color},
terminal::{self, ClearType},
2021-02-12 08:12:43 +00:00
};
2021-02-12 08:54:19 +00:00
use tokio::time;
2021-02-12 08:12:43 +00:00
2021-02-12 08:54:19 +00:00
use crate::ExitSender;
2021-02-12 08:12:43 +00:00
2021-02-12 13:21:00 +00:00
use self::table::Table;
const FRAME: Duration = Duration::from_millis(16);
/// X Y W H
#[derive(Copy, Clone)]
pub struct Rect(u16, u16, u16, u16);
2021-02-12 08:12:43 +00:00
2021-02-12 08:54:19 +00:00
pub async fn run_ui(mut w: impl Write, exit: ExitSender) -> Result<()> {
execute!(w, cursor::Hide, terminal::EnterAlternateScreen)?;
terminal::enable_raw_mode()?;
2021-02-12 08:12:43 +00:00
2021-02-12 13:21:00 +00:00
let mut table = Table::default();
table.push_row(vec!["ur mom Lol!".to_owned()]);
table.push_row(vec!["hek".to_owned()]);
2021-02-12 08:54:19 +00:00
loop {
2021-02-12 13:21:00 +00:00
queue!(
w,
style::SetBackgroundColor(Color::Reset),
style::SetForegroundColor(Color::Reset),
// terminal::Clear(ClearType::All),
cursor::MoveTo(0, 0),
)?;
2021-02-12 08:12:43 +00:00
2021-02-12 08:54:19 +00:00
let now = Local::now();
2021-02-12 12:32:17 +00:00
println!("time {}", now);
2021-02-12 08:12:43 +00:00
2021-02-12 13:21:00 +00:00
let (term_width, term_height) = terminal::size()?;
let bounds = Rect(5, 5, term_width - 10, term_height - 10);
table.draw(&mut w, bounds)?;
w.flush()?;
2021-02-12 08:54:19 +00:00
// approx 60fps
time::sleep(FRAME).await;
2021-02-12 08:12:43 +00:00
2021-02-12 08:54:19 +00:00
if event::poll(FRAME)? {
2021-02-12 13:21:00 +00:00
let event = event::read()?;
table.update(&event);
match event {
2021-02-12 08:12:43 +00:00
Event::Key(KeyEvent {
code: KeyCode::Char('q'),
..
2021-02-12 08:54:19 +00:00
}) => break,
2021-02-12 08:12:43 +00:00
_ => {}
}
}
}
2021-02-12 08:54:19 +00:00
execute!(
w,
style::ResetColor,
cursor::Show,
terminal::LeaveAlternateScreen
)?;
terminal::disable_raw_mode()?;
2021-02-12 08:12:43 +00:00
2021-02-12 08:54:19 +00:00
exit.send(()).expect("fake news?");
Ok(())
2021-02-12 08:12:43 +00:00
}