aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/ttyplay/display.rs
blob: c1871cd9f19f39348f210740d1df72b9565e8d97 (plain) (blame)
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
use textmode::Textmode as _;

pub struct Display {
    screen: vt100::Screen,
    current_frame: usize,
    total_frames: usize,
    done_loading: bool,
    paused: bool,
    show_ui: bool,
    show_help: bool,
    active_search: Option<String>,
}

impl Display {
    pub fn new() -> Self {
        Self {
            screen: vt100::Parser::default().screen().clone(),
            current_frame: 0,
            total_frames: 0,
            done_loading: false,
            paused: false,
            show_ui: true,
            show_help: false,
            active_search: None,
        }
    }

    pub fn screen(&mut self, screen: vt100::Screen) {
        self.screen = screen;
    }

    pub fn current_frame(&mut self, idx: usize) {
        self.current_frame = idx;
    }

    pub fn total_frames(&mut self, n: usize) {
        self.total_frames = n;
    }

    pub fn done_loading(&mut self) {
        self.done_loading = true;
    }

    pub fn paused(&mut self, paused: bool) {
        self.paused = paused;
    }

    pub fn toggle_ui(&mut self) {
        self.show_ui = !self.show_ui;
    }

    pub fn toggle_help(&mut self) {
        self.show_help = !self.show_help;
    }

    pub fn active_search(&mut self, s: String) {
        self.active_search = Some(s);
    }

    pub fn clear_search(&mut self) {
        self.active_search = None;
    }

    pub async fn render(
        &self,
        output: &mut textmode::Output,
    ) -> anyhow::Result<()> {
        let pos = output.screen().cursor_position();

        self.render_screen(output);

        if self.paused && self.show_ui {
            self.render_frame_count(output);
            self.render_pause_symbol(output);

            if self.show_help {
                self.render_help(output);
            }
        }

        self.render_search(output);

        output.reset_attributes();
        output.move_to(pos.0, pos.1);
        output.refresh().await?;

        Ok(())
    }

    fn render_screen(&self, output: &mut textmode::Output) {
        output.clear();
        output.move_to(0, 0);
        output.write(&self.screen.contents_formatted());
    }

    fn render_frame_count(&self, output: &mut textmode::Output) {
        output.move_to(0, 0);
        output.reset_attributes();
        output.set_fgcolor(textmode::color::BLACK);
        if self.done_loading {
            output.set_bgcolor(textmode::color::CYAN);
        } else {
            output.set_bgcolor(textmode::color::RED);
        }
        output.write_str(&format!(
            " {}/{} ",
            self.current_frame + 1,
            self.total_frames
        ));
    }

    #[allow(clippy::unused_self)]
    fn render_pause_symbol(&self, output: &mut textmode::Output) {
        let size = output.screen().size();
        output.move_to(0, size.1 - 1);
        output.reset_attributes();
        output.set_fgcolor(textmode::color::BLACK);
        output.set_bgcolor(textmode::color::RED);
        output.write_str("\u{23f8}");
    }

    #[allow(clippy::unused_self)]
    fn render_help(&self, output: &mut textmode::Output) {
        let size = output.screen().size();
        output.reset_attributes();
        output.set_fgcolor(textmode::color::BLACK);
        output.set_bgcolor(textmode::color::CYAN);

        output.move_to(size.0 - 13, size.1 - 23);
        output.write_str("         keys          ");
        output.move_to(size.0 - 12, size.1 - 23);
        output.write_str(" q:     quit           ");
        output.move_to(size.0 - 11, size.1 - 23);
        output.write_str(" space: pause/unpause  ");
        output.move_to(size.0 - 10, size.1 - 23);
        output.write_str(" tab:   hide/show ui   ");
        output.move_to(size.0 - 9, size.1 - 23);
        output.write_str(" h/p:   previous frame ");
        output.move_to(size.0 - 8, size.1 - 23);
        output.write_str(" l/n:   next frame     ");
        output.move_to(size.0 - 7, size.1 - 23);
        output.write_str(" g/0:   first frame    ");
        output.move_to(size.0 - 6, size.1 - 23);
        output.write_str(" G/$:   last frame     ");
        output.move_to(size.0 - 5, size.1 - 23);
        output.write_str(" +:     increase speed ");
        output.move_to(size.0 - 4, size.1 - 23);
        output.write_str(" -:     decrease speed ");
        output.move_to(size.0 - 3, size.1 - 23);
        output.write_str(" =:     normal speed   ");
        output.move_to(size.0 - 2, size.1 - 23);
        output.write_str(" ?:     hide/show help ");
    }

    fn render_search(&self, output: &mut textmode::Output) {
        if let Some(search) = &self.active_search {
            let size = output.screen().size();
            output.reset_attributes();
            output.set_fgcolor(textmode::color::BLACK);
            output.set_bgcolor(textmode::color::CYAN);

            output.move_to(size.0 - 1, 0);
            output.write_str("/");
            output.write_str(search);
            output.write_str(&" ".repeat(size.1 as usize - search.len() - 1));
        }
    }
}