From d105e821f823b8cf7f9dafcde06450fcacf35f12 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 18 Sep 2024 13:27:01 -0500 Subject: [PATCH] feat: add cligen adapter --- .gitignore | 3 ++ config.nims | 5 ++++ hwylterm.nimble | 5 ---- src/hwylterm/bbansi.nim | 23 ++++---------- src/hwylterm/bbansi/styles.nim | 1 + src/hwylterm/bbansi/utils.nim | 29 +++++++++++++++--- src/hwylterm/cli.nim | 55 ++++++++++++++++++++++++++++++++++ src/hwylterm/utils.nim | 22 ++++++++++++++ todo.md | 3 +- 9 files changed, 119 insertions(+), 27 deletions(-) create mode 100644 src/hwylterm/cli.nim create mode 100644 src/hwylterm/utils.nim diff --git a/.gitignore b/.gitignore index a912f72..8b8da25 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ tests/* !tests/*.nim !tests/*.nims +nimble.develop +nimble.paths +nimbledeps diff --git a/config.nims b/config.nims index 9b62011..a1bd7b3 100644 --- a/config.nims +++ b/config.nims @@ -1,2 +1,7 @@ task test, "run tests": selfExec "r tests/tbbansi.nim" + +task develop, "install cligen for development": + exec "nimble install -l 'cligen@1.7.5'" + +--path:"./nimbledeps/pkgs2/cligen-1.7.5-f3ffe7329c8db755677d3ca377d02ff176cec8b1" diff --git a/hwylterm.nimble b/hwylterm.nimble index ad01601..84d537d 100644 --- a/hwylterm.nimble +++ b/hwylterm.nimble @@ -1,12 +1,7 @@ -# Package - version = "0.1.0" author = "Daylin Morgan" description = "bringing some fun (hwyl) to the terminal" license = "MIT" srcDir = "src" - -# Dependencies - requires "nim >= 2.0.8" diff --git a/src/hwylterm/bbansi.nim b/src/hwylterm/bbansi.nim index 6a085d0..8b70861 100644 --- a/src/hwylterm/bbansi.nim +++ b/src/hwylterm/bbansi.nim @@ -4,22 +4,11 @@ use BB style markup to add color to strings using VT100 escape codes ]## -import std/[os, sequtils, strutils, terminal] +import std/[os, sequtils, strutils] -import bbansi/[styles, utils] - -proc checkColorSupport(): bool = - when defined(bbansiNoColor): - return true - else: - if os.getEnv("HWYLTERM_FORCE_COLOR") != "": - return false - if os.getEnv("NO_COLOR") != "": - return true - if not isatty(stdout): - return true - -let noColor = checkColorSupport() +import ./bbansi/[styles, utils] +export utils +export bbReset type BbSpan* = object @@ -152,7 +141,7 @@ func len*(bbs: BbString): int = bbs.plain.len proc `$`*(bbs: BbString): string = - if noColor: + if bbMode == Off: return bbs.plain for span in bbs.spans: @@ -164,7 +153,7 @@ proc `$`*(bbs: BbString): string = result.add bbs.plain[span.slice[0] .. span.slice[1]] if codes != "": - result.add bbReset + result.add toAnsiCode("reset") proc `&`*(x: BbString, y: BbString): Bbstring = # there is probably a more efficient way to do this diff --git a/src/hwylterm/bbansi/styles.nim b/src/hwylterm/bbansi/styles.nim index b3ec781..0625f3c 100644 --- a/src/hwylterm/bbansi/styles.nim +++ b/src/hwylterm/bbansi/styles.nim @@ -5,6 +5,7 @@ export tables const bbReset* = "\e[0m" bbStyles* = { + "reset": "0", "bold": "1", "b": "1", "faint": "2", diff --git a/src/hwylterm/bbansi/utils.nim b/src/hwylterm/bbansi/utils.nim index c537086..9a9430d 100644 --- a/src/hwylterm/bbansi/utils.nim +++ b/src/hwylterm/bbansi/utils.nim @@ -1,8 +1,27 @@ -import std/[strutils] +import std/[os, strutils, terminal] +import ./styles -import styles +type + BbMode* = enum + On, NoColor, Off + +proc checkColorSupport(): BbMode = + when defined(bbansiOff): + return Off + when defined(bbansiNoColor): + return NoColor + else: + if os.getEnv("HWYLTERM_FORCE_COLOR") != "": + return On + if os.getEnv("NO_COLOR") != "": + return NoColor + if not isatty(stdout): + return Off + +let bbMode* = checkColorSupport() proc toAnsiCode*(s: string): string = + if bbMode == Off: return var codes: seq[string] styles: seq[string] @@ -16,12 +35,14 @@ proc toAnsiCode*(s: string): string = for style in styles: if style in bbStyles: codes.add bbStyles[style] - elif style in bbColors: + elif style in bbColors and bbMode == On: codes.add "3" & bbColors[style] - if bgStyle in bbColors: + if bgStyle in bbColors and bbMode == On: codes.add "4" & bbColors[bgStyle] if codes.len > 0: result.add "\e[" result.add codes.join ";" result.add "m" + + diff --git a/src/hwylterm/cli.nim b/src/hwylterm/cli.nim new file mode 100644 index 0000000..840f32e --- /dev/null +++ b/src/hwylterm/cli.nim @@ -0,0 +1,55 @@ +## adapter to add hwylterm colors to cligen output +import std/[tables] +import cligen +import ./bbansi + + +type + CligenStyle = object + cmd: string = "bold cyan" + descrip: string = "" + dflval: string = "yellow" + optkeys: string = "green" + valtype: string = "red" + args: string = "italic" + + +proc helpAttr(styles: CligenStyle): Table[string, string] = + { + "cmd" : toAnsiCode(styles.cmd), + "clDescrip": toAnsiCode(styles.descrip), + "clDflVal" : toAnsiCode(styles.dflval), + "clOptKeys": toAnsiCode(styles.optkeys), + "clValType": toAnsiCode(styles.valtype), + "args" : toAnsiCode(styles.args) + }.toTable() + +proc helpAttrOff(): Table[string,string] = + let resetCode = toAnsiCode("reset") + { + "cmd" : resetCode, + "clDescrip": resetCode, + "clDflVal" : resetCode, + "clOptKeys": resetCode, + "clValType": resetCode, + "args" : resetCode, + }.toTable() + +proc hwylCli*( + clcfg: var ClCfg, + styles: CligenStyle = CligenStyle(), + useMulti: string = "${doc}[bold]Usage[/]:\n $command {SUBCMD} [[sub-command options & parameters]\n\n[bold]subcommands[/]:\n$subcmds", + useHdr: string = "[bold]usage[/]:\n " +) = + + if clCfg.useMulti == "": + clCfg.useMulti = $bb(useMulti) + + if clCfg.helpAttr.len == 0: + clCfg.helpAttr = styles.helpAttr() + clCfg.helpAttrOff = helpAttrOff() + + # clCfg.use does nothing? + if clCfg.useHdr == "": + clCfg.useHdr = $bb(useHdr) + diff --git a/src/hwylterm/utils.nim b/src/hwylterm/utils.nim new file mode 100644 index 0000000..8318ec4 --- /dev/null +++ b/src/hwylterm/utils.nim @@ -0,0 +1,22 @@ +import std/[os, terminal] + +type + BbMode* = enum + On, NoColor, Off + +proc checkColorSupport(): BbMode = + when defined(bbansiOff): + return Off + when defined(bbansiNoColor): + return NoColor + else: + if os.getEnv("HWYLTERM_FORCE_COLOR") != "": + return On + if os.getEnv("NO_COLOR") != "": + return NoColor + if not isatty(stdout): + return Off + +let bbMode* = checkColorSupport() + + diff --git a/todo.md b/todo.md index 2c428bb..6479772 100644 --- a/todo.md +++ b/todo.md @@ -2,6 +2,7 @@ - [ ] revamp spinner api - [ ] add basic progress bar -- [ ] add cligen adapters to add colors with bbansi +- [x] add cligen adapters to add colors with bbansi + - [ ] add integration test check cligen