rterm 0.0.6

A port of suckless terminal to rust.
Documentation
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// FIXME: support wide chars

use crate::charset::CharsetTable;
use crate::color::{BG_COLOR, CURSOR_COLOR, CURSOR_REV_COLOR, FG_COLOR};
use crate::cursor::Cursor;
use crate::glyph::{blank_glyph, Glyph, GlyphAttr, GlyphProp};
use crate::point::Point;
use crate::pty::Pty;
use crate::snap::{is_delim, SnapMode};
use crate::utils::{is_between, limit, sort_pair};
use crate::Result;
use bitflags::bitflags;
use std::cmp;
use unicode_width::UnicodeWidthChar;

struct Selection {
    pub mode: SnapMode,
    pub empty: bool,
    pub ob: Point,
    pub oe: Point,
    pub nb: Point,
    pub ne: Point,
}

impl Selection {
    pub fn new() -> Self {
        Selection {
            mode: SnapMode::None,
            empty: true,
            ob: Point::new(0, 0),
            oe: Point::new(0, 0),
            nb: Point::new(0, 0),
            ne: Point::new(0, 0),
        }
    }
}

bitflags! {
    pub struct TermMode: u32 {
        const WRAP        = 1 << 0;
        const INSERT      = 1 << 1;
        const ORIGIN      = 1 << 2;
        const CRLF        = 1 << 3;
        const ALTSCREEN   = 1 << 4;
    }
}

pub const COLS_MIN: usize = 2;
pub const COLS_MAX: usize = u16::MAX as usize;
pub const ROWS_MIN: usize = 1;
pub const ROWS_MAX: usize = u16::MAX as usize;
const TAB_STOP: usize = 8;

pub struct Term {
    pub rows: usize,
    pub cols: usize,
    pub c: Cursor,
    pub dirty: Vec<bool>,
    pub pty: Pty,
    pub scroll_top: usize,
    pub scroll_bot: usize,
    pub charset: CharsetTable,
    pub prop: GlyphProp,
    saved_c: Option<Cursor>,
    saved_alt_c: Option<Cursor>,
    // lines contains the main and alternate screens, access it through the
    // lines() and lines_mut() functions to not have to worry about indexing.
    lines: Vec<Vec<Glyph>>,
    tabs: Vec<bool>,
    mode: TermMode,
    sel: Selection,
}

impl Term {
    pub fn new(cols: usize, rows: usize) -> Result<Self> {
        let mut term = Term {
            rows: 0,
            cols: 0,
            c: Cursor::new(),
            lines: Vec::new(),
            dirty: Vec::new(),
            pty: Pty::new()?,
            scroll_top: 0,
            scroll_bot: 0,
            charset: CharsetTable::new(),
            prop: GlyphProp::new(FG_COLOR, BG_COLOR, GlyphAttr::empty()),
            mode: TermMode::WRAP,
            tabs: Vec::new(),
            sel: Selection::new(),
            saved_c: None,
            saved_alt_c: None,
        };

        term.resize(cols, rows);
        Ok(term)
    }

    pub fn resize(&mut self, cols: usize, rows: usize) -> bool {
        let cols = limit(cols, COLS_MIN, COLS_MAX);
        let rows = limit(rows, ROWS_MIN, ROWS_MAX);

        if cols == self.cols && rows == self.rows {
            return false;
        }

        if !self.sel.empty {
            self.clear_selection();
        }

        if self.c.y > rows - 1 {
            self.scroll_up(0, self.c.y - rows + 1)
        }

        // Double size to hold the main and alternate screens.
        self.lines.resize_with(rows * 2, Vec::new);
        self.lines.shrink_to_fit();
        for line in self.lines.iter_mut() {
            line.resize(cols, blank_glyph());
            line.shrink_to_fit();
        }

        self.dirty.resize(rows, true);
        self.dirty.shrink_to_fit();
        for i in 0..cmp::min(self.rows, rows) {
            self.dirty[i] = true;
        }

        let mut i = self.cols;
        self.tabs.resize_with(cols, || {
            let t = i % TAB_STOP == 0;
            i += 1;
            t
        });
        self.tabs.shrink_to_fit();

        self.scroll_top = 0;
        self.scroll_bot = rows - 1;
        self.cols = cols;
        self.rows = rows;
        self.pty.resize(cols, rows).unwrap();

        // relocate cursor
        self.move_to(self.c.x, self.c.y);
        true
    }

    pub fn set_mode(&mut self, mode: TermMode, val: bool) {
        self.mode.set(mode, val);
    }

    pub fn in_mode(&self, mode: TermMode) -> bool {
        self.mode.contains(mode)
    }

    pub fn get_glyph(&self, x: usize, y: usize) -> Glyph {
        let mut g = self.lines()[y][x];
        g.prop = g.prop.resolve(self.is_selected(x, y));
        g
    }

    pub fn get_glyph_at_cursor(&self) -> Glyph {
        let (x, y) = (self.c.x, self.c.y);
        let mut g = self.lines()[y][x];
        if self.is_selected(x, y) {
            g.prop.bg = CURSOR_REV_COLOR;
        } else {
            g.prop.bg = CURSOR_COLOR;
        }
        g
    }

    pub fn reset(&mut self) {
        self.clear_lines(0..self.rows);
        for i in 0..self.cols {
            self.tabs[i] = i % TAB_STOP == 0;
        }

        self.c.reset();
        self.scroll_top = 0;
        self.scroll_bot = self.rows - 1;
    }

    pub fn set_scroll(&mut self, top: usize, bot: usize) {
        let top = cmp::min(top, self.rows - 1);
        let bot = cmp::min(bot, self.rows - 1);
        let (top, bot) = sort_pair(top, bot);

        self.scroll_top = top;
        self.scroll_bot = bot;
    }

    fn set_dirty<R: Iterator<Item = usize>>(&mut self, range: R) {
        for i in range {
            self.dirty[i] = true;
        }
    }

    pub fn clear_region<R1, R2>(&mut self, xrange: R1, yrange: R2)
    where
        R1: Iterator<Item = usize> + Clone,
        R2: Iterator<Item = usize>,
    {
        let mut glyph = blank_glyph();
        glyph.prop = self.prop;
        for y in yrange {
            self.dirty[y] = true;
            for x in xrange.clone() {
                self.lines_mut()[y][x].clear(glyph);
                if self.is_selected(x, y) {
                    self.clear_selection();
                }
            }
        }
    }

    fn clear_lines<R: Iterator<Item = usize>>(&mut self, range: R) {
        self.clear_region(0..self.cols, range)
    }

    pub fn new_line(&mut self, first_col: bool) {
        if self.c.y == self.scroll_bot {
            self.scroll_up(self.scroll_top, 1);
        } else {
            self.c.y += 1;
        }

        if first_col {
            self.c.x = 0;
        }
    }

    pub fn scroll_up(&mut self, orig: usize, n: usize) {
        assert!(is_between(orig, self.scroll_top, self.scroll_bot));
        if n < 1 {
            return;
        }
        let bottom = self.scroll_bot;
        let n = cmp::min(n, bottom - orig + 1);

        self.clear_lines(orig..orig + n);
        self.set_dirty(orig + n..=bottom);
        self.lines_mut()[orig..=bottom].rotate_left(n);

        self.scroll_selection(orig, -(n as i32));
    }

    pub fn scroll_down(&mut self, orig: usize, n: usize) {
        assert!(is_between(orig, self.scroll_top, self.scroll_bot));
        if n < 1 {
            return;
        }
        let bottom = self.scroll_bot;
        let n = cmp::min(n, bottom - orig + 1);

        self.set_dirty(orig..bottom - n + 1);
        self.clear_lines(bottom - n + 1..=self.scroll_bot);
        self.lines_mut()[orig..=bottom].rotate_right(n);

        self.scroll_selection(orig, n as i32);
    }

    pub fn insert_lines(&mut self, n: usize) {
        if is_between(self.c.y, self.scroll_top, self.scroll_bot) {
            self.scroll_down(self.c.y, n);
        }
    }

    pub fn delete_lines(&mut self, n: usize) {
        if is_between(self.c.y, self.scroll_top, self.scroll_bot) {
            self.scroll_up(self.c.y, n);
        }
    }

    // move to a y pos that is not derived from a previous y pos
    pub fn move_ato(&mut self, x: usize, y: usize) {
        let mut y = y;
        if self.mode.contains(TermMode::ORIGIN) {
            y += self.scroll_top;
        }
        self.move_to(x, y);
    }

    pub fn move_to(&mut self, x: usize, y: usize) {
        self.c.x = cmp::min(x, self.cols - 1);
        if self.mode.contains(TermMode::ORIGIN) {
            self.c.y = limit(y, self.scroll_top, self.scroll_bot);
        } else {
            self.c.y = cmp::min(y, self.rows - 1);
        }
        self.c.wrap_next = false;
    }

    pub fn insert_blanks(&mut self, n: usize) {
        let (x, y) = (self.c.x, self.c.y);
        let n = cmp::min(n, self.cols - x);

        let source = x..self.cols - n;
        let dest = x + n;
        self.lines_mut()[y].copy_within(source, dest);
        self.clear_region(x..x + n, y..=y);
    }

    pub fn delete_chars(&mut self, n: usize) {
        let (x, y, cols) = (self.c.x, self.c.y, self.cols);
        let n = cmp::min(n, cols - x);

        self.lines_mut()[y].copy_within(x + n..cols, x);
        self.clear_region(cols - n..cols, y..=y);
    }

    pub fn put_tabs(&mut self, n: i32) {
        if n == 0 {
            return;
        }

        let mut n = n;
        if n > 0 {
            while n != 0 && self.c.x != self.cols - 1 {
                self.c.x += 1;
                if self.tabs[self.c.x] {
                    n -= 1;
                }
            }
        } else {
            while n != 0 && self.c.x != 0 {
                self.c.x -= 1;
                if self.tabs[self.c.x] {
                    n -= 1;
                }
            }
        }
    }

    pub fn put_char(&mut self, c: char) {
        let width = UnicodeWidthChar::width(c).unwrap_or(0);
        if width == 0 {
            return;
        }

        let cols = self.cols;

        if self.c.wrap_next {
            let y = self.c.y;
            // for wide chars, cursor is not at cols-1
            self.lines_mut()[y][cols - 1]
                .prop
                .attr
                .insert(GlyphAttr::WRAP);
            self.new_line(true);
            self.c.wrap_next = false;
        }

        if self.mode.contains(TermMode::INSERT) && self.c.x + width < cols {
            self.insert_blanks(width);
        }

        if self.c.x + width > cols {
            self.new_line(true);
        }

        if self.is_selected(self.c.x, self.c.y) {
            self.clear_selection();
        }

        // x, y may have updated.
        let (x, y) = (self.c.x, self.c.y);
        self.dirty[y] = true;
        self.lines_mut()[y][x].prop = self.prop;
        self.lines_mut()[y][x].c = c;
        for x2 in x + 1..x + width {
            self.lines_mut()[y][x2].prop.attr.insert(GlyphAttr::DUMMY);
        }

        self.c.x += width;
        if self.c.x == cols {
            if self.mode.contains(TermMode::WRAP) {
                self.c.x -= width;
                self.c.wrap_next = true;
            } else {
                self.c.x = 0;
            }
        }
    }

    pub fn put_string(&mut self, string: String) {
        string.chars().for_each(|c| self.put_char(c));
    }

    pub fn save_cursor(&mut self) {
        if self.mode.contains(TermMode::ALTSCREEN) {
            self.saved_alt_c = Some(self.c);
        } else {
            self.saved_c = Some(self.c);
        }
    }

    pub fn load_cursor(&mut self) {
        let saved = if self.mode.contains(TermMode::ALTSCREEN) {
            self.saved_alt_c
        } else {
            self.saved_c
        };
        if let Some(saved) = saved {
            self.c = saved;
            self.move_to(self.c.x, self.c.y);
        }
    }

    pub fn save_load_cursor(&mut self, save: bool) {
        if save {
            self.save_cursor();
        } else {
            self.load_cursor();
        }
    }

    pub fn clear_tabs<R: Iterator<Item = usize>>(&mut self, range: R) {
        for i in range {
            self.tabs[i] = false;
        }
    }

    pub fn set_tab(&mut self, x: usize) {
        self.tabs[x] = true;
    }

    pub fn start_selection(&mut self, x: usize, y: usize, mode: SnapMode) {
        if !self.sel.empty {
            self.clear_selection();
        }

        self.sel.mode = mode;
        self.sel.ob.x = cmp::min(x, self.cols - 1);
        self.sel.ob.y = cmp::min(y, self.rows - 1);
        self.sel.oe.x = self.sel.ob.x;
        self.sel.oe.y = self.sel.ob.y;

        match self.sel.mode {
            SnapMode::None => (),
            _ => self.normalize_selection(),
        }
    }

    pub fn extend_selection(&mut self, x: usize, y: usize) {
        self.clear_selection();
        self.sel.oe.x = cmp::min(x, self.cols - 1);
        self.sel.oe.y = cmp::min(y, self.rows - 1);
        self.normalize_selection();
    }

    pub fn is_selected(&self, x: usize, y: usize) -> bool {
        !self.sel.empty
            && is_between(y, self.sel.nb.y, self.sel.ne.y)
            && (y != self.sel.nb.y || x >= self.sel.nb.x)
            && (y != self.sel.ne.y || x <= self.sel.ne.x)
    }

    pub fn clear_selection(&mut self) {
        self.sel.empty = true;
        self.set_dirty(self.sel.nb.y..=self.sel.ne.y);
    }

    pub fn get_selection_content(&self) -> Option<String> {
        if self.sel.empty {
            return None;
        }

        let mut string = String::new();

        for y in self.sel.nb.y..=self.sel.ne.y {
            let start = if y == self.sel.nb.y { self.sel.nb.x } else { 0 };

            let end = if y == self.sel.ne.y {
                self.sel.ne.x
            } else {
                self.cols - 1
            };

            let text_end = cmp::min(end + 1, self.text_len(y));
            for x in start..text_end {
                string.push(self.lines()[y][x].c);
            }

            if end == self.cols - 1 && !self.is_wrap_line(y) {
                string.push('\n');
            }
        }

        Some(string)
    }

    pub fn swap_screen(&mut self) {
        self.mode ^= TermMode::ALTSCREEN;
        self.set_dirty(0..self.dirty.len());
    }

    pub fn setdirtattr(&mut self, attr: GlyphAttr) {
        let start = self.first_line();
        for i in 0..self.rows {
            for g in self.lines[start + i].iter() {
                if g.prop.attr.contains(attr) {
                    self.dirty[i] = true;
                    break;
                }
            }
        }
    }

    fn normalize_selection(&mut self) {
        let (mut nb, mut ne) = sort_pair(self.sel.ob, self.sel.oe);

        match self.sel.mode {
            SnapMode::None => {
                let end = self.text_len(nb.y);
                if nb.x > end {
                    nb.x = end;
                }
                if self.text_len(ne.y) <= ne.x {
                    ne.x = self.cols - 1;
                }
            }
            SnapMode::Word => {
                nb = self.snap_word(nb, Self::prev);
                ne = self.snap_word(ne, Self::next);
            }
            SnapMode::Line => {
                nb.x = 0;
                while nb.y > 0 && self.is_wrap_line(nb.y - 1) {
                    nb.y -= 1;
                }
                ne.x = self.cols - 1;
                while ne.y < self.rows - 1 && self.is_wrap_line(ne.y) {
                    ne.y += 1;
                }
            }
        }

        self.set_dirty(nb.y..=ne.y);
        self.sel.empty = false;
        self.sel.nb = nb;
        self.sel.ne = ne;
    }

    fn scroll_selection(&mut self, orig: usize, n: i32) {
        if self.sel.empty {
            return;
        }

        if !is_between(self.sel.ob.y, orig, self.scroll_bot)
            || !is_between(self.sel.ob.y, orig, self.scroll_bot)
        {
            self.clear_selection();
            return;
        }

        let by = self.sel.ob.y as i32 + n;
        let ey = self.sel.oe.y as i32 + n;
        if !is_between(by, orig as i32, self.scroll_bot as i32)
            || !is_between(ey, orig as i32, self.scroll_bot as i32)
        {
            self.clear_selection();
            return;
        }

        self.sel.ob.y = by as usize;
        self.sel.oe.y = ey as usize;
        self.normalize_selection();
    }

    fn prev(&self, p: &Point) -> Option<Point> {
        if p.x > 0 {
            return Some(Point::new(p.x - 1, p.y));
        }
        if p.y > 0 {
            let p = Point::new(self.cols - 1, p.y - 1);
            if self.lines()[p.y][p.x].prop.attr.contains(GlyphAttr::WRAP) {
                return Some(p);
            }
        }
        None
    }

    fn next(&self, p: &Point) -> Option<Point> {
        if p.x < self.cols - 1 {
            return Some(Point::new(p.x + 1, p.y));
        }
        if p.y < self.rows - 1 && self.lines()[p.y][p.x].prop.attr.contains(GlyphAttr::WRAP) {
            return Some(Point::new(0, p.y + 1));
        }
        None
    }

    fn snap_word<F>(&self, point: Point, f: F) -> Point
    where
        F: Fn(&Self, &Point) -> Option<Point>,
    {
        let c = self.lines()[point.y][point.x].c;
        let delim = is_delim(c);

        let mut point = point;
        while let Some(next_p) = f(self, &point) {
            let next_c = self.lines()[next_p.y][next_p.x].c;
            if next_c != c && (delim || is_delim(next_c)) {
                break;
            }
            point = next_p;
        }
        point
    }

    fn text_len(&self, y: usize) -> usize {
        let mut x = self.cols;
        if self.is_wrap_line(y) {
            return x;
        }
        while x > 0 && self.lines()[y][x - 1].c == ' ' {
            x -= 1
        }
        x
    }

    fn is_wrap_line(&self, y: usize) -> bool {
        self.lines()[y][self.cols - 1]
            .prop
            .attr
            .contains(GlyphAttr::WRAP)
    }

    #[inline]
    fn first_line(&self) -> usize {
        if self.mode.contains(TermMode::ALTSCREEN) {
            self.rows
        } else {
            0
        }
    }

    #[inline]
    fn lines(&self) -> &[Vec<Glyph>] {
        if self.mode.contains(TermMode::ALTSCREEN) {
            &self.lines[self.rows..]
        } else {
            &self.lines[0..self.rows]
        }
    }

    #[inline]
    fn lines_mut(&mut self) -> &mut [Vec<Glyph>] {
        if self.mode.contains(TermMode::ALTSCREEN) {
            &mut self.lines[self.rows..]
        } else {
            &mut self.lines[0..self.rows]
        }
    }
}