Skip to main content

uncurses/buffer/
line.rs

1//! Owned-row helpers and the [`Line`] type alias used by callers that
2//! need an allocated row (rather than a slice into a [`Buffer`]).
3
4use crate::cell::Cell;
5
6/// A single line of cells, used by helpers that allocate owned rows
7/// (e.g. [`fill_line`]). Buffer accessors return slices (`&[Cell]`) so
8/// callers don't pay an extra dereference through a per-row `Vec`.
9pub type Line = Vec<Cell>;
10
11/// Fill an existing row slot in place with `fill`. Wide fills
12/// (`fill.width() > 1`) lay down primary + continuation pairs whose
13/// continuation carries the fill's style; any trailing slot too narrow to
14/// fit another pair is a plain blank.
15pub(crate) fn fill_line_into(slot: &mut [Cell], fill: &Cell) {
16    let width = slot.len();
17    if fill.width() <= 1 {
18        slot.fill(fill.clone());
19        return;
20    }
21    let step = fill.width() as usize;
22    // A continuation is the rest of the cell that owns it, so it answers
23    // for that cell's colours too. Leaving it default paints a hole in the
24    // second column of every pair when the fill carries a background.
25    let mut cont = Cell::CONTINUATION;
26    cont.copy_style_from(fill);
27    let mut x = 0;
28    while x + step <= width {
29        slot[x] = fill.clone();
30        for i in 1..step {
31            slot[x + i] = cont.clone();
32        }
33        x += step;
34    }
35    while x < width {
36        slot[x] = Cell::BLANK;
37        x += 1;
38    }
39}
40
41/// Create a new line of the given `width` filled with `fill`. Wide
42/// fills (`fill.width() > 1`) lay down primary + continuation pairs; any
43/// trailing slot too narrow to fit another pair is a plain blank.
44pub fn fill_line(width: u16, fill: &Cell) -> Line {
45    let mut line = vec![Cell::BLANK; width as usize];
46    fill_line_into(&mut line, fill);
47    line
48}
49
50/// Create a new blank line of the given width.
51pub fn blank_line(width: u16) -> Line {
52    vec![Cell::BLANK; width as usize]
53}