fltk-term 0.2.5

A minimal terminal widget for fltk
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
use crate::styles::*;
use fltk::enums::Color;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Style {
    pub fg: Color,
    pub bg: Color,
    pub bold: bool,
    pub faint: bool,
    pub italic: bool,
    pub underline: bool,
    pub strikethrough: bool,
    pub overline: bool,
    pub inverse: bool,
}

impl Default for Style {
    fn default() -> Self {
        Self {
            fg: WHITE,
            bg: BLACK,
            bold: false,
            faint: false,
            italic: false,
            underline: false,
            strikethrough: false,
            overline: false,
            inverse: false,
        }
    }
}

impl Style {
    pub fn new(bg: Color, fg: Color) -> Self {
        Self {
            fg,
            bg,
            bold: false,
            faint: false,
            italic: false,
            underline: false,
            strikethrough: false,
            overline: false,
            inverse: false,
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Cell {
    pub ch: char,
    pub style: Style,
}

impl Cell {
    pub fn new(ch: char, style: Style) -> Self {
        Self { ch, style }
    }
}

pub struct CellBuffer {
    lines: Vec<Vec<Cell>>, // simple grow-only buffer for now
    pub max_lines: usize,
    pub cur_style: Style,
    pub default_bg: Color,
    pub default_fg: Color,
    dirty_start: Option<usize>,
    dirty_end: Option<usize>,
    dirty_cols: Vec<Option<(usize, usize)>>,
    cursor_row: usize,
    cursor_col: usize,
    cols: usize,
    rows: usize,
}

impl CellBuffer {
    pub fn new(max_lines: usize, default_bg: Color, default_fg: Color) -> Self {
        Self {
            lines: vec![Vec::new()],
            max_lines,
            cur_style: Style {
                fg: default_fg,
                bg: default_bg,
                ..Default::default()
            },
            default_bg,
            default_fg,
            dirty_start: None,
            dirty_end: None,
            dirty_cols: vec![None],
            cursor_row: 0,
            cursor_col: 0,
            cols: 80,
            rows: 24,
        }
    }

    pub fn set_style(&mut self, style: Style) {
        self.cur_style = style;
    }

    pub fn push_char(&mut self, ch: char) {
        if let Some(line) = self.lines.last_mut() {
            let col = line.len();
            line.push(Cell::new(ch, self.cur_style));
            let idx = self.lines.len().saturating_sub(1);
            self.mark_dirty_line(idx);
            self.mark_dirty_cols(idx, col, col);
            self.cursor_row = idx;
            self.cursor_col = col + 1;
        }
    }

    pub fn newline(&mut self) {
        self.lines.push(Vec::new());
        self.dirty_cols.push(None);
        if self.lines.len() > self.max_lines {
            let overflow = self.lines.len() - self.max_lines;
            self.lines.drain(0..overflow);
            if overflow > 0 {
                if self.dirty_cols.len() >= overflow {
                    self.dirty_cols.drain(0..overflow);
                }
                self.cursor_row = self.cursor_row.saturating_sub(overflow);
            }
        }
        let idx = self.lines.len().saturating_sub(1);
        self.mark_dirty_line(idx);
        self.mark_dirty_cols(idx, 0, 0);
        self.cursor_row = idx;
        self.cursor_col = 0;
    }

    pub fn clear_line_right(&mut self) {
        if let Some(line) = self.lines.last_mut() {
            let before = line.len();
            line.clear();
            let idx = self.lines.len().saturating_sub(1);
            self.mark_dirty_line(idx);
            if before > 0 {
                self.mark_dirty_cols(idx, 0, before - 1);
            }
            self.cursor_row = idx;
            self.cursor_col = 0;
        }
    }

    pub fn snapshot(&self) -> Vec<Vec<Cell>> {
        self.lines.clone()
    }

    pub fn set_dimensions(&mut self, cols: usize, rows: usize) {
        self.cols = cols.max(1);
        self.rows = rows.max(1);
        // Keep cursor anchored to bottom region if it fell outside
        let st = self.screen_top();
        let sb = st + self.rows.saturating_sub(1);
        if self.cursor_row < st {
            self.cursor_row = st;
            self.cursor_col = 0;
        } else if self.cursor_row > sb {
            self.cursor_row = sb;
            self.cursor_col = 0;
        }
    }

    fn screen_top(&self) -> usize {
        self.lines.len().saturating_sub(self.rows)
    }

    fn screen_bottom(&self) -> usize {
        self.lines.len().saturating_sub(1).max(
            self.screen_top()
                .saturating_add(self.rows.saturating_sub(1)),
        )
    }

    fn mark_dirty_line(&mut self, idx: usize) {
        self.dirty_start = Some(self.dirty_start.map(|s| s.min(idx)).unwrap_or(idx));
        self.dirty_end = Some(self.dirty_end.map(|e| e.max(idx)).unwrap_or(idx));
    }

    fn mark_dirty_cols(&mut self, idx: usize, start: usize, end: usize) {
        if idx >= self.dirty_cols.len() {
            self.dirty_cols.resize(idx + 1, None);
        }
        self.dirty_cols[idx] = match self.dirty_cols[idx] {
            Some((s, e)) => Some((s.min(start), e.max(end))),
            None => Some((start, end)),
        };
    }

    pub fn take_dirty(&mut self) -> Option<(usize, usize)> {
        match (self.dirty_start.take(), self.dirty_end.take()) {
            (Some(s), Some(e)) if s <= e => Some((s, e)),
            _ => None,
        }
    }

    pub fn take_dirty_areas(&mut self) -> Vec<(usize, usize, usize)> {
        let mut areas = Vec::new();
        for (i, rng) in self.dirty_cols.iter_mut().enumerate() {
            if let Some((s, e)) = rng.take() {
                areas.push((i, s, e));
            }
        }
        areas
    }

    pub fn cursor(&self) -> (usize, usize) {
        (self.cursor_row, self.cursor_col)
    }
    pub fn set_cursor(&mut self, row: usize, col: usize) {
        self.cursor_row = row;
        self.cursor_col = col;
    }

    fn ensure_row(&mut self, row: usize) {
        while self.lines.len() <= row {
            self.lines.push(Vec::new());
            self.dirty_cols.push(None);
        }
    }

    fn ensure_col(&mut self, row: usize, col: usize) {
        self.ensure_row(row);
        let line = &mut self.lines[row];
        if line.len() <= col {
            let pad = col + 1 - line.len();
            for _ in 0..pad {
                line.push(Cell::new(' ', self.cur_style));
            }
        }
    }

    pub fn write_char(&mut self, ch: char) {
        // simple DECAWM-style wrap at cols
        if self.cols > 0 && self.cursor_col >= self.cols {
            self.line_feed();
        }
        let (row, col) = (self.cursor_row, self.cursor_col);
        self.ensure_col(row, col);
        if let Some(cell) = self.lines[row].get_mut(col) {
            cell.ch = ch;
            cell.style = self.cur_style;
        }
        self.mark_dirty_line(row);
        self.mark_dirty_cols(row, col, col);
        self.cursor_col = self.cursor_col.saturating_add(1);
    }

    pub fn carriage_return(&mut self) {
        self.cursor_col = 0;
    }

    pub fn line_feed(&mut self) {
        self.cursor_row = self.cursor_row.saturating_add(1);
        self.cursor_col = 0;
        self.ensure_row(self.cursor_row);
        self.mark_dirty_line(self.cursor_row);
    }

    pub fn move_cursor_rel(&mut self, drow: isize, dcol: isize) {
        let st = self.screen_top();
        let sb = st + self.rows.saturating_sub(1);
        let nr = (self.cursor_row as isize + drow).clamp(st as isize, sb as isize) as usize;
        let mut nc = (self.cursor_col as isize + dcol).max(0) as usize;
        // clamp col to screen cols if set
        if self.cols > 0 {
            nc = nc.min(self.cols.saturating_sub(1));
        }
        self.cursor_row = nr;
        self.cursor_col = nc;
        self.ensure_row(nr);
    }

    pub fn move_cursor_abs(&mut self, row1: usize, col1: usize) {
        // Interpret row/col as screen-relative (0-based)
        let st = self.screen_top();
        let row = st + row1.min(self.rows.saturating_sub(1));
        let col = if self.cols > 0 {
            col1.min(self.cols.saturating_sub(1))
        } else {
            col1
        };
        self.cursor_row = row;
        self.cursor_col = col;
        self.ensure_row(row);
    }

    pub fn clear_eol(&mut self) {
        self.ensure_row(self.cursor_row);
        let col = self.cursor_col;
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            if col < line.len() {
                let end = line.len() - 1;
                line.truncate(col);
                self.mark_dirty_line(self.cursor_row);
                self.mark_dirty_cols(self.cursor_row, col, end);
            }
        }
    }

    // EL 0: erase from cursor to end of line (inclusive)
    pub fn clear_eol_0(&mut self) {
        self.ensure_row(self.cursor_row);
        let col = self.cursor_col;
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            if col < line.len() {
                let end = line.len().saturating_sub(1);
                for i in col..=end {
                    if let Some(cell) = line.get_mut(i) {
                        cell.ch = ' ';
                        cell.style = self.cur_style;
                    }
                }
                self.mark_dirty_line(self.cursor_row);
                self.mark_dirty_cols(self.cursor_row, col, end);
            }
        }
    }

    // EL 1: erase from start of line to cursor (inclusive of position before the cursor)
    pub fn clear_eol_1(&mut self) {
        let row = self.cursor_row;
        self.ensure_row(row);
        let col = self.cursor_col;
        // Ensure cells exist up to cursor first to avoid borrow conflict
        self.ensure_col(row, col);
        if let Some(line) = self.lines.get_mut(row) {
            if !line.is_empty() {
                let end = col.min(line.len().saturating_sub(1));
                for i in 0..=end {
                    if let Some(cell) = line.get_mut(i) {
                        cell.ch = ' ';
                        cell.style = self.cur_style;
                    }
                }
                self.mark_dirty_line(row);
                self.mark_dirty_cols(row, 0, end);
            }
        }
    }

    // EL 2: erase entire line
    pub fn clear_eol_2(&mut self) {
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            let len = line.len();
            if len > 0 {
                for c in line.iter_mut() {
                    c.ch = ' ';
                    c.style = self.cur_style;
                }
                self.mark_dirty_line(self.cursor_row);
                self.mark_dirty_cols(self.cursor_row, 0, len.saturating_sub(1));
            }
        }
    }

    pub fn clear_ed_0(&mut self) {
        // Clear from cursor to end of screen
        self.clear_eol_0();
        let row = self.cursor_row;
        let sb = self.screen_top() + self.rows.saturating_sub(1);
        let end_row = sb.min(self.lines.len().saturating_sub(1));
        if row < end_row {
            for r in row + 1..=end_row {
                let len = self.lines[r].len();
                if len > 0 {
                    self.lines[r].clear();
                    self.mark_dirty_line(r);
                    self.mark_dirty_cols(r, 0, len.saturating_sub(1));
                }
            }
        }
    }

    pub fn clear_ed_1(&mut self) {
        // Clear from start of screen to cursor position (inclusive)
        let row = self.cursor_row;
        let st = self.screen_top();
        // clear previous lines within the visible screen entirely
        for r in st..row {
            let len = self.lines.get(r).map(|l| l.len()).unwrap_or(0);
            if len > 0 {
                if let Some(line) = self.lines.get_mut(r) {
                    line.clear();
                }
                self.mark_dirty_line(r);
                self.mark_dirty_cols(r, 0, len.saturating_sub(1));
            }
        }
        // clear current line from start to cursor col without shifting remainder
        self.ensure_row(row);
        let col = self.cursor_col;
        // ensure cells exist up to cursor before borrowing line
        self.ensure_col(row, col);
        if let Some(line) = self.lines.get_mut(row) {
            let end = col.min(line.len().saturating_sub(1));
            for i in 0..=end {
                if let Some(cell) = line.get_mut(i) {
                    cell.ch = ' ';
                    cell.style = self.cur_style;
                }
            }
            self.mark_dirty_line(row);
            if end < usize::MAX {
                self.mark_dirty_cols(row, 0, end);
            }
        }
    }

    pub fn clear_ed_2(&mut self) {
        // Clear entire screen (visible area only)
        let st = self.screen_top();
        let sb = st + self.rows.saturating_sub(1);
        let end_row = sb.min(self.lines.len().saturating_sub(1));
        for r in st..=end_row {
            let len = self.lines.get(r).map(|l| l.len()).unwrap_or(0);
            if let Some(line) = self.lines.get_mut(r) {
                if len > 0 {
                    line.clear();
                }
            }
            if len > 0 {
                self.mark_dirty_line(r);
                self.mark_dirty_cols(r, 0, len.saturating_sub(1));
            }
        }
        // place cursor at top-left of screen
        self.cursor_row = st;
        self.cursor_col = 0;
    }

    pub fn clear_scrollback(&mut self) {
        // Clear scrollback: keep only the visible screen region
        let st = self.screen_top();
        if st > 0 {
            let keep: Vec<Vec<Cell>> = self.lines.split_off(st);
            self.lines = keep;
            self.dirty_cols = vec![None; self.lines.len()];
            self.cursor_row = self.cursor_row.saturating_sub(st);
            self.mark_dirty_line(0);
            if !self.lines.is_empty() {
                let last_len = self.lines[0].len();
                self.mark_dirty_cols(0, 0, last_len.saturating_sub(1));
            }
        }
    }

    pub fn insert_blanks(&mut self, count: usize) {
        self.ensure_row(self.cursor_row);
        let col = self.cursor_col;
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            let blanks = std::iter::repeat_n(Cell::new(' ', self.cur_style), count);
            if col >= line.len() {
                line.extend(blanks);
            } else {
                line.splice(col..col, blanks);
            }
            let end = col + count;
            self.mark_dirty_line(self.cursor_row);
            self.mark_dirty_cols(self.cursor_row, col, end);
        }
    }

    pub fn delete_chars(&mut self, count: usize) {
        self.ensure_row(self.cursor_row);
        let col = self.cursor_col;
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            if col < line.len() {
                let end = (col + count).min(line.len());
                line.drain(col..end);
                self.mark_dirty_line(self.cursor_row);
                self.mark_dirty_cols(self.cursor_row, col, end.saturating_sub(1));
            }
        }
    }

    pub fn insert_lines(&mut self, count: usize) {
        let row = self.cursor_row.min(self.lines.len());
        for _ in 0..count {
            self.lines.insert(row, Vec::new());
            self.dirty_cols.insert(row, None);
        }
        // Trim to max_lines by removing from end
        if self.lines.len() > self.max_lines {
            let overflow = self.lines.len() - self.max_lines;
            for _ in 0..overflow {
                self.lines.pop();
                self.dirty_cols.pop();
            }
        }
        // Mark dirty affected range
        let end = (row + count).min(self.lines.len().saturating_sub(1));
        self.mark_dirty_line(row);
        self.mark_dirty_line(end);
    }

    pub fn delete_lines(&mut self, count: usize) {
        let row = self.cursor_row.min(self.lines.len());
        let end = (row + count).min(self.lines.len());
        if row < end {
            self.lines.drain(row..end);
            self.dirty_cols.drain(row..end);
            // push one empty line at bottom to maintain capacity if desired
            if self.lines.len() < self.max_lines {
                self.lines.push(Vec::new());
                self.dirty_cols.push(None);
            }
            self.mark_dirty_line(row);
        }
    }

    // Horizontal Tab: advance to next 8-column stop
    pub fn tab(&mut self) {
        let next = ((self.cursor_col / 8) + 1) * 8;
        if next > self.cursor_col {
            // ensure cells until next-1 exist (filled with spaces)
            self.ensure_col(self.cursor_row, next.saturating_sub(1));
            // mark dirty area
            self.mark_dirty_line(self.cursor_row);
            self.mark_dirty_cols(self.cursor_row, self.cursor_col, next.saturating_sub(1));
            self.cursor_col = next;
        }
    }

    // ECH: erase N characters from cursor, cursor does not move
    pub fn erase_chars(&mut self, count: usize) {
        if count == 0 {
            return;
        }
        let row = self.cursor_row;
        let start = self.cursor_col;
        let end = start.saturating_add(count).saturating_sub(1);
        self.ensure_col(row, end);
        if let Some(line) = self.lines.get_mut(row) {
            let last = end.min(line.len().saturating_sub(1));
            for i in start..=last {
                if let Some(cell) = line.get_mut(i) {
                    cell.ch = ' ';
                    cell.style = self.cur_style;
                }
            }
            self.mark_dirty_line(row);
            self.mark_dirty_cols(row, start, last);
        }
    }
}