Terminal colors, text attributes, and composable styles.
All helpers return strings and have no terminal or global-state side effects.
Types
ColorPlane = enum cpForeground, cpBackground
- Selects whether a color affects text or its background. Source Edit
TerminalColor = object kind*: TerminalColorKind index*: uint8 red*, green*, blue*: uint8
-
A foreground or background color supported by modern terminals.
Components not used by kind remain zero. A regular object is used instead of a case object so color values remain safe in sequences and closure captures across all supported Nim 2.x compilers.
Source Edit TerminalColorKind = enum tckDefault, tckAnsi16, tckAnsi256, tckRgb
- Encoding used by a TerminalColor. Source Edit
TerminalStyle = object foreground*: TerminalColor background*: TerminalColor attributes*: set[TextAttribute]
- A reusable foreground, background, and attribute combination. Source Edit
TextAttribute = enum taBold, taDim, taItalic, taUnderline, taBlink, taRapidBlink, taReverse, taConceal, taStrikethrough, taOverline
- Independently composable SGR text attributes. Source Edit
Consts
colorBlack = (kind: tckAnsi16, index: 0'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightBlack = (kind: tckAnsi16, index: 8'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightBlue = (kind: tckAnsi16, index: 12'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightCyan = (kind: tckAnsi16, index: 14'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightGreen = (kind: tckAnsi16, index: 10'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightMagenta = (kind: tckAnsi16, index: 13'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightRed = (kind: tckAnsi16, index: 9'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightWhite = (kind: tckAnsi16, index: 15'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorBrightYellow = (kind: tckAnsi16, index: 11'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorDefault = (kind: tckDefault, index: 0'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorGreen = (kind: tckAnsi16, index: 2'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorMagenta = (kind: tckAnsi16, index: 5'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorWhite = (kind: tckAnsi16, index: 7'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
colorYellow = (kind: tckAnsi16, index: 3'u, red: 0'u, green: 0'u, blue: 0'u)
- Source Edit
termBgBlack = "\e[40m"
- Source Edit
termBgBlue = "\e[44m"
- Source Edit
termBgBrightBlack = "\e[100m"
- Source Edit
termBgBrightBlue = "\e[104m"
- Source Edit
termBgBrightCyan = "\e[106m"
- Source Edit
termBgBrightGreen = "\e[102m"
- Source Edit
termBgBrightMagenta = "\e[105m"
- Source Edit
termBgBrightRed = "\e[101m"
- Source Edit
termBgBrightWhite = "\e[107m"
- Source Edit
termBgBrightYellow = "\e[103m"
- Source Edit
termBgCyan = "\e[46m"
- Source Edit
termBgGreen = "\e[42m"
- Source Edit
termBgMagenta = "\e[45m"
- Source Edit
termBgWhite = "\e[47m"
- Source Edit
termBgYellow = "\e[43m"
- Source Edit
termBrightBlack = "\e[90m"
- Source Edit
termBrightBlue = "\e[94m"
- Source Edit
termBrightCyan = "\e[96m"
- Source Edit
termBrightGreen = "\e[92m"
- Source Edit
termBrightMagenta = "\e[95m"
- Source Edit
termBrightRed = "\e[91m"
- Source Edit
termBrightWhite = "\e[97m"
- Source Edit
termBrightYellow = "\e[93m"
- Source Edit
termConceal = "\e[8m"
- Source Edit
termHidden = "\e[8m"
- Source Edit
termItalic = "\e[3m"
- Source Edit
termMagenta = "\e[35m"
- Source Edit
termNegative = "\e[7m"
- Source Edit
termOverline = "\e[53m"
- Source Edit
termRapidBlink = "\e[6m"
- Source Edit
termReverse = "\e[7m"
- Source Edit
termStrikethrough = "\e[9m"
- Source Edit
termUnderline = "\e[4m"
- Source Edit
termYellow = "\e[33m"
- Source Edit
Procs
proc `==`(left, right: TerminalColor): bool {....raises: [], tags: [], forbids: [].}
- Compares colors by encoding and component values. Source Edit
proc ansi16Color(index: range[0 .. 15]): TerminalColor {....raises: [], tags: [], forbids: [].}
- Constructs one of the 16 standard terminal colors. Source Edit
proc ansiCode(attribute: TextAttribute): string {....raises: [], tags: [], forbids: [].}
- Returns the SGR escape sequence for one text attribute. Source Edit
proc ansiCode(color: TerminalColor; plane = cpForeground): string {....raises: [], tags: [], forbids: [].}
- Returns an SGR escape sequence for color and plane. Source Edit
proc ansiCode(textStyle: TerminalStyle): string {....raises: [], tags: [], forbids: [].}
- Returns one combined SGR sequence for all configured style properties. Source Edit
proc applyStyle(value: string; textStyle: TerminalStyle; enabled = true): string {. ...raises: [], tags: [], forbids: [].}
- Applies textStyle. Disabling it also removes existing ANSI controls. Source Edit
proc background(color: TerminalColor; values: varargs[string, `$`]): string {. ...raises: [], tags: [], forbids: [].}
- Applies an arbitrary background color. Source Edit
proc bgBrightBlack(values`gensym31: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc bgBrightBlue(values`gensym35: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc bgBrightCyan(values`gensym37: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc bgBrightGreen(values`gensym33: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc bgBrightMagenta(values`gensym36: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc bgBrightRed(values`gensym32: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc bgBrightWhite(values`gensym38: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc bgBrightYellow(values`gensym34: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc brightBlack(values`gensym15: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc brightBlue(values`gensym19: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc brightCyan(values`gensym21: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc brightGreen(values`gensym17: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc brightMagenta(values`gensym20: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc brightWhite(values`gensym22: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc brightYellow(values`gensym18: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc foreground(color: TerminalColor; values: varargs[string, `$`]): string {. ...raises: [], tags: [], forbids: [].}
- Applies an arbitrary foreground color. Source Edit
proc hexColor(value: string): TerminalColor {....raises: [ValueError], tags: [], forbids: [].}
- Parses #RGB, RGB, #RRGGBB, or RRGGBB as true color. Source Edit
proc indexedColor(index: range[0 .. 255]): TerminalColor {....raises: [], tags: [], forbids: [].}
- Constructs an ANSI-256 palette color. Source Edit
proc initTerminalStyle(foreground = colorDefault; background = colorDefault; attributes: set[TextAttribute] = {}): TerminalStyle {. ...raises: [], tags: [], forbids: [].}
- Creates a reusable style. Default colors are left unchanged. Source Edit
proc rapidBlink(values`gensym44: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc rgbColor(red, green, blue: range[0 .. 255]): TerminalColor {....raises: [], tags: [], forbids: [].}
- Constructs a 24-bit true-color value. Source Edit
proc strikethrough(values`gensym49: varargs[string, $]): string {....raises: [], tags: [], forbids: [].}
- Source Edit
proc style(value: string; textStyle: TerminalStyle; enabled = true): string {. ...raises: [], tags: [], forbids: [].}
- Concise alias for applyStyle. Source Edit
proc styled(textStyle: TerminalStyle; values: varargs[string, `$`]): string {. ...raises: [], tags: [], forbids: [].}
- Concatenates heterogeneous values and applies textStyle. Source Edit