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; any
13/// trailing slot too narrow to fit another pair is a plain blank.
14pub(crate) fn fill_line_into(slot: &mut [Cell], fill: &Cell) {
15 let width = slot.len();
16 if fill.width() <= 1 {
17 slot.fill(fill.clone());
18 return;
19 }
20 let step = fill.width() as usize;
21 let mut x = 0;
22 while x + step <= width {
23 slot[x] = fill.clone();
24 for i in 1..step {
25 slot[x + i] = Cell::continuation();
26 }
27 x += step;
28 }
29 while x < width {
30 slot[x] = Cell::BLANK;
31 x += 1;
32 }
33}
34
35/// Create a new line of the given `width` filled with `fill`. Wide
36/// fills (`fill.width() > 1`) lay down primary + continuation pairs; any
37/// trailing slot too narrow to fit another pair is a plain blank.
38pub fn fill_line(width: u16, fill: &Cell) -> Line {
39 let mut line = vec![Cell::BLANK; width as usize];
40 fill_line_into(&mut line, fill);
41 line
42}
43
44/// Create a new blank line of the given width.
45pub fn blank_line(width: u16) -> Line {
46 vec![Cell::BLANK; width as usize]
47}