-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrenderer.rs
More file actions
290 lines (255 loc) · 7.6 KB
/
Copy pathrenderer.rs
File metadata and controls
290 lines (255 loc) · 7.6 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use crate::ansi::Color;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::VecDeque;
pub struct Renderer {
pub name: String,
pub bold: bool,
pub italic: bool,
pub underline: bool,
pub dim: bool,
pub foreground: Color,
pub background: Color,
current_row: usize,
rows: VecDeque<Row>,
styles: Styles,
}
#[derive(Debug, Default)]
pub struct Styles {
known: RefCell<HashMap<(Color, bool), String>>,
}
const ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz";
const BASE: usize = ALPHABET.len();
impl Styles {
fn with<T>(&self, color: Color, bold: bool, mut func: impl FnMut(&str) -> T) -> T {
let mut known = self.known.borrow_mut();
let mut next_idx = known.len();
let name = known.entry((color, bold)).or_insert_with(|| {
let mut name = String::new();
loop {
name.push(ALPHABET[next_idx % BASE] as char);
next_idx /= BASE;
if next_idx == 0 {
break;
}
}
name
});
func(&name)
}
}
struct Row {
cells: Vec<Cell>,
position: usize,
}
impl Row {
fn new() -> Self {
Row {
cells: Vec::new(),
position: 0,
}
}
fn erase(&mut self) {
for c in &mut self.cells {
c.text = ' ';
}
}
fn seek(&mut self, position: usize) {
self.position = position;
}
// FIXME: This misbehaves if the position is off in space
fn print(&mut self, cell: Cell) {
if let Some(current) = self.cells.get_mut(self.position) {
*current = cell;
} else {
self.cells.push(cell);
}
self.position += 1;
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Cell {
text: char, // TODO: totally wrong, graphmeme clusters
foreground: Color,
background: Color,
bold: bool,
italic: bool,
underline: bool,
dim: bool,
}
impl Default for Cell {
fn default() -> Self {
Self {
text: ' ',
foreground: Color::bright_white(),
background: Color::black(),
bold: false,
italic: false,
underline: false,
dim: false,
}
}
}
impl Renderer {
pub fn new(name: String) -> Self {
Self {
name,
bold: false,
italic: false,
underline: false,
dim: false,
foreground: Color::bright_white(),
background: Color::black(),
current_row: 0,
rows: vec![Row::new()].into(),
styles: Styles::default(),
}
}
pub fn print(&mut self, c: char) {
let cell = Cell {
text: c,
background: self.background,
foreground: self.foreground,
bold: self.bold,
italic: self.italic,
underline: self.underline,
dim: self.dim,
};
self.current_row().print(cell);
}
fn current_row(&mut self) -> &mut Row {
&mut self.rows[self.current_row]
}
pub fn put_tab(&mut self) {
self.print(' ');
self.print(' ');
self.print(' ');
self.print(' ');
}
pub fn backspace(&mut self) {
self.current_row().position = self.current_row().position.saturating_sub(1);
}
pub fn carriage_return(&mut self) {
self.current_row().seek(0);
}
pub fn linefeed(&mut self) {
self.current_row += 1;
if self.current_row == self.rows.len() {
self.rows.push_back(Row::new());
}
}
pub fn erase_in_display(&mut self, mode: Option<u16>) {
// Ignore attempts to clear the whole screen
if mode == Some(2) || mode == Some(3) {
return;
}
log::warn!("Unimplemented erase_in_display {:?}", mode);
}
pub fn erase_in_line(&mut self, mode: Option<u16>) {
let row = self.current_row();
match mode.unwrap_or(0) {
0 => {
row.cells.truncate(row.position + 1);
}
1 => {
for cell in &mut row.cells[..row.position] {
*cell = Cell::default();
}
}
2 => {
self.current_row().erase();
}
_ => {}
}
}
pub fn handle_move(&mut self, row: u16, col: u16) {
self.current_row = row as usize;
while self.current_row >= self.rows.len() {
self.rows.push_back(Row::new());
}
self.set_column(col);
}
pub fn move_up_by(&mut self, cells: u16) {
self.current_row = self.current_row.saturating_sub(cells as usize);
}
pub fn move_down_by(&mut self, cells: u16) {
self.current_row += cells as usize;
while self.current_row >= self.rows.len() {
self.rows.push_back(Row::new());
}
}
pub fn move_right_by(&mut self, cells: u16) {
let pos = (self.current_row().position as u16).saturating_add(cells);
self.set_column(pos);
}
pub fn move_left_by(&mut self, cells: u16) {
self.current_row().position = self.current_row().position.saturating_sub(cells as usize);
}
pub fn set_column(&mut self, cells: u16) {
let row = self.current_row();
row.position = cells.saturating_sub(1) as usize;
while row.cells.len() < row.position {
row.cells.push(Cell::default());
}
let _ = &row.cells[..row.position];
}
pub fn pop_completed_row(&mut self) -> Option<Vec<u8>> {
if self.rows.len() > 64 {
self.remove_oldest_row()
} else {
None
}
}
pub fn remove_oldest_row(&mut self) -> Option<Vec<u8>> {
use std::io::Write;
let mut out = Vec::new();
let mut prev = Cell {
text: ' ',
foreground: Color::bright_white(),
background: Color::black(),
bold: false,
italic: false,
underline: false,
dim: false,
};
out.extend(b"<span>");
let row = self.rows.pop_front()?;
self.current_row = self.current_row.saturating_sub(1);
for cell in &row.cells {
// Terminal applications will often reset the style right after some formatted text
// then write some whitespace then set it to something again.
// So we only apply style changes if the cell is nonempty. This is a ~50% savings
// in emitted HTML.
if cell.text != ' ' {
if cell.bold != prev.bold || cell.foreground != prev.foreground {
self.styles
.with(cell.foreground, cell.bold, |class| {
write!(out, "</span><span class='{}'>", class)
})
.unwrap();
}
prev = cell.clone();
}
match cell.text {
'<' => out.extend(b"<"),
'>' => out.extend(b">"),
c => write!(out, "{}", c).unwrap(),
}
}
out.extend(&[b'\n']);
out.extend(b"</span>");
Some(out)
}
pub fn emit_css<W: std::io::Write>(&self, mut out: W) -> std::io::Result<()> {
for ((color, bold), name) in self.styles.known.borrow().iter() {
write!(
out,
".{}{{color:{};font-weight:{}}}\n",
name,
color.as_str(),
if *bold { "bold" } else { "normal" }
)?;
}
Ok(())
}
}