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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::{
    error::Error,
    time::{Duration, Instant},
};

#[allow(unused)] // in case mouse capture is enabled in the future
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use tui::{
    backend::{Backend, CrosstermBackend},
    Terminal,
};

use super::profiler::{LogBuffer, ProfilerExt, StateBuffer};

mod ui;

pub struct Dash<P: ProfilerExt + 'static> {
    profiler: &'static P,
    state_buffer: StateBuffer,
    tabs: TabsState<'static>,
    q_counter: u8,
    should_quit: bool,
    show_log: bool,
    log_buffer: LogBuffer,
    // Just so we don't allocate
    domain: Vec<f64>,
}

impl<P: ProfilerExt + 'static> Dash<P> {
    pub fn from_profiler(profiler: &'static P) -> Dash<P> {
        Dash {
            profiler,
            state_buffer: profiler.state_buffer(),
            log_buffer: profiler.log_buffer(),
            tabs: TabsState::new(vec![P::TITLE]),
            q_counter: 0,
            should_quit: false,
            show_log: true,
            domain: (0..P::NUM_AVERAGES).map(|i| i as f64).collect(),
        }
    }

    pub fn run(&mut self, tick_rate: Duration) -> Result<(), Box<dyn Error>> {
        // setup terminal
        enable_raw_mode()?;
        let mut stdout = std::io::stdout();
        execute!(stdout, EnterAlternateScreen /*, EnableMouseCapture */)?;
        let backend = CrosstermBackend::new(stdout);
        let mut terminal = Terminal::new(backend)?;

        // create app and run it
        let res = self.run_app(&mut terminal, tick_rate);

        // restore terminal
        disable_raw_mode()?;
        execute!(
            terminal.backend_mut(),
            LeaveAlternateScreen /*,
                                 DisableMouseCapture*/
        )?;
        terminal.show_cursor()?;

        if let Err(err) = res {
            println!("{:?}", err)
        }

        Ok(())
    }

    fn run_app<B: Backend>(
        &mut self,
        terminal: &mut Terminal<B>,
        tick_rate: Duration,
    ) -> std::io::Result<()> {
        let mut last_tick = Instant::now();
        loop {
            terminal.draw(|f| ui::draw(f, self))?;

            let timeout = tick_rate
                .checked_sub(last_tick.elapsed())
                .unwrap_or_else(|| Duration::from_secs(0));
            if crossterm::event::poll(timeout)? {
                if let Event::Key(key) = event::read()? {
                    match key.code {
                        KeyCode::Char(c) => self.on_key(c),
                        KeyCode::Left => self.on_left(),
                        KeyCode::Up => self.on_up(),
                        KeyCode::Right => self.on_right(),
                        KeyCode::Down => self.on_down(),
                        _ => {}
                    }
                }
            }
            if last_tick.elapsed() >= tick_rate {
                self.on_tick();
                last_tick = Instant::now();
            }
            if self.should_quit {
                return Ok(());
            }
        }
    }

    pub fn on_key(&mut self, key: char) {
        match key {
            // Quit key
            'q' => {
                // Increment q counter
                self.q_counter += 1;

                // If hit enough times, quit
                const NUM_Q_TO_QUIT: u8 = 2;
                if self.q_counter == NUM_Q_TO_QUIT {
                    self.should_quit = true
                }
            }
            // toggle logs
            'l' => {
                // Reset q counter on non-q key
                self.q_counter = 0;

                // If hit enough times, quit
                self.show_log = !self.show_log;
            }
            _ => {
                // Reset q counter on any other key
                self.q_counter = 0;

                // Do nothing for now
            }
        }
    }

    pub fn on_up(&mut self) {
        // do nothing for now
    }

    pub fn on_down(&mut self) {
        // do nothing for now
    }

    pub fn on_right(&mut self) {
        self.tabs.next();
    }

    pub fn on_left(&mut self) {
        self.tabs.previous();
    }

    pub fn on_tick(&mut self) {
        // Update state buffer
        self.profiler.update_buffer(&mut self.state_buffer);
        // Update log buffer
        self.profiler.update_logs(&mut self.log_buffer);
    }
}

pub struct TabsState<'a> {
    pub titles: Vec<&'a str>,
    pub index: usize,
}

impl<'a> TabsState<'a> {
    pub fn new(titles: Vec<&'a str>) -> TabsState {
        TabsState { titles, index: 0 }
    }
    pub fn next(&mut self) {
        self.index = (self.index + 1) % self.titles.len();
    }

    pub fn previous(&mut self) {
        if self.index > 0 {
            self.index -= 1;
        } else {
            self.index = self.titles.len() - 1;
        }
    }
}