diff --git a/cliutils.nim b/cliutils.nim index d581198..9598ffb 100644 --- a/cliutils.nim +++ b/cliutils.nim @@ -1,40 +1,17 @@ -import std/[nre, terminal, strtabs, tables, unicode] -import strutils except toUpper, toLower +import std/[strtabs, strutils, tables] +import ./cliutils/ansiterm import ./cliutils/config import ./cliutils/daemonize import ./cliutils/procutil import ./cliutils/queue_logger +export ansiterm export config export daemonize export procutil export queue_logger -type - TermColor = ForegroundColor or BackgroundColor - -const termReset* = "\e[0;m" - -proc termColor*(color: TermColor, bright, bold = false): string = - var colorVal = ord(color) - if bright: inc(colorVal, 60) - return "\e[" & $colorVal & (if bold: ";1" else: "") & "m" - - -proc termFmt*(str: string, color: TermColor, bright, bold, underline, skipReset = false): string = - result = termColor(color, bright, bold) & str - if underline: result = "\e[4m" & result - if not skipReset: result &= termReset - -proc withColor*(str: string, color: TermColor, bright, bold, skipReset = false): string = - termFmt(str, color, bright, bold, false, skipReset) - -proc stripAnsi*(str: string): string = - let STRIP_ANSI_REGEX = re"\x1B\[([0-9]{1,2}(;[0-9]{0,2})?)?[m|K]" - return str.replace(STRIP_ANSI_REGEX, "") - - proc doParseQueryParams[T](queryParams: T): seq[string] = result = @[] diff --git a/cliutils.nimble b/cliutils.nimble index a45958f..755a4eb 100644 --- a/cliutils.nimble +++ b/cliutils.nimble @@ -1,6 +1,6 @@ # Package -version = "0.10.2" +version = "0.11.0" author = "Jonathan Bernard" description = "Helper functions for writing command line interfaces." license = "MIT" diff --git a/cliutils/ansiterm.nim b/cliutils/ansiterm.nim new file mode 100644 index 0000000..3e49df7 --- /dev/null +++ b/cliutils/ansiterm.nim @@ -0,0 +1,112 @@ +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..