panorama/src/ui/mod.rs

86 lines
1.9 KiB
Rust
Raw Normal View History

2021-02-14 12:27:13 +00:00
//! UI
2021-02-12 13:21:00 +00:00
mod table;
2021-02-16 10:45:41 +00:00
use std::fmt::Debug;
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},
2021-02-15 10:36:06 +00:00
terminal,
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(20);
2021-02-12 13:21:00 +00:00
/// X Y W H
#[derive(Copy, Clone)]
pub struct Rect(u16, u16, u16, u16);
2021-02-12 08:12:43 +00:00
2021-02-14 12:27:13 +00:00
/// UI entrypoint.
2021-02-20 01:44:04 +00:00
#[instrument(skip(w, exit))]
2021-02-16 10:45:41 +00:00
pub async fn run_ui(mut w: impl Write + Debug, exit: ExitSender) -> Result<()> {
2021-02-12 08:54:19 +00:00
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
// check to see if there's even an event this frame. otherwise, just keep going
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);
2021-02-15 10:36:06 +00:00
if let Event::Key(KeyEvent {
code: KeyCode::Char('q'),
..
}) = event
{
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-14 13:20:35 +00:00
exit.send(()).await?;
debug!("sent exit");
2021-02-12 08:54:19 +00:00
Ok(())
2021-02-12 08:12:43 +00:00
}