feat: add cligen adapter

This commit is contained in:
Daylin Morgan 2024-09-18 13:27:01 -05:00
parent 0ecd7d222d
commit d105e821f8
Signed by: daylin
GPG key ID: 950D13E9719334AD
9 changed files with 119 additions and 27 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
tests/*
!tests/*.nim
!tests/*.nims
nimble.develop
nimble.paths
nimbledeps

View file

@ -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"

View file

@ -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"

View file

@ -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

View file

@ -5,6 +5,7 @@ export tables
const
bbReset* = "\e[0m"
bbStyles* = {
"reset": "0",
"bold": "1",
"b": "1",
"faint": "2",

View file

@ -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"

55
src/hwylterm/cli.nim Normal file
View file

@ -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)

22
src/hwylterm/utils.nim Normal file
View file

@ -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()

View file

@ -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
<!-- generated with <3 by daylinmorgan/todo -->