From e447fc2b9226293d4eccd88e0a29363f8ffab8a9 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 30 Sep 2024 17:06:57 -0500 Subject: [PATCH] add convience cli generator --- config.nims | 2 +- src/hwylterm.nim | 2 + src/hwylterm/bbansi.nim | 45 ++++++++++----------- src/hwylterm/chooser.nim | 32 +++++++-------- src/hwylterm/cli.nim | 84 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 43 deletions(-) create mode 100644 src/hwylterm/cli.nim diff --git a/config.nims b/config.nims index 45e26af..050dd4e 100644 --- a/config.nims +++ b/config.nims @@ -24,7 +24,7 @@ task docs, "Deploy doc html + search index to public/ directory": when defined(clean): echo fmt"clearing {deployDir}" rmDir deployDir - for module in ["cligen", "chooser", "logging"]: + for module in ["cligen", "chooser", "logging", "cli"]: selfExec fmt"doc --docRoot:{getCurrentDir()}/src/ --index:on --outdir:{deployDir} src/hwylterm/{module}" selfExec fmt"doc --project --index:on {gitFlags} --outdir:{deployDir} --project src/{pkgName}.nim" docFixup(deployDir,pkgName) diff --git a/src/hwylterm.nim b/src/hwylterm.nim index 8bfa1fe..336ff7b 100644 --- a/src/hwylterm.nim +++ b/src/hwylterm.nim @@ -3,9 +3,11 @@ see also these utility modules with extra dependencies: + - [cli](./hwylterm/cli.html) - [cligen adapter](./hwylterm/cligen.html), requires [cligen](https://github.com/c-blake/cligen) - [chooser](./hwylterm/chooser.html) - [logging](./hwylterm/logging.html) + ]## import hwylterm/[spin, bbansi] diff --git a/src/hwylterm/bbansi.nim b/src/hwylterm/bbansi.nim index 3fc1928..869631e 100644 --- a/src/hwylterm/bbansi.nim +++ b/src/hwylterm/bbansi.nim @@ -224,33 +224,28 @@ proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} = # NOTE: could move to standlone modules in the tools/ directory when isMainModule: - import std/[parseopt, sugar] + import std/[parseopt] + import ./cli + const version = staticExec "git describe --tags --always --dirty=-dev" - const longOptPad = 8 - const flags = collect( - for (s, l, d) in [ - ("h", "help", "show this help"), - ("v", "version", "show version"), - ("s", "style", "set style for string"), - ]: - fmt" [yellow]-{s}[/] [green]--{l.alignLeft(longOptPad)}[/] {d}" - ).join("\n") + proc writeHelp() = - let help = - bbfmt""" -[bold]bbansi[/] \[[green]args...[/]] [[[faint]-h|-v[/]] - -[italic]usage[/]: - bbansi "[[yellow] yellow text!" - |-> [yellow] yellow text![/] - bbansi "[[bold red] bold red text[[/] plain text..." - |-> [bold red] bold red text[/] plain text... - bbansi "[[red]some red[[/red] but all italic" --style:italic - |-> [italic][red]some red[/red] but all italic[/italic] - -flags: -{flags} -""" + let help = $newHwylCli( + "[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-v[/]]", + """ +bbansi "[[yellow] yellow text!" + -> [yellow] yellow text![/] +bbansi "[[bold red] bold red text[[/] plain text..." + -> [bold red] bold red text[/] plain text... +bbansi "[[red]some red[[/red] but all italic" --style:italic + -> [italic][red]some red[/red] but all italic[/italic] +""", + [ + ("h", "help", "show this help"), + ("v", "version", "show version"), + ("s", "style", "set style for string"), + ] + ) echo help; quit 0 proc testCard() = diff --git a/src/hwylterm/chooser.nim b/src/hwylterm/chooser.nim index cce53fd..e421436 100644 --- a/src/hwylterm/chooser.nim +++ b/src/hwylterm/chooser.nim @@ -128,24 +128,22 @@ proc choose*[T](things: openArray[T], height: Natural = 6): seq[T] = when isMainModule: import std/[parseopt, strformat] - func styleFlag(s, l, d: string): string = - fmt" [yellow]-{s}[/] [green]--{l.alignLeft(10)}[/] {d}" + import ./cli + proc writeHelp() = - const flags = [ - styleFlag("h", "help", "show this help"), - styleFlag("s", "seperator", "seperator to split items"), - ].join("\n") - echo bbfmt""" -[bold]hwylchoose[/] \[[green]args...[/]] \[[faint]-h[/]] - -[italic]usage[/]: - hwylchoose a b c d - hwylchoose a,b,c,d -s, - hwylchoose a,b,c,d --seperator "," - -flags: -{flags} -""" + let cli = newHwylCli( + "[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]", + """ +hwylchoose a b c d +hwylchoose a,b,c,d -s, +hwylchoose a,b,c,d --seperator "," +""", + [ + ("h", "help", "show this help"), + ("s", "seperator", "seperator to split items"), + ] + ) + echo cli var posArgs: seq[string] diff --git a/src/hwylterm/cli.nim b/src/hwylterm/cli.nim new file mode 100644 index 0000000..b217510 --- /dev/null +++ b/src/hwylterm/cli.nim @@ -0,0 +1,84 @@ +##[ + # Cli +]## + +import std/[strutils] +import ./bbansi + +type + HwylFlag = tuple + short, long, description = "" + HwylCliStyles* = object + hdr = "b cyan" + shortFlag = "yellow" + longFlag = "magenta" + descFlag = "" + HwylCli* = object + cmd*: string + usage*: string + flags*: seq[HwylFlag] + styles*: HwylCliStyles + shortArgLen, longArgLen, descArgLen: int + + +func newHwylCli*( + cmd = "", + usage = "", + flags: openArray[HwylFlag] = @[], + styles = HwylCliStyles() +): HwylCli = + result.cmd = cmd + result.usage = usage + result.flags = @flags + result.styles = styles + for f in flags: + result.shortArgLen = max(result.shortArgLen, f.short.len) + result.longArgLen = max(result.longArgLen, f.long.len) + result.descArgLen = max(result.descArgLen, f.description.len) + + +func flagHelp(cli: HwylCli, f: HwylFlag): string = + result.add " " + if f.short != "": + result.add "[" & cli.styles.shortFlag & "]" + result.add "-" & f.short.alignLeft(cli.shortArgLen) + result.add "[/]" + else: + result.add " ".repeat(1 + cli.shortArgLen) + + result.add " " + if f.long != "": + result.add "[" & cli.styles.longFlag & "]" + result.add "--" & f.long.alignLeft(cli.longArgLen) + result.add "[/]" + else: + result.add " ".repeat(2 + cli.longArgLen) + + result.add " " + if f.description != "": + result.add "[" & cli.styles.descFlag & "]" + result.add f.description + result.add "[/]" + result.add "\n" + +proc bbImpl(cli: HwylCli): string = + if cli.cmd != "": + result.add cli.cmd + result.add "\n" + if cli.usage != "": + result.add "\n" + result.add "[" & cli.styles.hdr & "]" + result.add "usage[/]:\n" + result.add indent(cli.usage, 2 ) + result.add "\n" + if cli.flags.len > 0: + result.add "[" & cli.styles.hdr & "]" + result.add "flags[/]:\n" + for f in cli.flags: + result.add flagHelp(cli,f) + +proc bb*(cli: HwylCli): BbString = + result = bb(bbImpl(cli)) + +proc `$`*(cli: HwylCli): string = + result = $bb(cli)