Skip to content
FutureBlitz

    Install

    Import std.term.style

    std.term.style

    std.term's styles: a TermColor for text, one for what's behind it, and TextAttributes, in a Style, which a terminal shows through the escape codes Apply writes, in as many colors as it shows, the nearest of them where it shows fewer. Paint styles text for the console, which gets the text alone where it isn't a terminal or NO_COLOR is set. StripStyles and VisibleWidth read text that has codes in it.

    std/term/style.fb

    Types

    ColorLevel enum

    Public Enum ColorLevel

    How many colors a terminal shows.

    std/term/style.fb, line 18

    • NONE enum member #

      None, as a pipe or a file, or a terminal where NO_COLOR is set, which gets no styles at all.

    • BASIC enum member #

      The 16 basic ones.

    • INDEXED enum member #
    • TRUE_COLOR enum member #

      24-bit color.

    ColorKind enum

    Public Enum ColorKind

    What a TermColor is.

    std/term/style.fb, line 30

    • NORMAL enum member #

      The terminal's own color.

    • BASIC enum member #

      One of the 16 basic ones.

    • INDEXED enum member #

      One of 256 indexed ones.

    • RGB enum member #

      Red, green, and blue, 24-bit.

    TermColor struct

    Public Struct TermColor Implements Hashable, Printable

    A color for text, or for what's behind it: the terminal's NORMAL one, its zero; one of the 16 BASIC ones or 256 INDEXED ones, by its Index; or RGB's R, G, and B. A terminal that shows fewer colors gets the nearest it has, as Nearest gives it.

    std/term/style.fb, line 44

    • Public ReadOnly Field Kind: ColorKind field #

      Whether it's the terminal's own, a basic one, an indexed one, or RGB.

    • Public ReadOnly Field Index: UInt8 field #

      A BASIC or INDEXED color's index.

    • Public ReadOnly Field R: UInt8 field #

      An RGB color's red.

    • Public ReadOnly Field G: UInt8 field #

      An RGB color's green.

    • Public ReadOnly Field B: UInt8 field #

      An RGB color's blue.

    • Public Static Property Normal: TermColor property #

      The terminal's own color, for text or what's behind it.

    • Public Static Property Black: TermColor property #

      Black, a basic color.

    • Public Static Property Red: TermColor property #

      Red, a basic color.

    • Public Static Property Green: TermColor property #

      Green, a basic color.

    • Public Static Property Yellow: TermColor property #

      Yellow, a basic color.

    • Public Static Property Blue: TermColor property #

      Blue, a basic color.

    • Public Static Property Magenta: TermColor property #

      Magenta, a basic color.

    • Public Static Property Cyan: TermColor property #

      Cyan, a basic color.

    • Public Static Property White: TermColor property #

      White, a basic color.

    • Public Static Property BrightBlack: TermColor property #

      Bright black, which most terminals show as gray.

    • Public Static Property BrightRed: TermColor property #

      Bright red, a basic color.

    • Public Static Property BrightGreen: TermColor property #

      Bright green, a basic color.

    • Public Static Property BrightYellow: TermColor property #

      Bright yellow, a basic color.

    • Public Static Property BrightBlue: TermColor property #

      Bright blue, a basic color.

    • Public Static Property BrightMagenta: TermColor property #

      Bright magenta, a basic color.

    • Public Static Property BrightCyan: TermColor property #

      Bright cyan, a basic color.

    • Public Static Property BrightWhite: TermColor property #

      Bright white, a basic color.

    • @PanicsAtCaller Public Static Method Basic(index: Int): TermColor static method #

      One of the 16 basic colors by its number: black, red, green, yellow, blue, magenta, cyan, and white from 0 to 7, and their bright ones from 8 to 15. Panics past 15.

    • Public Static Method Indexed(index: UInt8): TermColor static method #

      One of 256 colors by its number: the 16 basic ones, then a cube of 6 reds by 6 greens by 6 blues from 16 to 231, and 24 grays, darkest first, from 232 to 255.

    • Public Static Method Rgb(r: UInt8, g: UInt8, b: UInt8): TermColor static method #

      Red, green, and blue, from 0 to 255 each.

    • @PanicsAtCaller Public Static Method Hex(text: String): TermColor static method #

      The color hex text gives, as CSS writes one: "#rgb" or "#rrggbb", with or without the "#", in either case. Any other text panics; TryHex takes text from outside the program.

    • Public Static Method TryHex(text: String): TermColor? static method #

      The color hex text gives, as Hex takes it, or Null for text that isn't one.

    • Public Method ToRgb(): (Int, Int, Int) method #

      Its red, green, and blue: an RGB color's own, and for another, what xterm shows it as. The terminal's NORMAL color has none of its own, so it's black's.

    • Public Method Nearest(level: ColorLevel): TermColor method #

      The color as a terminal that shows level's colors shows it: itself, where it has it, or the nearest one it has, by red, green, and blue. NONE has none, so it's NORMAL there.

    • Public Method ToString(): String method #

      "normal", "red", "bright red", "color 208", or "#ff8800".

    TextAttributes enum

    @Flags Public Enum TextAttributes

    How text looks, beside its colors, in any mix. Not every terminal shows every one.

    std/term/style.fb, line 265

    • NONE = 0 enum member #

      None.

    • BOLD enum member #

      Bold.

    • DIM enum member #

      Dim.

    • ITALIC enum member #

      Italic.

    • UNDERLINE enum member #

      Underlined.

    • REVERSE enum member #

      The colors swapped.

    • HIDDEN enum member #

      Hidden.

    • STRIKETHROUGH enum member #

      Struck through.

    Style struct

    Public Struct Style Implements Hashable, Printable

    How text looks: its Foreground color, the Background color behind it, and its Attributes. Its zero is plain text, as the terminal shows it, and Style(TermColor.Red).Bold() is bold red.

    std/term/style.fb, line 289

    • Public Field Foreground: TermColor field #

      Its text's color.

    • Public Field Background: TermColor field #

      The color behind its text.

    • Public Field Attributes: TextAttributes field #

      Its attributes, like BOLD, which combine.

    • Public Property IsPlain: Bool property #

      Whether it's plain text, as the terminal shows it.

    • Public Method On(background: TermColor): Style method #

      A copy with background behind the text.

    • Public Method With(attributes: TextAttributes): Style method #

      A copy with attributes too.

    • Public Method Bold(): Style method #

      A copy that's bold too.

    • Public Method Dim(): Style method #

      A copy that's dim too.

    • Public Method Italic(): Style method #

      A copy that's italic too.

    • Public Method Underline(): Style method #

      A copy that's underlined too.

    • Public Method Reverse(): Style method #

      A copy with its colors swapped too.

    • Public Method Hidden(): Style method #

      A copy that's hidden too.

    • Public Method Strikethrough(): Style method #

      A copy that's struck through too.

    • Public Method Over(under: Style): Style method #

      This style over under: its own colors, and under's where it has the terminal's NORMAL ones, and both's attributes, as a list's selected line is drawn over its own style.

    • Public Method Codes(level: ColorLevel): String method #

      The escape code that starts it, from plain text, as a terminal of level's colors shows it, like "\e[0;1;31m", or "" where level is NONE. A plain style is "\e[0m".

    • Public Method Apply(text: String, level: ColorLevel): String method #

      text as this style shows it on a terminal of level's colors: between the codes that start it and the one that ends it, or alone where level is NONE or the style is plain.

    • Public Method ToString(): String method #

      Like "bold red on blue", or "plain".

    Functions

    ConsoleColors function

    Public Function ConsoleColors(stream: StandardStream = StandardStream.OUTPUT): ColorLevel

    How many colors stream shows, which Paint goes by: NONE where it isn't a terminal, or NO_COLOR is set and isn't empty, or TERM is "dumb"; TRUE_COLOR where COLORTERM is "truecolor" or "24bit", or on Windows' console; INDEXED where TERM has "256color" in it; and BASIC otherwise.

    std/term/style.fb, line 375

    Paint function

    Public Function Paint(text: String, style: Style): String

    text in style, for stdout: with the codes that show it where stdout is a terminal that shows colors, and alone where it isn't, or NO_COLOR is set, as Print(Paint("done", Style(TermColor.Green))).

    std/term/style.fb, line 382

    Paint function

    Public Function Paint(text: String, style: Style, stream: StandardStream): String

    text in style, for stream, as stream shows colors.

    std/term/style.fb, line 387

    StripStyles function

    Public Function StripStyles(text: String): String

    text without the escape codes in it: a CSI's, like a style's "\e[1;31m" or a cursor move; an OSC's, like a link's, or a DCS's, an APC's, a PM's, or an SOS's, like a sixel image's or kitty's graphics', up to its BEL or ST; and any other escape and the characters it takes, as "\e(B" or "\e7". A code cut short at the end goes, as far as it goes.

    std/term/style.fb, line 395

    VisibleWidth function

    Public Function VisibleWidth(text: String): Int

    The columns text takes in a terminal, as DisplayWidth counts them, without its escape codes. Each line's are counted alike, so for text of several lines it's the widest one's.

    std/term/style.fb, line 413

    Report an issue with this page or with FutureBlitz on GitHub.