Files
nim-cli-utils/cliutils/ansiterm.nim

113 lines
3.5 KiB
Nim

import std/[nre, sequtils]
const CSI = "\x1b["
const RESET_FORMATTING* = "\x1b[0m"
const ANSI_ESCAPE_CODE_ENDINGS*: seq[char] = toSeq('A'..'Z') & toSeq('a'..'z')
let FORMATTING_REGEX* = re("\x1b\\[([0-9;]*)([a-zA-Z])")
type
CursorType* = enum
ctBlockBlink = 1, ctBlock, ctUnderlineBlink, ctUnderline, ctBarBlink, ctBar
EraseMode* = enum
emToEnd, emToStart, emAll
TerminalColors* = enum
cBlack, cRed, cGreen, cYellow, cBlue, cMagenta, cCyan, cWhite
proc stripFormatting*(text: string): string =
text.replace(FORMATTING_REGEX, "")
proc stripAnsi*(text: string): string = stripFormatting(text)
func ansiAwareSubstring*(s: string, start, length: int): string =
result = ""
var curAnsiEscCode = ""
var i = 0
var visibleLen = 0
while i < len(s) and visibleLen < length:
if len(s) > i + len(CSI) and
# We need to notice ANSI escape codes...
s[i..<i + len(CSI)] == CSI:
var j = i + len(CSI)
while j < s.len and s[j] notin ANSI_ESCAPE_CODE_ENDINGS: j += 1
# and remember it if we're before the start of the substring
if i < start: curAnsiEscCode = s[i..j]
# or add it without increasing our substring length
else: result.add(s[i..j])
# either way we want to pick up after it
i = j
else:
result.add(s[i])
visibleLen += 1
i += 1
result = curAnsiEscCode & result
func color*(text: string, fg = cWhite, bg = cBlack): string =
return CSI & $int(fg) & ";" & $(int(bg) + 40) & "m" & text & RESET_FORMATTING
func color*(text: string, fg: TerminalColors): string =
return CSI & $int(fg) & "m" & text & RESET_FORMATTING
func color*(text: string, bg: TerminalColors): string =
return CSI & $(int(bg) + 40) & "m" & text & RESET_FORMATTING
func invert*(text: string): string = return CSI & "7m" & text & RESET_FORMATTING
func striketrough*(text: string): string = return CSI & "9m" & text & RESET_FORMATTING
func eraseDisplay*(mode = emToEnd): string = return CSI & $int(mode) & "J"
func eraseLine*(mode = emToEnd): string = return CSI & $int(mode) & "K"
func blinkSlow*(text: string): string = return CSI & "5m" & text & RESET_FORMATTING
func blinkFast*(text: string): string = return CSI & "6m" & text & RESET_FORMATTING
func cursorUp*(n: int = 1): string = return CSI & $int(n) & "A"
func cursorDown*(n: int = 1): string = return CSI & $int(n) & "B"
func cursorForward*(n: int = 1): string = return CSI & $int(n) & "C"
func cursorBackward*(n: int = 1): string = return CSI & $int(n) & "D"
func cursorNextLine*(n: int = 1): string = return CSI & $int(n) & "E"
func cursorPrevLine*(n: int = 1): string = return CSI & $int(n) & "F"
func cursorHorizontalAbsolute*(col: int): string = return CSI & $int(col) & "G"
func cursorPosition*(row, col: int): string =
return CSI & $int(row) & ";" & $int(col) & "H"
proc cursorType*(ct: CursorType): string = return CSI & $int(ct) & " q"
func saveCursorPosition*(): string = return CSI & "s"
func restoreCursorPosition*(): string = return CSI & "u"
func scrollUp*(n: int = 1): string = return CSI & $int(n) & "S"
func scrollDown*(n: int = 1): string = return CSI & $int(n) & "T"
proc write*(f: File, x, y: int, text: string) =
f.write(cursorPosition(y, x) & text)
proc write*(x, y: int, text: string) =
stdout.write(cursorPosition(y, x) & text)
proc setCursorType*(ct: CursorType) = stdout.write(cursorType(ct))
proc setCursorPosition*(x, y: int) = stdout.write(cursorPosition(y, x))
proc hideCursor*() = stdout.write(CSI & "?25l")
proc showCursor*() = stdout.write(CSI & "?25l")