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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/// Exports the `Term` type which is a high-level API for the Grid
use std::ops::Range;
use ansi::{self, Attr};
use grid::{self, Grid, CellFlags, ClearRegion};
use tty;
use ::Rgb;
/// coerce val to be between min and max
fn limit<T: PartialOrd>(val: T, min: T, max: T) -> T {
if val < min {
min
} else if val > max {
max
} else {
val
}
}
/// 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 mod mode {
bitflags! {
pub flags TermMode: u8 {
const SHOW_CURSOR = 0b00000001,
const APP_CURSOR = 0b00000010,
const ANY = 0b11111111,
const NONE = 0b00000000,
}
}
impl Default for TermMode {
fn default() -> TermMode {
SHOW_CURSOR
}
}
}
pub use self::mode::TermMode;
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;
}
}
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,
/// Mode flags
mode: TermMode,
/// Scroll region
scroll_region: Range<usize>,
/// Size
size_info: SizeInfo,
}
/// Terminal size info
#[derive(Debug)]
pub struct SizeInfo {
/// Terminal window width
pub width: f32,
/// Terminal window height
pub height: f32,
/// Width of individual cell
pub cell_width: f32,
/// Height of individual cell
pub cell_height: f32,
}
impl SizeInfo {
#[inline]
pub fn rows(&self) -> usize {
(self.height / self.cell_height) as usize
}
#[inline]
pub fn cols(&self) -> usize {
(self.width / self.cell_width) as usize
}
}
impl Term {
pub fn new(width: f32, height: f32, cell_width: f32, cell_height: f32) -> Term {
let size = SizeInfo {
width: width as f32,
height: height as f32,
cell_width: cell_width as f32,
cell_height: cell_height as f32,
};
let num_cols = size.cols();
let num_rows = size.rows();
println!("num_cols, num_rows = {}, {}", num_cols, num_rows);
let grid = Grid::new(num_rows, num_cols);
let tty = tty::new(num_rows as u8, num_cols as u8);
tty.resize(num_rows, num_cols, size.width as usize, size.height as usize);
let mut tabs = (0..grid.num_cols()).map(|i| i % TAB_SPACES == 0)
.collect::<Vec<bool>>();
tabs[0] = false;
let alt = grid.clone();
let scroll_region = 0..grid.num_rows();
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(),
mode: Default::default(),
scroll_region: scroll_region,
size_info: size
}
}
/// Resize terminal to new dimensions
pub fn resize(&mut self, width: f32, height: f32) {
let size = SizeInfo {
width: width,
height: height,
cell_width: self.size_info.cell_width,
cell_height: self.size_info.cell_height,
};
let old_cols = self.size_info.cols();
let old_rows = self.size_info.rows();
let num_cols = size.cols();
let num_rows = size.rows();
self.size_info = size;
if old_cols == num_cols && old_rows == num_rows {
return;
}
// Scroll up to keep cursor and as much context as possible in grid. This only runs when the
// rows decreases.
self.scroll_region = 0..self.grid.num_rows();
// XXX why is +1 required?
let row_diff = (self.cursor_y() as isize - num_rows as isize) + 1;
if row_diff > 0 {
self.scroll(row_diff);
self.cursor.advance(-row_diff as i64, 0);
}
println!("num_cols, num_rows = {}, {}", num_cols, num_rows);
// Resize grids to new size
self.grid.resize(num_rows, num_cols);
self.alt_grid.resize(num_rows, num_cols);
// Ensure cursor is in-bounds
self.cursor.y = limit(self.cursor.y, 0, num_rows as u16);
self.cursor.x = limit(self.cursor.x, 0, num_cols as u16);
// Recreate tabs list
self.tabs = (0..self.grid.num_cols()).map(|i| i % TAB_SPACES == 0)
.collect::<Vec<bool>>();
// Make sure bottom of terminal is clear
if row_diff > 0 {
self.grid.clear_region((self.cursor.y as usize)..);
self.alt_grid.clear_region((self.cursor.y as usize)..);
}
// Reset scrolling region to new size
self.scroll_region = 0..self.grid.num_rows();
// Inform tty of new dimensions
self.tty.resize(num_rows,
num_cols,
self.size_info.width as usize,
self.size_info.height as usize);
}
#[inline]
pub fn tty(&self) -> &tty::Tty {
&self.tty
}
#[inline]
pub fn size_info(&self) -> &SizeInfo {
&self.size_info
}
pub fn grid(&self) -> &Grid {
&self.grid
}
#[inline]
pub fn mode(&self) -> &TermMode {
&self.mode
}
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();
}
}
#[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 {
println!("wrapping");
self.cursor.y += 1;
self.cursor.x = 0;
}
if self.cursor.y == self.grid.num_rows() as u16 {
panic!("cursor fell off grid");
}
let cell = &mut self.grid[self.cursor];
cell.c = c;
cell.fg = self.fg;
cell.bg = self.bg;
cell.flags = self.attr;
}
/// Convenience function for scrolling
fn scroll(&mut self, count: isize) {
println!("[TERM] scrolling {} lines", count);
self.grid.scroll(self.scroll_region.clone(), count);
if count > 0 {
// Scrolled down, so need to clear from bottom
let start = self.scroll_region.end - (count as usize);
self.grid.clear_region(start..self.scroll_region.end);
} else {
// Scrolled up, clear from top
let end = self.scroll_region.start + ((-count) as usize);
self.grid.clear_region(self.scroll_region.start..end);
}
}
}
impl ansi::TermInfo for Term {
#[inline]
fn rows(&self) -> usize {
self.grid.num_rows()
}
#[inline]
fn cols(&self) -> usize {
self.grid.num_cols()
}
}
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) {
println!("backspace");
self.cursor.x -= 1;
}
/// Carriage return
#[inline]
fn carriage_return(&mut self) {
println!("carriage_return");
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.scroll_region.end as u16 {
self.scroll(1);
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);
self.scroll(-rows as isize);
}
fn scroll_down(&mut self, rows: i64) {
println!("scroll_down: {}", rows);
self.scroll(rows as isize);
}
fn insert_blank_lines(&mut self, count: i64) {
println!("insert_blank_lines: {}", count);
if self.scroll_region.contains(self.cursor_y() as usize) {
self.scroll(-count as isize);
}
}
fn delete_lines(&mut self, count: i64) {
if self.scroll_region.contains(self.cursor_y() as usize) {
self.scroll(count as isize);
}
}
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 row = &mut self.grid[self.cursor.y as usize];
let start = self.cursor.x as usize;
for cell in row[start..].iter_mut() {
cell.reset();
}
},
_ => (),
}
}
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.scroll(-1);
} 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),
Attr::Bold => self.attr.insert(grid::BOLD),
Attr::CancelBoldDim => self.attr.remove(grid::BOLD),
Attr::Italic => self.attr.insert(grid::ITALIC),
Attr::CancelItalic => self.attr.remove(grid::ITALIC),
Attr::Underscore => self.attr.insert(grid::UNDERLINE),
Attr::CancelUnderline => self.attr.remove(grid::UNDERLINE),
_ => {
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(),
ansi::Mode::ShowCursor => self.mode.insert(mode::SHOW_CURSOR),
ansi::Mode::CursorKeys => self.mode.insert(mode::APP_CURSOR),
_ => {
println!(".. ignoring set_mode");
}
}
}
fn unset_mode(&mut self,mode: ansi::Mode) {
println!("unset_mode: {:?}", mode);
match mode {
ansi::Mode::SwapScreenAndSetRestoreCursor => self.swap_alt(),
ansi::Mode::ShowCursor => self.mode.remove(mode::SHOW_CURSOR),
ansi::Mode::CursorKeys => self.mode.remove(mode::APP_CURSOR),
_ => {
println!(".. ignoring unset_mode");
}
}
}
fn set_scrolling_region(&mut self, top: i64, bot: i64) {
println!("set scroll region: {:?} - {:?}", top, bot);
// 1 is added to bottom for inclusive range
self.scroll_region = (top as usize)..((bot as usize) + 1);
}
}
|