uncurses 0.0.5

A Rust terminal rendering library
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
//! Buffer insert/delete operations.

use crate::cell::Cell;
use crate::layout::Position;

use super::{Buffer, fill_line_into};

/// Replace row `y` entirely with `fill`. Wide fills are stepped by
/// `fill.width()` so primaries own their continuation slots. Whole-row
/// replacement means we don't need to clean up wide cells in the row
/// being overwritten — they're discarded as the row is rewritten.
fn fill_row(buf: &mut Buffer, y: u16, fill: &Cell) {
    if let Some(line) = buf.line_mut(y) {
        fill_line_into(line, fill);
    }
}

/// Fill `[lo, hi)` on row `y` with `fill`, routing through `set()` so
/// wide-cell semantics around the fill region (and any wide cells in
/// the slots being overwritten) are honored. Wide fills are laid down
/// one primary per `fill.width()` slots; any trailing partial slot gets
/// a plain blank.
fn fill_range(buf: &mut Buffer, y: u16, lo: u16, hi: u16, fill: &Cell) {
    if lo >= hi {
        return;
    }
    let step = fill.width().max(1) as u16;
    let mut x = lo;
    while x + step <= hi {
        buf.set((x, y), fill);
        x += step;
    }
    while x < hi {
        buf.set((x, y), &Cell::BLANK);
        x += 1;
    }
}

impl Buffer {
    /// Insert rows within a bounded vertical region.
    ///
    /// Existing rows in `[y, bounds_bottom)` are pushed downward by `n`.
    /// Rows pushed past `bounds_bottom` are discarded, and the newly opened
    /// rows starting at `y` are filled with `fill`.
    ///
    /// # Parameters
    ///
    /// - `y`: zero-based row where insertion begins.
    /// - `n`: number of rows to insert.
    /// - `bounds_bottom`: exclusive lower row bound for the affected region;
    ///   clamped to the buffer height.
    /// - `fill`: cell used to fill inserted rows.
    ///
    /// # Returns
    ///
    /// Nothing.
    ///
    /// # Panics
    ///
    /// Never panics.
    ///
    /// # Usage notes
    ///
    /// Calls with `y >= bounds_bottom` are no-ops. Whole rows are swapped in
    /// row-major storage, so wide-cell pairs inside moved rows remain
    /// structurally unchanged.
    pub fn insert_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
        let bottom = bounds_bottom.min(self.height) as usize;
        let y_usize = y as usize;
        let n = n as usize;

        if y_usize >= bottom {
            return;
        }

        // Shift lines down
        let shift_end = bottom;
        let shift_start = (y_usize + n).min(shift_end);

        // Move from bottom to top to avoid overwrites
        for i in (shift_start..shift_end).rev() {
            let src = i - n;
            if src >= y_usize {
                self.swap_rows(i, src);
            }
        }

        // Clear the inserted lines
        let fill_end = y_usize + n.min(shift_end - y_usize);
        for i in y_usize..fill_end {
            fill_row(self, i as u16, fill);
        }
    }

    /// Delete rows within a bounded vertical region.
    ///
    /// Existing rows below the deleted range are pulled upward within
    /// `[y, bounds_bottom)`. The freed rows at the bottom of the region are
    /// filled with `fill`.
    ///
    /// # Parameters
    ///
    /// - `y`: zero-based first row to delete.
    /// - `n`: number of rows to delete.
    /// - `bounds_bottom`: exclusive lower row bound for the affected region;
    ///   clamped to the buffer height.
    /// - `fill`: cell used to fill freed bottom rows.
    ///
    /// # Returns
    ///
    /// Nothing.
    ///
    /// # Panics
    ///
    /// Never panics.
    ///
    /// # Usage notes
    ///
    /// Calls with `y >= bounds_bottom` are no-ops. Deleting more rows than
    /// remain in the region is equivalent to deleting through
    /// `bounds_bottom`.
    pub fn delete_lines(&mut self, y: u16, n: u16, bounds_bottom: u16, fill: &Cell) {
        let bottom = bounds_bottom.min(self.height) as usize;
        let y_usize = y as usize;
        let n = n as usize;

        if y_usize >= bottom {
            return;
        }

        let n = n.min(bottom - y_usize);

        // Shift lines up
        for i in y_usize..bottom - n {
            self.swap_rows(i, i + n);
        }

        // Clear the bottom lines
        for i in bottom - n..bottom {
            fill_row(self, i as u16, fill);
        }
    }

    /// Insert cells within one row.
    ///
    /// Cells in `[pos.x, bounds_right)` on `pos.y` are pushed right by `n`.
    /// Cells pushed past `bounds_right` are discarded, and the newly opened
    /// slots starting at `pos.x` are filled with `fill`.
    ///
    /// # Parameters
    ///
    /// - `pos`: row and starting column for insertion.
    /// - `n`: number of cells to insert.
    /// - `bounds_right`: exclusive right column bound for the affected row
    ///   region; clamped to the buffer width.
    /// - `fill`: cell used to fill newly opened slots.
    ///
    /// # Returns
    ///
    /// Nothing.
    ///
    /// # Panics
    ///
    /// Never panics.
    ///
    /// # Usage notes
    ///
    /// Calls with `n == 0`, an out-of-bounds row, or `pos.x >= bounds_right`
    /// are no-ops. Width-1 fills are written directly into the freed span.
    /// Wide fills are routed through [`Buffer::set`] so primary and
    /// continuation columns are placed consistently.
    pub fn insert_cells(
        &mut self,
        pos: impl Into<Position>,
        n: u16,
        bounds_right: u16,
        fill: &Cell,
    ) {
        let pos = pos.into();
        let y = pos.y as usize;
        let x = pos.x as usize;
        let n = n as usize;
        let right = bounds_right.min(self.width) as usize;

        if y >= self.height() as usize || x >= right || n == 0 {
            return;
        }

        let n = n.min(right - x);

        let needs_wide_fill = fill.is_wide();
        {
            // Shift the row's `[x, right)` window right by `n` via an
            // in-place rotate. This is a `memmove`-shaped operation,
            // so we avoid an O(W-n) loop of `Cell::clone` plus a heap
            // allocation per non-empty hyperlink.
            let line = self.line_mut(pos.y).expect("row in range");
            line[x..right].rotate_right(n);

            if !needs_wide_fill {
                // Width-1 fill: single sweep over the freed region.
                // This overwrites any stray continuation marker the
                // rotate parked here, so no separate blank pre-pass is
                // needed.
                line[x..x + n].fill(fill.clone());
            } else {
                // Wide fill: raw-blank the freed slots so a parked
                // continuation can't fool `fill_range`'s `set()` calls
                // into walking back and clobbering an unrelated
                // primary cell.
                for cell in &mut line[x..x + n] {
                    *cell = Cell::BLANK;
                }
            }
        }

        if needs_wide_fill {
            // Wide fill: route through fill_range so primary /
            // continuation pairs are laid down correctly.
            fill_range(self, pos.y, x as u16, (x + n) as u16, fill);
        }
    }

    /// Delete cells within one row.
    ///
    /// Cells in `[pos.x + n, bounds_right)` on `pos.y` are pulled left by
    /// `n`. The freed slots at the right edge of the affected region are
    /// filled with `fill`.
    ///
    /// # Parameters
    ///
    /// - `pos`: row and starting column for deletion.
    /// - `n`: number of cells to delete.
    /// - `bounds_right`: exclusive right column bound for the affected row
    ///   region; clamped to the buffer width.
    /// - `fill`: cell used to fill freed right-edge slots.
    ///
    /// # Returns
    ///
    /// Nothing.
    ///
    /// # Panics
    ///
    /// Never panics.
    ///
    /// # Usage notes
    ///
    /// Calls with `n == 0`, an out-of-bounds row, or `pos.x >= bounds_right`
    /// are no-ops. Width-1 fills are written directly into the freed span.
    /// Wide fills are routed through [`Buffer::set`] so primary and
    /// continuation columns are placed consistently.
    pub fn delete_cells(
        &mut self,
        pos: impl Into<Position>,
        n: u16,
        bounds_right: u16,
        fill: &Cell,
    ) {
        let pos = pos.into();
        let y = pos.y as usize;
        let x = pos.x as usize;
        let n = n as usize;
        let right = bounds_right.min(self.width) as usize;

        if y >= self.height() as usize || x >= right || n == 0 {
            return;
        }

        let n = n.min(right - x);

        let needs_wide_fill = fill.is_wide();
        {
            // Shift the row's `[x, right)` window left by `n` via an
            // in-place rotate (memmove-shaped). Cleanup happens below
            // when the freed right-edge slots are filled: width-1 fills
            // overwrite parked continuations directly, and wide fills
            // blank before routing through fill_range.
            let line = self.line_mut(pos.y).expect("row in range");
            line[x..right].rotate_left(n);

            if !needs_wide_fill {
                // Width-1 fill: single sweep over the freed right-edge
                // region. Any stray continuation marker parked there by
                // the rotate is overwritten in this same pass.
                line[right - n..right].fill(fill.clone());
            } else {
                // Wide fill: raw-blank the freed slots before routing
                // through fill_range, so its `set()` calls don't walk
                // back into still-live shifted data.
                for cell in &mut line[right - n..right] {
                    *cell = Cell::BLANK;
                }
            }
        }

        if needs_wide_fill {
            fill_range(self, pos.y, (right - n) as u16, right as u16, fill);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::buffer::Surface;

    #[test]
    fn test_insert_lines() {
        let mut buf = Buffer::new(5, 5);
        buf.set((0, 0), &Cell::narrow("A"));
        buf.set((0, 1), &Cell::narrow("B"));
        buf.set((0, 2), &Cell::narrow("C"));

        buf.insert_lines(1, 2, 5, &Cell::BLANK);
        assert_eq!(buf.cell(Position::new(0, 0)).unwrap().content(), "A");
        assert!(buf.cell(Position::new(0, 1)).unwrap().is_blank());
        assert!(buf.cell(Position::new(0, 2)).unwrap().is_blank());
        assert_eq!(buf.cell(Position::new(0, 3)).unwrap().content(), "B");
        assert_eq!(buf.cell(Position::new(0, 4)).unwrap().content(), "C");
    }

    #[test]
    fn test_delete_lines() {
        let mut buf = Buffer::new(5, 5);
        buf.set((0, 0), &Cell::narrow("A"));
        buf.set((0, 1), &Cell::narrow("B"));
        buf.set((0, 2), &Cell::narrow("C"));
        buf.set((0, 3), &Cell::narrow("D"));

        buf.delete_lines(1, 2, 5, &Cell::BLANK);
        assert_eq!(buf.cell(Position::new(0, 0)).unwrap().content(), "A");
        assert_eq!(buf.cell(Position::new(0, 1)).unwrap().content(), "D");
        assert!(buf.cell(Position::new(0, 2)).unwrap().is_blank());
    }

    #[test]
    fn test_insert_cells() {
        let mut buf = Buffer::new(10, 1);
        buf.set((0, 0), &Cell::narrow("A"));
        buf.set((1, 0), &Cell::narrow("B"));
        buf.set((2, 0), &Cell::narrow("C"));

        buf.insert_cells((1, 0), 2, 10, &Cell::BLANK);
        assert_eq!(buf.cell(Position::new(0, 0)).unwrap().content(), "A");
        assert!(buf.cell(Position::new(1, 0)).unwrap().is_blank());
        assert!(buf.cell(Position::new(2, 0)).unwrap().is_blank());
        assert_eq!(buf.cell(Position::new(3, 0)).unwrap().content(), "B");
        assert_eq!(buf.cell(Position::new(4, 0)).unwrap().content(), "C");
    }

    #[test]
    fn test_delete_cells() {
        let mut buf = Buffer::new(10, 1);
        buf.set((0, 0), &Cell::narrow("A"));
        buf.set((1, 0), &Cell::narrow("B"));
        buf.set((2, 0), &Cell::narrow("C"));
        buf.set((3, 0), &Cell::narrow("D"));

        buf.delete_cells((1, 0), 2, 10, &Cell::BLANK);
        assert_eq!(buf.cell(Position::new(0, 0)).unwrap().content(), "A");
        assert_eq!(buf.cell(Position::new(1, 0)).unwrap().content(), "D");
        assert!(buf.cell(Position::new(2, 0)).unwrap().is_blank());
    }

    #[test]
    fn insert_cells_blanks_dangling_continuation_when_primary_shifted_off() {
        // A wide cell straddling the truncation boundary must not leave a
        // continuation marker behind once its primary is pushed past the
        // right edge.
        let mut buf = Buffer::new(6, 1);
        buf.set((0, 0), &Cell::narrow("A"));
        // Wide cell at columns 4-5 (primary at 4, continuation at 5).
        buf.set((4, 0), &Cell::wide("漢"));
        assert!(buf.cell(Position::new(5, 0)).unwrap().is_continuation());

        // Insert 1 cell at col 1: primary at 4 shifts to 5, continuation
        // (originally at 5) falls off. The new cell at col 5 is the wide
        // primary with no continuation room — set() truncates it to BLANK.
        buf.insert_cells((1, 0), 1, 6, &Cell::BLANK);
        assert_eq!(buf.cell(Position::new(0, 0)).unwrap().content(), "A");
        assert!(buf.cell(Position::new(1, 0)).unwrap().is_blank());
        // The wide cell's continuation that used to be at col 5 must not
        // remain as a stale marker.
        assert!(!buf.cell(Position::new(5, 0)).unwrap().is_continuation());
    }

    #[test]
    fn delete_cells_blanks_dangling_primary_at_fill_boundary() {
        // A wide cell straddling the fill region's left edge must have
        // its dangling primary cleaned up when the fill writes a blank
        // over its continuation half.
        let mut buf = Buffer::new(6, 1);
        buf.set((0, 0), &Cell::narrow("A"));
        // Wide cell at columns 4-5 (primary at 4, continuation at 5).
        buf.set((4, 0), &Cell::wide("漢"));
        assert!(buf.cell(Position::new(5, 0)).unwrap().is_continuation());

        // Delete 1 cell at col 0 (the "A"). Cells shift left so the wide
        // pair lands at cols 3-4. The fill at col 5 (now blank from the
        // shift) is written via set(); col 5 was the continuation prior
        // to the shift but is now arbitrary — what we really need to
        // verify is that no orphan continuations leak past the right
        // boundary.
        buf.delete_cells((0, 0), 1, 6, &Cell::BLANK);
        // Last column must be a clean blank, not a stray continuation.
        let last = buf.cell(Position::new(5, 0)).unwrap();
        assert!(!last.is_continuation());
        assert!(last.is_blank());
    }

    #[test]
    fn fill_with_wide_cell_steps_by_width() {
        // When the fill cell is itself wide, each primary must own its
        // continuation slot — no orphan primaries from stepping by 1.
        let mut buf = Buffer::new(5, 1);
        let wide = Cell::wide("漢");

        // Fill via insert_cells with n covering the whole row.
        buf.insert_cells((0, 0), 5, 5, &wide);
        // Cols 0,2 are wide primaries; 1,3 are continuations; 4 is a
        // plain blank (odd trailing slot can't fit another pair).
        assert_eq!(buf.cell(Position::new(0, 0)).unwrap().width(), 2);
        assert!(buf.cell(Position::new(1, 0)).unwrap().is_continuation());
        assert_eq!(buf.cell(Position::new(2, 0)).unwrap().width(), 2);
        assert!(buf.cell(Position::new(3, 0)).unwrap().is_continuation());
        assert!(buf.cell(Position::new(4, 0)).unwrap().is_blank());
        assert!(!buf.cell(Position::new(4, 0)).unwrap().is_continuation());
    }
}