Cells

A terminal screen is not a canvas of free pixels. It is a grid of fixed slots, each one character tall and (usually) one character wide. uncurses calls one slot a cell, and the cell is the atomic unit of everything you draw.

What a cell holds

Every cell carries three things: the grapheme to show (if any), how it should look, and how many columns it occupies.

flowchart TB
  accTitle: Cell content, style, and width
  accDescr {
    A cell stores a grapheme or empty content, colors and attributes,
    and the number of columns it occupies.
  }
  cell["A cell"] --> txt["content: grapheme or empty"]
  cell --> look["style: colors and attributes"]
  cell --> cols["width: columns occupied"]

For visible cells, the content is a single grapheme. That means “one character” the way a human counts it, even when it is several Unicode code points stitched together (think e plus a combining accent, or a flag emoji). The style is color and attributes like bold or underline. The width is the interesting part.

Narrow, wide, and continuation

Most cells are narrow: one grapheme, one column. Some graphemes are wider. A CJK character like 世 wants two columns, not one. uncurses models that as a primary cell holding the grapheme, followed by a continuation placeholder for each further column. A continuation has no content of its own and reports width zero, because its column belongs to the primary on its left.

A primary owns as many columns as its grapheme measures, and holds one continuation for each column after the first.

row / col123
row 1世contA

One terminal row. The wide glyph 世 is a primary cell in column 1 with a zero-width continuation cell in column 2, and the narrow A sits in column 3.

You almost never create a continuation by hand: writing a wide grapheme into a grid lays down the primary and its continuations together. When you do build a row yourself, Cell::CONTINUATION is the placeholder to put in each column after the first. The Width page digs into how uncurses decides what is narrow and what is wide, and why getting it wrong smears a whole row.

The blank cell

What is an empty terminal slot, a place showing nothing at all? A blank cell: a single space painted in the default style. uncurses calls that Cell::BLANK. It is what a freshly allocated grid is full of, and what clearing a cell puts back.

Building a cell

Construct a cell with Cell::new and specify its content and width. Use with_style and with_link to set its presentation in either order. Use style() to read or copy its style. Use set_style() to change an existing cell. This sample targets the current source checkout:

rust
use std::sync::Arc;
use uncurses::cell::Cell;
use uncurses::color::Color;
use uncurses::style::{Link, Style};

fn main() {
    let mut cell = Cell::new("a", 1)
        .with_style(Style::new().bold().fg(Color::Green))
        .with_link(Arc::new(Link {
            url: "https://example.com".into(),
            params: String::new(),
        }));
    assert_eq!(cell.width(), 1);
    assert_eq!(cell.style().fg, Some(Color::Green));

    let mut style = cell.style();
    style.bg = Some(Color::Blue);
    cell.set_style(style);
    assert_eq!(cell.style().bg, Some(Color::Blue));
    assert_eq!(cell.link().unwrap().url, "https://example.com");
    cell.set_link(None);
    assert!(cell.link().is_none());

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

Style is a small, copyable visual value. A cell stores its shared hyperlink separately. set_style() preserves that link. link() borrows the shared handle, and set_link(None) clears it.

Measure the width rather than guessing it: WidthMode::grapheme_width gives the number the terminal will advance, and passing that keeps the grid and the screen in agreement.

A single cell is not very useful on its own. The next step is a whole grid of them: see Buffers.

Edit this page

Search documentation

Type to search the documentation.