nim-cli-utils/cliutils.nim

48 lines
1.3 KiB
Nim

import std/[nre, terminal, strtabs, tables, unicode]
import strutils except toUpper, toLower
import ./cliutils/config
import ./cliutils/daemonize
import ./cliutils/procutil
import ./cliutils/queue_logger
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 withColor*(str: string, color: TermColor, bright, bold = false): string =
return termColor(color, bright, bold) & str
proc stripAnsi*(str: string): string =
let STRIP_ANSI_REGEX = re"\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]"
return str.replace(STRIP_ANSI_REGEX, "")
proc doParseQueryParams[T](queryParams: T): seq[string] =
result = @[]
for k,v in queryParams:
# support ?arg1=val1&arg2=val2 -> cmd val1 val2
if k.startsWith("arg"): result.add(v)
else :
result.add("--" & k)
if v != "true": result.add(v) # support things like ?verbose=true -> cmd --verbose
proc queryParamsToCliArgs*(queryParams: Table[string, string]): seq[string] =
doParseQueryParams(queryParams)
proc queryParamsToCliArgs*(queryParams: StringTableRef): seq[string] =
doParseQueryParams(queryParams)