terminal_style/ansi

Search:
Group by:
Source   Edit  

ANSI escape-sequence tokenization and composition.

The tokenizer recognizes complete CSI sequences (including SGR colors), OSC sequences terminated by BEL or ST, and two-byte escape sequences. Incomplete or malformed escapes are returned as ordinary text instead of silently consuming the remainder of a log line.

Types

AnsiToken = object
  kind*: AnsiTokenKind
  value*: string
One lossless token from an ANSI-bearing string. Source   Edit  
AnsiTokenKind = enum
  atkText, atkCsi, atkOsc, atkEscape
The kind of a token returned by tokenizeAnsi. Source   Edit  

Consts

ansiEscape = '\e'
Source   Edit  
ansiReset = "\e[0m"
Source   Edit  

Procs

proc composeAnsi(opening, value: string; enabled = true): string {....raises: [],
    tags: [], forbids: [].}
Wraps value in an SGR opening sequence. When nested content resets its style, the outer sequence is reapplied. RGB components and palette indices retain their color meaning when zero. Style parameters following a reset in the same SGR sequence are preserved. With enabled = false all complete ANSI controls in value are removed. Source   Edit  
proc stripAnsi(value: string): string {....raises: [], tags: [], forbids: [].}
Removes complete ANSI control sequences. Malformed sequences are retained verbatim, making the operation safe for arbitrary application input. Source   Edit  
proc tokenizeAnsi(value: string): seq[AnsiToken] {....raises: [], tags: [],
    forbids: [].}
Splits value without losing any bytes. Only complete ANSI sequences become control tokens; malformed or unterminated sequences remain text. Source   Edit