nim-cli-utils/cliutils.nim

51 lines
1.4 KiB
Nim
Raw Normal View History

2022-01-21 18:04:40 -06:00
import nre, terminal, strtabs, tables, unicode
import strutils except toUpper, toLower
import ./cliutils/config
import ./cliutils/daemonize
import ./cliutils/procutil
2022-01-21 18:04:40 -06:00
export config
export daemonize
export procutil
2022-01-21 18:04:40 -06:00
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
2020-11-12 04:26:43 -06:00
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 queryParamsToCliArgs*(queryParams: Table[string, string]): seq[string] =
result = @[]
for k,v in queryParams:
# support ?arg1=val1&arg2=val2 -> cmd val1 val2
if k.startsWith("arg"): result.add(v)
else :
2020-03-23 08:18:08 -05:00
result.add("--" & k)
if v != "true": result.add(v) # support things like ?verbose=true -> cmd --verbose
proc queryParamsToCliArgs*(queryParams: StringTableRef): 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