@std/cli/style.ms#
Import each required symbol with import Name from "@std/cli/style.ms".
Color Enum#
A terminal color: the terminal default, a 16-color index, a 256-color palette index,
or a 24-bit RGB value. paint downgrades richer colors to what the terminal
supports.
enum Color {
Default
Ansi(index Int)
Indexed(index Int)
Rgb(red Int, green Int, blue Int)
}Color.Ansi#
One of the 16 ANSI colors; values outside 0 to 15 are clamped.
Ansi(index Int)Color.Default#
The terminal’s own default color, which emits no color sequence.
DefaultColor.Indexed#
An entry of the 256-color palette; values outside 0 to 255 are clamped, and terminals below 256 colors get the nearest of 16.
Indexed(index Int)Color.Rgb#
A 24-bit color; terminals with fewer than 16777216 colors get the nearest color they support.
Rgb(red Int, green Int, blue Int)Color.Ansi.index#
0 to 7 normal colors, 8 to 15 bright colors.
index IntColor.Indexed.index#
0 to 255 in the 256-color palette.
index IntColor.Rgb.blue#
Blue, 0 to 255.
blue IntColor.Rgb.green#
Green, 0 to 255.
green IntColor.Rgb.red#
Red, 0 to 255.
red IntStyle Type#
Colors and attributes of a run of text.
type Style {
foreground Color = Color.Default
background Color = Color.Default
bold Bool = false
dim Bool = false
italic Bool = false
underline Bool = false
reverse Bool = false
strikethrough Bool = false
}Style.background#
Background color; Color.Default leaves the terminal’s own background.
background Color = Color.DefaultStyle.bold#
Heavier weight when true; defaults to false.
bold Bool = falseStyle.dim#
Reduced intensity when true; defaults to false.
dim Bool = falseStyle.foreground#
Text color; Color.Default leaves the terminal’s own foreground.
foreground Color = Color.DefaultStyle.italic#
Slanted text when true; defaults to false.
italic Bool = falseStyle.reverse#
Swaps foreground and background when true; defaults to false.
reverse Bool = falseStyle.strikethrough#
Struck-through text when true; defaults to false.
strikethrough Bool = falseStyle.underline#
Underlined text when true; defaults to false.
underline Bool = falsepaint Function#
Wraps text in style and a reset. Returns text unchanged when colors is 0 or
the style is plain.
fn paint(text String, style Style, colors Int) String { ... }strip_ansi Function#
Removes escape sequences from text.
fn strip_ansi(text String) String { ... }style_start Function#
The escape sequence that starts style for a terminal with colors colors, or ""
when it changes nothing.
fn style_start(style Style, colors Int) String { ... }styled Function#
Like paint, using the color count of the current terminal.
fn styled(text String, style Style) String { ... }truncate_text Function#
Cuts the single line text to at most width visible columns, keeping escape
sequences and ending a styled cut with a reset.
fn truncate_text(text String, width Int) String { ... }visible_width Function#
The number of terminal columns text occupies, ignoring escape sequences. Any other
control character returns EBADMSG.
fn visible_width(text String) Result[Int, Error] { ... }wrap_text Function#
Splits text on newlines and wraps each line into rows of at most width columns, counting only visible
characters. Escape sequences stay with the text they style: a row that breaks inside styled text ends with a
reset and the next row repeats the active color and style sequences. Other sequences, such as window titles
and hyperlinks, are kept whole and count for no columns. A width below 1 is treated as 1.
fn wrap_text(text String, width Int) List[String] { ... }