Skip to main content

Module cell

Module cell 

Source
Expand description

Terminal cell values and grapheme segmentation.

This module defines Cell, the value stored by buffers and surfaces. A cell combines grapheme content, visual style, and the number of terminal columns the content occupies.

§Cell value type

A Cell stores three pieces of state:

  • content: the grapheme cluster to display. Continuation cells have no content of their own.
  • style: colors, attributes, underline data, and link metadata applied to the cell.
  • width: the cell’s column footprint, returned by Cell::width.

Width also encodes the cell’s structural role. A width of 1 or more marks a primary cell that owns that many columns. A width of 0 marks a continuation placeholder, whose column is owned by the primary to its left.

§Construction

Construct every cell with Cell::new, passing the content and the number of columns it occupies. Measure that width with WidthMode::grapheme_width rather than assuming it. The constructor uses the default Style; use Cell::style() to attach a style afterwards.

Cell::CONTINUATION is the placeholder that stands in the columns after a multi-column grapheme. Most callers never write one; writing a multi-column cell through Buffer::set or SurfaceMut::set_cell creates the placeholders automatically.

ⓘ
use uncurses::cell::Cell;

let a = Cell::new("a", 1);
assert_eq!(a.width(), 1);

let wide = Cell::new("中", 2);
assert_eq!(wide.width(), 2);

§Multi-column cells

A grapheme wider than one column occupies that many adjacent grid columns. The leftmost column stores the primary and every column after it stores a continuation placeholder:

col:    0       1       2
      ┌───────┬───────┬───┐
row 0 │ 中    │ cont. │ A │
      └───────┴───────┴───┘
        width=2 width=0 width=1

Most graphemes are one or two columns wide. A cell may be wider: under WidthMode::Wc a joined emoji sequence measures the sum of its code points, so a four-person family emoji is eight columns and owns seven continuations.

Continuations preserve the one-Cell-per-column layout for wide graphemes. Cell::is_blank identifies a cell that claims one column and draws a space, regardless of style. A continuation claims zero columns, so it returns false.

Structs§

Cell
A single terminal-grid cell.