aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 41561b8d3595f393f0ba57b90106cf9570913eb6 (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
169
170
171
172
173
174
175
176
177
#![allow(clippy::collapsible_if)]

pub mod blocking;

pub mod color;
pub mod error;
pub use error::{Error, Result};
mod key;
pub use key::Key;
mod raw_guard;
pub use raw_guard::RawGuard;

#[cfg(feature = "async")]
mod output;
#[cfg(feature = "async")]
pub use output::{Output, ScreenGuard};
#[cfg(feature = "async")]
mod input;
#[cfg(feature = "async")]
pub use input::Input;

const INIT: &[u8] = b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h";
const DEINIT: &[u8] = b"\x1b[?47l\x1b8\x1b[?25h";

mod private {
    pub trait TextmodeImpl {
        fn cur(&self) -> &vt100::Parser;
        fn cur_mut(&mut self) -> &mut vt100::Parser;
        fn next(&self) -> &vt100::Parser;
        fn next_mut(&mut self) -> &mut vt100::Parser;

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

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

pub trait Textmode: private::TextmodeImpl {
    fn screen(&self) -> &vt100::Screen {
        self.next().screen()
    }

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

    fn set_size(&mut self, rows: u16, cols: u16) {
        self.cur_mut().set_size(rows, cols);
        self.next_mut().set_size(rows, cols);
    }

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

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

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

    fn clear_line(&mut self) {
        self.write(b"\x1b[K");
    }

    fn reset_attributes(&mut self) {
        self.write(b"\x1b[m");
    }

    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 if i < 16 {
                    self.write(b"\x1b[");
                    self.write_u8(82 + 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");
            }
        }
    }

    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 if i < 16 {
                    self.write(b"\x1b[");
                    self.write_u8(92 + 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 set_bold(&mut self, bold: bool) {
        if bold {
            self.write(b"\x1b[1m");
        } else {
            self.write(b"\x1b[22m");
        }
    }

    fn set_italic(&mut self, italic: bool) {
        if italic {
            self.write(b"\x1b[3m");
        } else {
            self.write(b"\x1b[23m");
        }
    }

    fn set_underline(&mut self, underline: bool) {
        if underline {
            self.write(b"\x1b[4m");
        } else {
            self.write(b"\x1b[24m");
        }
    }

    fn set_inverse(&mut self, inverse: bool) {
        if inverse {
            self.write(b"\x1b[7m");
        } else {
            self.write(b"\x1b[27m");
        }
    }
}