panorama/src/ui.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

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},
style, 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 12:32:17 +00:00
const FRAME: Duration = Duration::from_millis(33);
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 08:54:19 +00:00
loop {
execute!(w, 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 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 08:12:43 +00:00
match event::read()? {
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
}