Styling text
Every bit of text you draw can carry a visual Style: foreground and
background color, attributes like bold and italic, and an underline shape and
color. A Pen can also carry a shared OSC 8 link.
Building a style
Style::new() (or Style::default()) starts empty: no color, no attributes, no
underline. Builder methods return an updated style, so you chain
only what you want.
use uncurses::color::Color;
use uncurses::style::{Style, UnderlineStyle};
let heading = Style::new()
.bold()
.fg(Color::BrightCyan);
let warning = Style::new()
.fg(Color::Rgb(255, 105, 180))
.bg(Color::Black)
.italic();
let spelling = Style::new()
.underline()
.underline_style(UnderlineStyle::Curly)
.underline_color(Color::BrightRed);Attributes include bold, faint, italic, underline, reverse,
strikethrough, conceal, blink, and rapid_blink. Underline shapes go
beyond a single line: double, curly, dotted, and dashed, and each can have an
underline color.
Colors
Colors come in three representations, and you can use them freely:
use uncurses::color::Color;
use uncurses::style::Style;
Style::new().fg(Color::Green); // 16-color
Style::new().fg(Color::Indexed(208)); // 256-color palette
Style::new().fg(Color::Rgb(255, 105, 180)); // 24-bit truecolor
You always specify the color you mean. When a Screen renders to a terminal
that cannot do truecolor, the renderer downsamples to the nearest supported
color through its color profile; you do
not branch on terminal capability yourself.
A Program detects the color profile during initialization and applies it to its
screen. If you construct a bare Screen with Screen::new, choose the profile
yourself when you need deterministic output. set_color_profile is a plain,
infallible setter on Screen; it writes nothing and affects later renders.
use uncurses::color::Profile;
use uncurses::screen::Screen;
let mut screen = Screen::new(Vec::new(), (20, 1));
screen.set_color_profile(Profile::Ansi256);Painting styled text
On a Screen, TextBuffer, or any other TextSurface, pass a Style or
Pen value as the third argument to set_str:
use uncurses::screen::Screen;
use uncurses::style::Style;
use uncurses::text::TextSurface;
let mut screen = Screen::new(Vec::new(), (20, 4));
let heading = Style::new().bold();
let warning = Style::new().italic();
screen.set_str((2, 1), "Heading", heading);
screen.set_str((2, 3), "be careful", warning);set_str paints the text literally: the style applies to the whole run, and any
escape sequences in the string are drawn as visible characters, not interpreted.
Use Pen when the whole run also needs
a hyperlink. The styles example shows both cell-level links and linked text
painting.
Inline escapes with a Painter
Painter and ANSI text utilities accept UTF-8 C1 controls in input strings.
For example, \u{9b}1m sets bold, like \x1b[1m.
The renderer writes cell styles with ordinary terminal controls.
Screen::insert_above also converts UTF-8 C1 framing to 7-bit forms before
it measures and writes inserted content.
Use 7-bit escapes when you write raw commands to a terminal.
Run cargo run -p examples --example ansi_text to compare both input encodings.
Sometimes the string you want to draw already contains SGR or OSC 8 escapes,
perhaps from another program, a log line with ANSI codes, or a snippet you
assembled by hand. A Painter
interprets those inline escapes and turns them into cell styles, where the
default set_str would draw them literally.
Wrap any TextSurface, such as a Screen or TextBuffer, in a Painter and
paint through it:
use uncurses::screen::Screen;
use uncurses::style::Style;
use uncurses::text::{Painter, TextSurface};
let mut screen = Screen::new(Vec::new(), (40, 2));
Painter::new(&mut screen).set_str(
(2, 1),
"plain \x1b[1;32mbold green\x1b[0m back to plain",
Style::new(),
);The \x1b[1;32m and \x1b[0m are read as Painter walks the string, so
“bold green” lands bold and green while the rest stays plain. The Style you
pass is the base: inline escapes layer on top of it, winning where they set a
field and letting the base fill in the rest. An inline reset (\x1b[0m) clears
the inline state, so the cells after it fall back to your base rather than to
the terminal default. Had you passed Style::new().fg(Color::Blue) above,
“back to plain” would return blue, not colorless.
The painter keeps no style between calls: every set_str starts fresh from the
base you hand it, so calls never bleed into each other.
Hyperlinks
This sample targets the current source checkout.
A cell stores its OSC 8 hyperlink separately from its visual Style.
Use Cell::with_link to move a shared Arc<Link> into a new cell.
Use Cell::set_link to change an existing cell.
Cell::link borrows that handle. set_style preserves the link.
Use set_link(None) to clear it.
use std::sync::Arc;
use uncurses::buffer::{SurfaceMut, TextBuffer};
use uncurses::cell::Cell;
use uncurses::color::Color;
use uncurses::style::{Link, Style};
use uncurses::text::{Encode, Painter, TextSurface};
let mut buffer = TextBuffer::new(30, 2);
let docs = Style::new().underline().fg(Color::BrightBlue);
let mut cell = Cell::new("D", 1)
.with_style(docs)
.with_link(Arc::new(Link {
url: "https://github.com/aymanbagabas/uncurses".into(),
params: "id=docs".into(),
}));
cell.set_style(docs.bold());
assert!(cell.link().is_some());
buffer.set_cell((0, 0).into(), &cell);
cell.set_link(None);
buffer.set_cell((1, 0).into(), &cell);
Painter::new(&mut buffer).set_str(
(0, 1),
"\x1b]8;id=docs;https://github.com/aymanbagabas/uncurses\x1b\\uncurses\x1b]8;;\x1b\\",
docs,
);
let mut output = Vec::new();
buffer.encode(&mut output).unwrap();Link::params holds the OSC 8 parameter string, usually empty.
id=docs lets capable terminals group separate runs as one link.
Painter parses OSC 8 in strings, as the second row shows.
Each call starts with the link from its base Pen.
An inline OSC 8 link replaces the active link, and an OSC 8 close clears it.
An SGR reset preserves the active link.
For text without inline escapes, pass a Pen value to set_str or another
text painting method. It applies the visual style and shared link to each
painted cell. Build it with Pen::new().with_style(docs).with_link(link).
Truncation methods accept separate pens for the main text and tail.
Styling plain output
Off the grid, a Style also implements Display: formatting it writes the
SGR opener. The alternate form {style:#} writes that style’s closer, so styled
println! follows an open/close pattern with a single value:
use uncurses::style::Style;
let bold = Style::new().bold();
println!("{bold}important{bold:#}");The opener is additive, so an empty style writes nothing. The closer only emits
what it needs: a visual style closes with CSI m.
The closer resets that span to defaults; it does
not restore an outer style or the terminal’s previous state.
This works with any io::Write too, since write! forwards Display:
use std::io::Write;
use uncurses::style::Style;
fn main() -> std::io::Result<()> {
let mut out = Vec::new();
let bold = Style::new().bold();
write!(out, "{bold}important{bold:#}")?;
Ok(())
}See the styles example in examples/examples/styles.rs for a full tour of
attributes, underline shapes, colors, and a clickable hyperlink.
