Skip to main content

Module style

Module style 

Source
Expand description

Text style values and terminal SGR/OSC 8 rendering.

§Style as a value

Style is an owned description of how text should look: optional foreground/background colors, optional underline color, an UnderlineStyle, an AttrFlags bitset, and an optional OSC 8 Link. Builder methods take and return Self, so styles can be composed fluently and then cloned into cells or spans.

§Open/close versus wrapped rendering

The std::fmt::Display implementation for Style emits the opener: the SGR sequence (CSI … m), followed by the OSC 8 hyperlink start when the style carries a link. The opener does not reset the terminal or close the link afterward; following output remains in that style until another style or reset is written.

The alternate form ({style:#}) emits the matching closer: the OSC 8 hyperlink terminator (when the style carries a link) followed by the SGR reset (CSI m). Used together, {style} and {style:#} wrap a complete span with a single style value. The SGR reset clears to defaults rather than restoring a previously active style, so wrap each span independently:

without link: ┌─────────┐ ┌──────┐ ┌───────┐
              │ CSI … m │▶│ text │▶│ CSI m │
              └─────────┘ └──────┘ └───────┘
with link:    ┌─────────┐ ┌───────┐ ┌──────┐ ┌───────────┐ ┌───────┐
              │ CSI … m │▶│ OSC 8 │▶│ text │▶│ OSC 8 end │▶│ CSI m │
              └─────────┘ └───────┘ └──────┘ └───────────┘ └───────┘

§Attributes and underline

Boolean SGR attributes such as bold, italic, blinking, reverse video, conceal, and strikethrough live in AttrFlags. Underlining is modeled separately with UnderlineStyle because SGR supports multiple underline shapes (4, 4:2, 4:3, 4:4, 4:5) and an independent underline color.

§SGR encoding

Style emission uses a single CSI … m sequence for all SGR state. Standard foreground/background colors use 3037/4047, bright colors use 9097/100107, indexed colors use 38;5;n/48;5;n, true color uses 38;2;r;g;b/48;2;r;g;b, and underline color uses the colon subparameter form (58:5:n or 58:2::r:g:b).

ESC [   1 ;   4:3  ;    38;2;255;128;0  ; 58:2::0:255:255     m
└─┬─┘ └─────────────── SGR parameters ─────────────────────┘ └┬┘
 CSI  attrs  underline   fg truecolor     ul color          final
use uncurses::color::Color;
use uncurses::style::Style;

// `{style}` writes the opener; `{style:#}` writes the matching closer.
let heading = Style::default().bold().fg(Color::Green);
println!("{heading}Hello{heading:#}");

let link = Style::default()
    .underline()
    .link("https://example.com", "");
println!("{link}docs{link:#}");

Structs§

AttrFlags
Bitflags for SGR text attributes.
Link
OSC 8 hyperlink target carried by a Style.
Style
A complete terminal text style.

Enums§

UnderlineStyle
Underline shape encoded in SGR underline parameters.