mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-21 02:30:44 -06:00
feat: add cligen adapter
This commit is contained in:
parent
0ecd7d222d
commit
d105e821f8
9 changed files with 119 additions and 27 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,6 @@
|
||||||
tests/*
|
tests/*
|
||||||
!tests/*.nim
|
!tests/*.nim
|
||||||
!tests/*.nims
|
!tests/*.nims
|
||||||
|
nimble.develop
|
||||||
|
nimble.paths
|
||||||
|
nimbledeps
|
||||||
|
|
|
@ -1,2 +1,7 @@
|
||||||
task test, "run tests":
|
task test, "run tests":
|
||||||
selfExec "r tests/tbbansi.nim"
|
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"
|
||||||
|
|
|
@ -1,12 +1,7 @@
|
||||||
# Package
|
|
||||||
|
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
author = "Daylin Morgan"
|
author = "Daylin Morgan"
|
||||||
description = "bringing some fun (hwyl) to the terminal"
|
description = "bringing some fun (hwyl) to the terminal"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
srcDir = "src"
|
srcDir = "src"
|
||||||
|
|
||||||
|
|
||||||
# Dependencies
|
|
||||||
|
|
||||||
requires "nim >= 2.0.8"
|
requires "nim >= 2.0.8"
|
||||||
|
|
|
@ -4,22 +4,11 @@
|
||||||
use BB style markup to add color to strings using VT100 escape codes
|
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]
|
import ./bbansi/[styles, utils]
|
||||||
|
export utils
|
||||||
proc checkColorSupport(): bool =
|
export bbReset
|
||||||
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()
|
|
||||||
|
|
||||||
type
|
type
|
||||||
BbSpan* = object
|
BbSpan* = object
|
||||||
|
@ -152,7 +141,7 @@ func len*(bbs: BbString): int =
|
||||||
bbs.plain.len
|
bbs.plain.len
|
||||||
|
|
||||||
proc `$`*(bbs: BbString): string =
|
proc `$`*(bbs: BbString): string =
|
||||||
if noColor:
|
if bbMode == Off:
|
||||||
return bbs.plain
|
return bbs.plain
|
||||||
|
|
||||||
for span in bbs.spans:
|
for span in bbs.spans:
|
||||||
|
@ -164,7 +153,7 @@ proc `$`*(bbs: BbString): string =
|
||||||
result.add bbs.plain[span.slice[0] .. span.slice[1]]
|
result.add bbs.plain[span.slice[0] .. span.slice[1]]
|
||||||
|
|
||||||
if codes != "":
|
if codes != "":
|
||||||
result.add bbReset
|
result.add toAnsiCode("reset")
|
||||||
|
|
||||||
proc `&`*(x: BbString, y: BbString): Bbstring =
|
proc `&`*(x: BbString, y: BbString): Bbstring =
|
||||||
# there is probably a more efficient way to do this
|
# there is probably a more efficient way to do this
|
||||||
|
|
|
@ -5,6 +5,7 @@ export tables
|
||||||
const
|
const
|
||||||
bbReset* = "\e[0m"
|
bbReset* = "\e[0m"
|
||||||
bbStyles* = {
|
bbStyles* = {
|
||||||
|
"reset": "0",
|
||||||
"bold": "1",
|
"bold": "1",
|
||||||
"b": "1",
|
"b": "1",
|
||||||
"faint": "2",
|
"faint": "2",
|
||||||
|
|
|
@ -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 =
|
proc toAnsiCode*(s: string): string =
|
||||||
|
if bbMode == Off: return
|
||||||
var
|
var
|
||||||
codes: seq[string]
|
codes: seq[string]
|
||||||
styles: seq[string]
|
styles: seq[string]
|
||||||
|
@ -16,12 +35,14 @@ proc toAnsiCode*(s: string): string =
|
||||||
for style in styles:
|
for style in styles:
|
||||||
if style in bbStyles:
|
if style in bbStyles:
|
||||||
codes.add bbStyles[style]
|
codes.add bbStyles[style]
|
||||||
elif style in bbColors:
|
elif style in bbColors and bbMode == On:
|
||||||
codes.add "3" & bbColors[style]
|
codes.add "3" & bbColors[style]
|
||||||
if bgStyle in bbColors:
|
if bgStyle in bbColors and bbMode == On:
|
||||||
codes.add "4" & bbColors[bgStyle]
|
codes.add "4" & bbColors[bgStyle]
|
||||||
|
|
||||||
if codes.len > 0:
|
if codes.len > 0:
|
||||||
result.add "\e["
|
result.add "\e["
|
||||||
result.add codes.join ";"
|
result.add codes.join ";"
|
||||||
result.add "m"
|
result.add "m"
|
||||||
|
|
||||||
|
|
||||||
|
|
55
src/hwylterm/cli.nim
Normal file
55
src/hwylterm/cli.nim
Normal 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
22
src/hwylterm/utils.nim
Normal 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()
|
||||||
|
|
||||||
|
|
3
todo.md
3
todo.md
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
- [ ] revamp spinner api
|
- [ ] revamp spinner api
|
||||||
- [ ] add basic progress bar
|
- [ ] 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 -->
|
<!-- generated with <3 by daylinmorgan/todo -->
|
||||||
|
|
Loading…
Reference in a new issue