Skip to content
CryoCryo home

terminal

import std::io::terminal; · source

Colours and text attributes written as ANSI escape sequences, and whether a standard stream should get them. A Style names a foreground and background Color and the attributes bold, dim, italic, and underline; paint wraps a Display value so that printing it writes the style, the value, and a reset.

A style with nothing set writes the value alone, so a program picks its styles once, from Terminal::supports_color, and prints the same way either way:

import std::fmt;
import std::io::terminal::{ Color, StandardStream, Style, Styled, Terminal };

mut error: Style = Style::new();
if (Terminal::supports_color(StandardStream::Error)) {
    error = Style::new().foreground(Color::Red).bold();
}
const label: Styled<Str> = error.paint(Str::new("error"));
fmt::eprintln(f"{label}: file not found");

Types

Style

type struct Style with [Eq, Clone, Debug] {
    private foreground_color: Option<Color>;
    private background_color: Option<Color>;
    private is_bold:          boolean;
    private is_dim:           boolean;
    private is_italic:        boolean;
    private is_underlined:    boolean;
}

Built up from Style::new(), which sets nothing; each builder method takes the style by value and returns it with one more thing set. is_plain() is true while nothing is set. write_start and write_end write the escape sequences that turn the style on and reset it, for a Display implementation that styles part of its own output; both write nothing for a plain style. The start sequence resets first, so the text gets exactly this style whatever was on before it.

Constructors

static new() -> Style;

A style that sets nothing: painting with it writes the value alone.

Methods

foreground(mut this, color: Color) -> Style;

This style with the text in color.

background(mut this, color: Color) -> Style;

This style with the cells behind the text in color.

bold(mut this) -> Style;

This style in bold.

dim(mut this) -> Style;

This style dimmed, as the terminal renders faint text.

italic(mut this) -> Style;

This style in italics, where the terminal has them.

underline(mut this) -> Style;

This style underlined.

is_plain(&this) -> boolean;

True when the style sets nothing, so it writes no escape sequences.

paint<D>(&this, value: D) -> Styled<D> where D: Display;

value, to be printed in this style: its Display writes the escape sequence that turns the style on, the value, and the one that resets every style.

write_start(&this, f: mut &Formatter) -> Result<(), FormatError>;

Write the escape sequence that turns this style on; nothing for a plain style. The sequence resets first (ESC[0;...m), so the text gets exactly this style whatever was on before it.

write_end(&this, f: mut &Formatter) -> Result<(), FormatError>;

Write the escape sequence that resets every style; nothing for a plain style, which turned none on.

Styled<D>

type struct Styled<D> where D: Display {
    style: Style;
    value: D;
}

A value with the style it is printed in, made by Style.paint. Its Display writes the start sequence, the value, and the reset.

Color

type enum Color with [Eq, Clone, Debug] {
    Black;
    Red;
    Green;
    Yellow;
    Blue;
    Magenta;
    Cyan;
    White;
    BrightBlack;
    BrightRed;
    BrightGreen;
    BrightYellow;
    BrightBlue;
    BrightMagenta;
    BrightCyan;
    BrightWhite;
}

The sixteen colours every ANSI terminal has: eight, and a bright form of each. The terminal's theme decides the exact shade. foreground_code() and background_code() are the SGR parameters that select the colour.

Variants

Variant
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White
BrightBlack
BrightRed
BrightGreen
BrightYellow
BrightBlue
BrightMagenta
BrightCyan
BrightWhite

Methods

foreground_code(&this) -> u8;

The SGR parameter that sets this colour as the foreground; the background's is 10 more.

background_code(&this) -> u8;

The SGR parameter that sets this colour as the background.

StandardStream

type enum StandardStream with [Eq, Clone, Debug] {
    Output;
    Error;
}

The two standard streams text is styled for.

Terminal

type struct Terminal {}

supports_color(stream) answers whether text written to stream should be styled: the stream is a terminal, NO_COLOR is unset or empty, and TERM is not dumb. On Windows it also turns on the console's escape-sequence processing, and answers false where the console refuses it.

Static functions

static supports_color(stream: StandardStream) -> boolean;

Whether text written to stream should be styled: the stream is a terminal, NO_COLOR is unset or empty, and TERM is not dumb. On Windows this also turns on the console's escape-sequence processing, and answers false where the console refuses it.

Trait implementations

implement<D> trait Display for struct Styled<D> where D: Display