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

pub struct Display {
    current_frame: usize,
    total_frames: usize,
    done_loading: bool,
    paused: bool,
    show_ui: bool,
}

impl Display {
    pub fn new() -> Self {
        Self {
            current_frame: 0,
            total_frames: 0,
            done_loading: false,
            paused: false,
            show_ui: true,
        }
    }

    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 async fn render(
        &self,
        screen: &vt100::Screen,
        output: &mut textmode::Output,
    ) -> anyhow::Result<()> {
        output.clear();
        output.move_to(0, 0);
        output.write(&screen.contents_formatted());
        if self.paused && self.show_ui {
            let pos = output.screen().cursor_position();

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

            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}");

            output.reset_attributes();
            output.move_to(pos.0, pos.1);
        }

        output.refresh().await?;

        Ok(())
    }
}