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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/// Exports the `Term` type which is a high-level API for the Grid
use std::sync::Arc;
use ansi::{self, Attr, DebugHandler};
use grid::{self, Grid, CellFlags};
use tty;
use ::Rgb;
/// tomorrow night bright
///
/// because contrast
pub static COLORS: &'static [Rgb] = &[
Rgb {r: 0x00, g: 0x00, b: 0x00}, // Black
Rgb {r: 0xd5, g: 0x4e, b: 0x53}, // Red
Rgb {r: 0xb9, g: 0xca, b: 0x4a}, // Green
Rgb {r: 0xe6, g: 0xc5, b: 0x47}, // Yellow
Rgb {r: 0x7a, g: 0xa6, b: 0xda}, // Blue
Rgb {r: 0xc3, g: 0x97, b: 0xd8}, // Magenta
Rgb {r: 0x70, g: 0xc0, b: 0xba}, // Cyan
Rgb {r: 0x42, g: 0x42, b: 0x42}, // White
Rgb {r: 0x66, g: 0x66, b: 0x66}, // Bright black
Rgb {r: 0xff, g: 0x33, b: 0x34}, // Bright red
Rgb {r: 0x9e, g: 0xc4, b: 0x00}, // Bright green
Rgb {r: 0xe7, g: 0xc5, b: 0x47}, // Bright yellow
Rgb {r: 0x7a, g: 0xa6, b: 0xda}, // Bright blue
Rgb {r: 0xb7, g: 0x7e, b: 0xe0}, // Bright magenta
Rgb {r: 0x54, g: 0xce, b: 0xd6}, // Bright cyan
Rgb {r: 0x2a, g: 0x2a, b: 0x2a}, // Bright white
];
pub const CURSOR_SHAPE: char = '█';
pub const DEFAULT_FG: Rgb = Rgb { r: 0xea, g: 0xea, b: 0xea};
pub const DEFAULT_BG: Rgb = Rgb { r: 0, g: 0, b: 0};
pub const TAB_SPACES: usize = 8;
/// State for cursor
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Cursor {
pub x: u16,
pub y: u16,
}
impl Default for Cursor {
fn default() -> Cursor {
Cursor { x: 0, y: 0 }
}
}
impl Cursor {
pub fn goto(&mut self, x: u16, y: u16) {
self.x = x;
self.y = y;
}
pub fn advance(&mut self, rows: i64, cols: i64) {
self.x = (self.x as i64 + cols) as u16;
self.y = (self.y as i64 + rows) as u16;
}
}
struct Mover<'a> {
cursor: &'a mut Cursor,
}
pub struct Term {
/// The grid
grid: Grid,
/// Alternate grid
alt_grid: Grid,
/// Alt is active
alt: bool,
/// Reference to the underlying tty
tty: tty::Tty,
/// The cursor
cursor: Cursor,
/// Alt cursor
alt_cursor: Cursor,
/// Active foreground color
fg: Rgb,
/// Active background color
bg: Rgb,
/// Tabstops
tabs: Vec<bool>,
/// Cell attributes
attr: grid::CellFlags,
}
impl Term {
pub fn new(tty: tty::Tty, grid: Grid) -> Term {
let mut tabs = (0..grid.num_cols()).map(|i| i % TAB_SPACES == 0)
.collect::<Vec<bool>>();
tabs[0] = false;
let alt = grid.clone();
Term {
grid: grid,
alt_grid: alt,
alt: false,
cursor: Cursor::default(),
alt_cursor: Cursor::default(),
fg: DEFAULT_FG,
bg: DEFAULT_BG,
tty: tty,
tabs: tabs,
attr: CellFlags::empty(),
}
}
pub fn grid(&self) -> &Grid {
&self.grid
}
pub fn swap_alt(&mut self) {
self.alt = !self.alt;
::std::mem::swap(&mut self.grid, &mut self.alt_grid);
::std::mem::swap(&mut self.cursor, &mut self.alt_cursor);
if self.alt {
self.grid.clear();
}
}
pub fn resize(&mut self) {
unimplemented!();
}
#[inline]
pub fn cursor_x(&self) -> u16 {
self.cursor.x
}
#[inline]
pub fn cursor_y(&self) -> u16 {
self.cursor.y
}
#[inline]
pub fn cursor(&self) -> Cursor {
self.cursor
}
/// Set character in current cursor position
fn set_char(&mut self, c: char) {
if self.cursor.x == self.grid.num_cols() as u16 {
self.cursor.y += 1;
self.cursor.x = 0;
}
let cell = &mut self.grid[self.cursor];
cell.c = c;
cell.fg = self.fg;
cell.bg = self.bg;
cell.flags = self.attr;
}
/// Advance to next line
fn newline_c(&mut self, count: u16) {
// TODO handle scroll
self.cursor.x = 0;
self.cursor.y += 1;
}
}
impl ansi::Handler for Term {
/// A character to be displayed
#[inline]
fn input(&mut self, c: char) {
self.set_char(c);
self.cursor.x += 1;
}
fn goto(&mut self, x: i64, y: i64) {
println!("goto: x={}, y={}", x, y);
self.cursor.goto(x as u16, y as u16);
}
fn goto_row(&mut self, y: i64) {
println!("goto_row: {}", y);
let x = self.cursor_x();
self.cursor.goto(x, y as u16);
}
fn goto_col(&mut self, x: i64) {
println!("goto_col: {}", x);
let y = self.cursor_y();
self.cursor.goto(x as u16, y);
}
fn insert_blank(&mut self, num: i64) { println!("insert_blank: {}", num); }
fn move_up(&mut self, rows: i64) {
println!("move_up: {}", rows);
self.cursor.advance(-rows, 0);
}
fn move_down(&mut self, rows: i64) {
println!("move_down: {}", rows);
self.cursor.advance(rows, 0);
}
fn move_forward(&mut self, cols: i64) {
println!("move_forward: {}", cols);
self.cursor.advance(0, cols);
}
fn move_backward(&mut self, spaces: i64) {
println!("move_backward: {}", spaces);
self.cursor.advance(0, -spaces);
}
fn identify_terminal(&mut self) { println!("identify_terminal"); }
fn move_down_and_cr(&mut self, rows: i64) { println!("move_down_and_cr: {}", rows); }
fn move_up_and_cr(&mut self, rows: i64) { println!("move_up_and_cr: {}", rows); }
fn put_tab(&mut self, mut count: i64) {
println!("put_tab: {}", count);
let mut x = self.cursor_x();
while x < self.grid.num_cols() as u16 && count != 0 {
count -= 1;
loop {
if x == self.grid.num_cols() as u16 || self.tabs[x as usize] {
break;
}
x += 1;
}
}
self.cursor.x = x;
}
/// Backspace `count` characters
#[inline]
fn backspace(&mut self, count: i64) {
self.cursor.x -= 1;
self.set_char(' ');
}
/// Carriage return
#[inline]
fn carriage_return(&mut self) {
self.cursor.x = 0;
}
/// Linefeed
#[inline]
fn linefeed(&mut self) {
println!("linefeed");
// TODO handle scroll? not clear what parts of this the pty handle
if self.cursor_y() + 1 == self.grid.num_rows() as u16 {
self.grid.feed();
self.clear_line(ansi::LineClearMode::Right);
} else {
self.cursor.y += 1;
}
}
/// Set current position as a tabstop
fn bell(&mut self) { println!("bell"); }
fn substitute(&mut self) { println!("substitute"); }
fn newline(&mut self) { println!("newline"); }
fn set_horizontal_tabstop(&mut self) { println!("set_horizontal_tabstop"); }
fn scroll_up(&mut self, rows: i64) { println!("scroll_up: {}", rows); }
fn scroll_down(&mut self, rows: i64) { println!("scroll_down: {}", rows); }
fn insert_blank_lines(&mut self, count: i64) { println!("insert_blank_lines: {}", count); }
fn delete_lines(&mut self, count: i64) { println!("delete_lines: {}", count); }
fn erase_chars(&mut self, count: i64) { println!("erase_chars: {}", count); }
fn delete_chars(&mut self, count: i64) { println!("delete_chars: {}", count); }
fn move_backward_tabs(&mut self, count: i64) { println!("move_backward_tabs: {}", count); }
fn move_forward_tabs(&mut self, count: i64) { println!("move_forward_tabs: {}", count); }
fn save_cursor_position(&mut self) { println!("save_cursor_position"); }
fn restore_cursor_position(&mut self) { println!("restore_cursor_position"); }
fn clear_line(&mut self, mode: ansi::LineClearMode) {
println!("clear_line: {:?}", mode);
match mode {
ansi::LineClearMode::Right => {
let cols = self.grid.num_cols();
let row = &mut self.grid[self.cursor.y as usize];
let start = self.cursor.x as usize;
for col in start..cols {
row[col].c = ' ';
}
},
_ => (),
}
}
fn clear_screen(&mut self, mode: ansi::ClearMode) {
println!("clear_screen: {:?}", mode);
match mode {
ansi::ClearMode::Below => {
let start = self.cursor_y() as usize;
let end = self.grid.num_rows();
for i in start..end {
let row = &mut self.grid[i];
for cell in row.iter_mut() {
cell.c = ' ';
}
}
},
ansi::ClearMode::All => {
self.grid.clear();
},
_ => {
panic!("ansi::ClearMode::Above not implemented");
}
}
}
fn clear_tabs(&mut self, mode: ansi::TabulationClearMode) { println!("clear_tabs: {:?}", mode); }
fn reset_state(&mut self) { println!("reset_state"); }
fn reverse_index(&mut self) {
println!("reverse_index");
// if cursor is at the top
if self.cursor.y == 0 {
self.grid.unfeed();
} else {
// can't wait for nonlexical lifetimes.. omg borrowck
let x = self.cursor.x;
let y = self.cursor.y;
self.cursor.goto(x, y - 1);
}
}
/// set a terminal attribute
fn terminal_attribute(&mut self, attr: Attr) {
match attr {
Attr::DefaultForeground => {
self.fg = DEFAULT_FG;
},
Attr::DefaultBackground => {
self.bg = DEFAULT_BG;
},
Attr::Foreground(named_color) => {
self.fg = COLORS[named_color as usize];
},
Attr::Background(named_color) => {
self.bg = COLORS[named_color as usize];
},
Attr::ForegroundSpec(rgb) => {
self.fg = rgb;
},
Attr::BackgroundSpec(rgb) => {
self.bg = rgb;
},
Attr::Reset => {
self.fg = DEFAULT_FG;
self.bg = DEFAULT_BG;
self.attr = CellFlags::empty();
},
Attr::Reverse => {
self.attr.insert(grid::INVERSE);
},
Attr::CancelReverse => {
self.attr.remove(grid::INVERSE);
},
_ => {
println!("Term got unhandled attr: {:?}", attr);
}
}
}
fn set_mode(&mut self, mode: ansi::Mode) {
println!("set_mode: {:?}", mode);
match mode {
ansi::Mode::SwapScreenAndSetRestoreCursor => {
self.swap_alt();
}
}
}
fn unset_mode(&mut self, mode: ansi::Mode) {
println!("unset_mode: {:?}", mode);
match mode {
ansi::Mode::SwapScreenAndSetRestoreCursor => {
self.swap_alt();
}
}
}
}
|