cursive_core 0.4.6

Core components for the Cursive TUI
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
//! Output buffer

use crate::backend::Backend;
use crate::style::ConcreteStyle;
use crate::{Rect, Vec2};

use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

/// The width of a cell.
///
/// Most characters are single-width. Some asian characters and emojis are double-width.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CellWidth {
    /// This character takes a single cell in the grid.
    Single,

    /// This character takes 2 cells in the grid (mostly for emojis and asian characters).
    Double,
}

impl Default for CellWidth {
    fn default() -> Self {
        CellWidth::Single
    }
}

impl CellWidth {
    /// Convert the width as returned from `UnicodeWidthStr::width()` into a `CellWidth`.
    ///
    /// # Panics
    ///
    /// If `width > 2`.
    pub fn from_usize(width: usize) -> Self {
        match width {
            1 => CellWidth::Single,
            2 => CellWidth::Double,
            n => panic!("expected width of 1 or 2 only. Got {n}."),
        }
    }

    /// Returns the width as a usize: 1 or 2.
    pub fn as_usize(self) -> usize {
        match self {
            CellWidth::Single => 1,
            CellWidth::Double => 2,
        }
    }

    /// Returns the width of the given grapheme.
    ///
    /// # Panics
    ///
    /// If `text` has a width > 2 (it means it is not a single grapheme).
    pub fn from_grapheme(text: &str) -> Self {
        Self::from_usize(text.width())
    }
}

/// A single cell in a grid.
///
/// Most characters use 1 cell in the grid. Some wide graphemes use 2 cells
/// (mostly asian characters and some emojis).
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct Cell {
    /// Style used for this cell.
    style: ConcreteStyle,

    /// Text buffer for this cell.
    ///
    /// Most graphemes fit in a couple bytes, but there's theoretically no limit.
    text: compact_str::CompactString,

    /// Either 1 or 2.
    ///
    /// If it's 2, the next cell should be None.
    ///
    /// Should be equal to `text.width()`
    ///
    /// TODO: Use a smaller sized integer to reduce the memory footprint?
    width: CellWidth,
}

impl Cell {
    /// Returns the text content of this cell.
    ///
    /// This should be a single grapheme.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Returns the width of this cell: either 1 or 2.
    ///
    /// If this returns 2, then the next cell in the grid should be empty.
    pub fn width(&self) -> usize {
        self.width.as_usize()
    }

    /// Sets the style for this cell.
    pub fn set_style(&mut self, style: ConcreteStyle) {
        self.style = style;
    }

    /// Sets the content of this cell.
    ///
    /// `text` should be a single grapheme, with width 1 or 2.
    ///
    /// # Panics
    ///
    /// If `text.width() > 2`.
    pub fn set_text(&mut self, text: &str) {
        self.text.clear();
        self.text.push_str(text);
        self.width = CellWidth::from_grapheme(text);
    }

    fn set(&mut self, style: ConcreteStyle, text: &str, width: CellWidth) {
        self.style = style;
        self.text.clear();
        self.text.push_str(text);
        self.width = width;
    }
}

/// A buffer for printing stuff.
///
/// Can be flushed out to the backend.
///
/// The goal of the buffer is to accumulate all print operation for a refresh cycle, then flush it
/// all to the backend in one go. This should improve performance by:
/// * Reducing the amount of cursor movements.
/// * Only flushing the diff from the previous screen.
/// * Removing any delay during printing the screen that could result in tearing.
pub struct PrintBuffer {
    // A cell can be `None` if the cell before was double-wide (or more).
    // It can also be `None` if this is the first time since last resize.

    // Where we are actively writing.
    active_buffer: Vec<Option<Cell>>,

    // Last buffer flushed.
    //
    // Used to compute the diff between active and frozen when flushing.
    frozen_buffer: Vec<Option<Cell>>,

    // This is an internal cache used to remember the last style flushed to the backend.
    current_style: ConcreteStyle,

    size: Vec2,
}

/// A view into a rectangular area of the buffer.
pub struct Window<'a> {
    buffer: &'a mut PrintBuffer,
    viewport: Rect,
}

impl<'a> Window<'a> {
    /// Returns the cell at the given location.
    ///
    /// Returns `None` if the cell is empty because the previous one was double-wide.
    pub fn cell_at(&self, pos: Vec2) -> Option<&Cell> {
        let pos = self.absolute_pos(pos)?;

        self.buffer.cell_at(pos)
    }

    fn absolute_pos(&self, pos: Vec2) -> Option<Vec2> {
        if !pos.fits_in(self.viewport.size()) {
            return None;
        }

        Some(pos + self.viewport.top_left())
    }

    /// Iterate on the rows of this window.
    pub fn rows(&self) -> impl Iterator<Item = &[Option<Cell>]> {
        self.buffer
            .rows()
            .skip(self.viewport.top())
            .take(self.viewport.height())
            .map(|row| &row[self.viewport.left()..=self.viewport.right()])
    }

    /// Returns the viewport this window is covering.
    pub fn viewport(&self) -> Rect {
        self.viewport
    }

    /// Returns the size of this window.
    pub fn size(&self) -> Vec2 {
        self.viewport.size()
    }

    /// Get mutable access to the style at the given cell, if any.
    pub fn style_at_mut<V>(&mut self, pos: V) -> Option<&mut ConcreteStyle>
    where
        V: Into<Vec2>,
    {
        let pos = pos.into();
        let pos = self.absolute_pos(pos)?;

        self.buffer.style_at_mut(pos)
    }
}

impl Default for PrintBuffer {
    fn default() -> Self {
        Self::new()
    }
}

impl PrintBuffer {
    /// Create a new empty print buffer.
    pub const fn new() -> Self {
        PrintBuffer {
            active_buffer: Vec::new(),
            frozen_buffer: Vec::new(),
            current_style: ConcreteStyle::terminal_default(),
            size: Vec2::ZERO,
        }
    }

    /// Iterate on the rows of this buffer.
    pub fn rows(&self) -> impl Iterator<Item = &[Option<Cell>]> {
        self.active_buffer.chunks(self.size.x)
    }

    /// Clear this buffer.
    ///
    /// The buffer will be empty afterwards.
    pub fn clear(&mut self) {
        let len = self.size.x * self.size.y;

        let reset = |buffer: &mut Vec<Option<Cell>>| {
            buffer.clear();
            buffer.resize_with(len, Default::default);
        };

        reset(&mut self.active_buffer);
        reset(&mut self.frozen_buffer);
    }

    /// Fill the buffer with the given text and style.
    pub fn fill(&mut self, text: &str, style: impl Into<ConcreteStyle>) {
        let style = style.into();
        let width = CellWidth::from_usize(text.width());
        if width != CellWidth::Single {
            panic!("Filling the screen with double-wide characters is not currently supported.");
        }

        for cell in &mut *self.active_buffer {
            let cell = cell.get_or_insert_with(Default::default);
            cell.style = style;
            cell.text.clear();
            cell.text.push_str(text);
            cell.width = width;
        }
    }

    /// Returns the current size of the buffer.
    pub fn size(&self) -> Vec2 {
        self.size
    }

    /// Resize the buffer to the given size.
    pub fn resize(&mut self, size: Vec2) {
        if self.size == size {
            return;
        }

        self.size = size;
        let len = size.x * size.y;

        self.active_buffer.clear();
        self.frozen_buffer.clear();

        self.active_buffer.resize_with(len, Default::default);
        self.frozen_buffer.resize_with(len, Default::default);
    }

    /// Print some text at the given location.
    pub fn print_at(&mut self, start: Vec2, text: &str, style: ConcreteStyle) {
        if !(start.strictly_lt(self.size)) {
            return;
        }

        // If the character before was double-wide, we need to remove it.
        // TODO: Do not re-compute the ID of the first cell twice?
        let id = self.cell_id(start);
        if self.active_buffer[id].is_none() && start.x > 0 {
            // If the previous character is double-wide, then this cell would be None.
            // So only check that if we're None to begin with.
            // Here `id - 1` is safe to compute since `start.x > 0`.
            if let Some(ref mut prev) = self.active_buffer[id - 1] {
                if prev.width == CellWidth::Double {
                    prev.width = CellWidth::Single;
                    prev.text.clear();
                    prev.text.push_str(" ");
                    // Preserve style.
                }
            }
        }

        let mut pos = start;

        // We only consider "regular" graphemes that can be displayed.
        //
        // Control characters should not be displayed.
        fn is_control_char(g: &str) -> bool {
            g.chars()
                .all(|c| matches!(c, (..='\u{001F}') | ('\u{007F}'..='\u{009F}')))
        }

        // Fill our active buffer
        // TODO: Use some WithWidth(&str, usize) to not re-compute width a thousand times
        for g in text.graphemes(true) {
            let width = g.width();
            if width == 0 {
                // Any zero-width grapheme can be ignored.
                // With unicode-width < 0.1.13, this includes control chars.
                continue;
            }

            if is_control_char(g) {
                // With unicode-width >= 0.1.13, control chars have non-zero width (in
                // practice width = 1).
                debug_assert_eq!(
                    1, width,
                    "Control character '{g:?}' should've had a width of 1"
                );
                debug_assert_eq!(1, "\u{FFFD}".width(), "\u{FFFD} should've had a width of 1");
                self.set_cell(pos, "\u{fffd}", CellWidth::from_usize(width), style);
            } else {
                self.set_cell(pos, g, CellWidth::from_usize(width), style);
            }
            pos.x += width;
        }
    }

    fn cell_id(&self, pos: Vec2) -> usize {
        pos.x + pos.y * self.size.x
    }

    /// Get mutable access to the style at the given cell, if any.
    ///
    /// Returns `None` if the previous cell was double-wide.
    pub fn style_at_mut(&mut self, pos: Vec2) -> Option<&mut ConcreteStyle> {
        let id = self.cell_id(pos);
        self.active_buffer[id].as_mut().map(|cell| &mut cell.style)
    }

    /// Returns the cell at the given location.
    pub fn cell_at(&self, pos: Vec2) -> Option<&Cell> {
        let id = self.cell_id(pos);
        self.active_buffer[id].as_ref()
    }

    /// Returns a mutable access to a sub-region from this buffer.
    pub fn window(&mut self, viewport: Rect) -> Option<Window<'_>> {
        if !viewport.bottom_right().fits_in(self.size) {
            return None;
        }

        Some(Window {
            buffer: self,
            viewport,
        })
    }

    /// Get the text at the given position
    ///
    /// Returns `None` if there is no text, because the previous cell was double-wide.
    pub fn cell_text(&self, pos: Vec2) -> Option<&str> {
        let id = self.cell_id(pos);
        self.active_buffer[id].as_ref().map(|cell| cell.text())
    }

    /// Get the style at the given position.
    ///
    /// Returns `None` if there is no text, because the previous cell was double-wide.
    pub fn cell_style(&self, pos: Vec2) -> Option<ConcreteStyle> {
        let id = self.cell_id(pos);
        self.active_buffer[id].as_ref().map(|cell| cell.style)
    }

    /// Set a cell.
    ///
    /// width _must_ be grapheme.width().
    fn set_cell(&mut self, pos: Vec2, grapheme: &str, width: CellWidth, style: ConcreteStyle) {
        debug_assert_eq!(width.as_usize(), grapheme.width());

        let id = self.cell_id(pos);

        let cell = &mut self.active_buffer[id].get_or_insert_with(Default::default);
        cell.set(style, grapheme, width);

        // If this is a double-wide grapheme, mark the next cell as blocked.
        for dx in 1..width.as_usize() {
            if pos.x + dx >= self.size.x {
                break;
            }

            self.active_buffer[id + dx] = None;
        }
    }

    /// Flush out all accumulated prints so far.
    ///
    /// * Assumes the backend was representing `self.frozen_buffer`.
    /// * Ensures the backend now represents `self.active_buffer`.
    /// * Try to minimize the commands sent to the backend to achieve that.
    ///
    /// Afterwards, replace `self.frozen_buffer` with `self.active_buffer`.
    /// `self.active_buffer` should not be affected by this call.
    ///
    /// Note: this does _not_ call `backend.refresh()`!
    ///
    /// Ideally it should be possible to call `flush()` at any times, possibly repeatedly.
    ///
    /// (Successive calls should do nothing.)
    pub fn flush(&mut self, backend: &dyn Backend) {
        let terminal_width = self.size.x;

        let persistent = backend.is_persistent();

        let mut current_pos = Vec2::zero();
        backend.move_to(current_pos);

        for (i, (active, frozen)) in self
            .active_buffer
            .iter()
            .zip(self.frozen_buffer.iter())
            .enumerate()
        {
            if persistent && active == frozen {
                // TODO (optim): it may be pricier to omit printing a letter but to then "move to" the
                // cell to the right. So there should be a price N for the jump, and wait until we see
                // N bytes without changes to actually jump. If it changes before that, flush the
                // unchanged bytes rather than the jump.

                // Let's not change this cell.
                continue;
            }

            // eprintln!("Non matching: {frozen:?} -> {active:?}");

            // Skip empty cells.
            let Some(Cell { style, text, width }) = active else {
                continue;
            };

            let x = i % terminal_width;
            let y = i / terminal_width;

            // Should we move?
            if current_pos != (x, y) {
                current_pos = Vec2::new(x, y);
                backend.move_to(current_pos);
            }

            // Make sure we have the correct style
            // eprintln!("Applying {style:?} over {:?} for {text} @ {x}:{y}", self.current_style);
            apply_diff(&self.current_style, style, backend);
            self.current_style = *style;

            backend.print(text);

            current_pos.x += width.as_usize();

            // Assume we never wrap over?
        }

        // Keep the active buffer the same, because why not?
        // We could also flush it to Nones?
        self.frozen_buffer.clone_from_slice(&self.active_buffer);
    }
}

fn apply_diff(old: &ConcreteStyle, new: &ConcreteStyle, backend: &dyn Backend) {
    if old.color != new.color {
        // TODO: flush front/back colors separately?
        backend.set_color(new.color);
    }

    // Check the diff between two effect sets:
    // - Effects in new but not in old
    for effect in new.effects.iter() {
        if old.effects.contains(effect) {
            continue;
        }
        backend.set_effect(effect);
    }
    // - Effects in old but not in new
    for effect in old.effects.iter() {
        if new.effects.contains(effect) {
            continue;
        }
        backend.unset_effect(effect);
    }
}