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 cont = Cell::CONTINUATION.style(fill.style.clone());
26    let mut x = 0;
27    while x + step <= width {
28        slot[x] = fill.clone();
29        for i in 1..step {
30            slot[x + i] = cont.clone();
31        }
32        x += step;
33    }
34    while x < width {
35        slot[x] = Cell::BLANK;
36        x += 1;
37    }
38}
39
40/// Create a new line of the given `width` filled with `fill`. Wide
41/// fills (`fill.width() > 1`) lay down primary + continuation pairs; any
42/// trailing slot too narrow to fit another pair is a plain blank.
43pub fn fill_line(width: u16, fill: &Cell) -> Line {
44    let mut line = vec![Cell::BLANK; width as usize];
45    fill_line_into(&mut line, fill);
46    line
47}
48
49/// Create a new blank line of the given width.
50pub fn blank_line(width: u16) -> Line {
51    vec![Cell::BLANK; width as usize]
52}