1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! UI

mod table;

use std::io::Write;
use std::time::Duration;

use anyhow::Result;
use chrono::Local;
use crossterm::{
    cursor,
    event::{self, Event, KeyCode, KeyEvent},
    style::{self, Color},
    terminal::{self, ClearType},
};
use tokio::time;

use crate::ExitSender;

use self::table::Table;

const FRAME: Duration = Duration::from_millis(20);

/// X Y W H
#[derive(Copy, Clone)]
pub struct Rect(u16, u16, u16, u16);

/// UI entrypoint.
pub async fn run_ui(mut w: impl Write, exit: ExitSender) -> Result<()> {
    execute!(w, cursor::Hide, terminal::EnterAlternateScreen)?;
    terminal::enable_raw_mode()?;

    let mut table = Table::default();
    table.push_row(vec!["ur mom Lol!".to_owned()]);
    table.push_row(vec!["hek".to_owned()]);

    loop {
        queue!(
            w,
            style::SetBackgroundColor(Color::Reset),
            style::SetForegroundColor(Color::Reset),
            // terminal::Clear(ClearType::All),
            cursor::MoveTo(0, 0),
        )?;

        let now = Local::now();
        println!("time {}", now);

        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()?;

        // approx 60fps
        time::sleep(FRAME).await;

        // check to see if there's even an event this frame. otherwise, just keep going
        if event::poll(FRAME)? {
            let event = event::read()?;
            table.update(&event);
            match event {
                Event::Key(KeyEvent {
                    code: KeyCode::Char('q'),
                    ..
                }) => break,
                _ => {}
            }
        }
    }

    execute!(
        w,
        style::ResetColor,
        cursor::Show,
        terminal::LeaveAlternateScreen
    )?;
    terminal::disable_raw_mode()?;

    exit.send(()).expect("fake news?");
    Ok(())
}