aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 39c201b5dcbd8a12a47beb353dd9b50c9cf4f1ad (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
use std::io::Write as _;

pub mod color;

pub struct Textmode {
    cur: vt100::Parser,
    next: vt100::Parser,
}

impl Textmode {
    pub fn new() -> std::io::Result<Self> {
        let (rows, cols) = match terminal_size::terminal_size() {
            Some((terminal_size::Width(w), terminal_size::Height(h))) => {
                (h, w)
            }
            _ => (24, 80),
        };
        let cur = vt100::Parser::new(rows, cols, 0);
        let next = vt100::Parser::new(rows, cols, 0);

        let self_ = Self { cur, next };
        self_.write_stdout(b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h")?;
        Ok(self_)
    }

    pub fn cursor_position(&self) -> (u16, u16) {
        self.next.screen().cursor_position()
    }

    pub fn write(&mut self, buf: &[u8]) {
        self.next.process(buf);
    }

    pub fn refresh(&mut self) -> std::io::Result<()> {
        let diff = self.next.screen().contents_diff(self.cur.screen());
        self.write_stdout(&diff)?;
        self.cur.process(&diff);
        Ok(())
    }

    pub fn set_size(&mut self, rows: u16, cols: u16) {
        self.cur.set_size(rows, cols);
        self.next.set_size(rows, cols);
    }

    pub fn write_str(&mut self, text: &str) {
        self.write(text.as_bytes());
    }

    pub fn move_to(&mut self, row: u16, col: u16) {
        self.write(b"\x1b[");
        self.write_u16(row);
        self.write(b";");
        self.write_u16(col);
        self.write(b"H");
    }

    pub fn clear(&mut self) {
        self.write(b"\x1b[2J");
    }

    pub fn set_fgcolor(&mut self, color: vt100::Color) {
        match color {
            vt100::Color::Default => {
                self.write(b"\x1b[39m");
            }
            vt100::Color::Idx(i) => {
                if i < 8 {
                    self.write(b"\x1b[");
                    self.write_u8(30 + i);
                    self.write(b"m");
                } else {
                    self.write(b"\x1b[38;5;");
                    self.write_u8(i);
                    self.write(b"m");
                }
            }
            vt100::Color::Rgb(r, g, b) => {
                self.write(b"\x1b[38;2;");
                self.write_u8(r);
                self.write(b";");
                self.write_u8(g);
                self.write(b";");
                self.write_u8(b);
                self.write(b"m");
            }
        }
    }

    pub fn set_bgcolor(&mut self, color: vt100::Color) {
        match color {
            vt100::Color::Default => {
                self.write(b"\x1b[49m");
            }
            vt100::Color::Idx(i) => {
                if i < 8 {
                    self.write(b"\x1b[");
                    self.write_u8(40 + i);
                    self.write(b"m");
                } else {
                    self.write(b"\x1b[48;5;");
                    self.write_u8(i);
                    self.write(b"m");
                }
            }
            vt100::Color::Rgb(r, g, b) => {
                self.write(b"\x1b[48;2;");
                self.write_u8(r);
                self.write(b";");
                self.write_u8(g);
                self.write(b";");
                self.write_u8(b);
                self.write(b"m");
            }
        }
    }

    fn write_u16(&mut self, i: u16) {
        // unwrap is fine because vt100::Parser::write can never fail
        itoa::write(&mut self.next, i).unwrap();
    }

    fn write_u8(&mut self, i: u8) {
        // unwrap is fine because vt100::Parser::write can never fail
        itoa::write(&mut self.next, i).unwrap();
    }

    fn write_stdout(&self, buf: &[u8]) -> std::io::Result<()> {
        let mut stdout = std::io::stdout();
        stdout.write_all(buf)?;
        stdout.flush()?;
        Ok(())
    }
}

impl Drop for Textmode {
    fn drop(&mut self) {
        let _ = self.write_stdout(b"\x1b[?47l\x1b8\x1b[?25h");
    }
}