Compare commits

..

6 commits

6 changed files with 74 additions and 43 deletions

View file

@ -149,6 +149,7 @@ func stripAnsi*(s: string): string =
type
BbSpan* = object
styles*: seq[string]
# TODO: use actual slice?
slice*: array[2, int]
BbString* = object
@ -160,17 +161,24 @@ func shift(s: BbSpan, i: Natural): BbSpan =
inc(result.slice[0],i)
inc(result.slice[1],i)
proc len(span: BbSpan): int =
proc size(span: BbSpan): int =
span.slice[1] - span.slice[0]
# TODO: make sure we don't get non-existent spans?
template endSpan(bbs: var BbString) =
if bbs.spans.len == 0:
return
if bbs.plain.len >= 1:
bbs.spans[^1].slice[1] = bbs.plain.len - 1
if bbs.spans[^1].len == 0 and bbs.plain.len == 0:
if bbs.plain.len == bbs.spans[^1].slice[0]:
bbs.spans.delete(bbs.spans.len - 1)
elif bbs.plain.len >= 1:
bbs.spans[^1].slice[1] = bbs.plain.len - 1
# I think this is covered by the first condition now?
# if bbs.spans[^1].size == 0 and bbs.plain.len == 0:
# bbs.spans.delete(bbs.spans.len - 1)
proc newSpan(bbs: var BbString, styles: seq[string] = @[]) =
bbs.spans.add BbSpan(styles: styles, slice: [bbs.plain.len, 0])
@ -312,7 +320,7 @@ func alignLeft*(bs: BbString, count: Natural, padding = ' '): Bbstring =
func slice(bs: BbString, span: BbSpan): string =
bs.plain[span.slice[0]..span.slice[1]]
proc truncate*(bs: Bbstring, len: Natural): Bbstring =
func truncate*(bs: Bbstring, len: Natural): Bbstring =
if bs.len < len: return bs
for span in bs.spans:
if span.slice[0] >= len: break
@ -325,7 +333,7 @@ proc truncate*(bs: Bbstring, len: Natural): Bbstring =
result.spans.add span
result.plain.add bs.slice(span)
proc `&`*(x: BbString, y: BbString): Bbstring =
func `&`*(x: BbString, y: BbString): Bbstring =
result.plain.add x.plain
result.spans.add x.spans
result.plain.add y.plain
@ -333,7 +341,13 @@ proc `&`*(x: BbString, y: BbString): Bbstring =
for span in y.spans:
result.spans.add shift(span, i)
proc bbEscape*(s: string): string {.inline.} =
func add*(x: var Bbstring, y :Bbstring) =
let i = x.plain.len
x.plain.add y.plain
for span in y.spans:
x.spans.add shift(span, i)
func bbEscape*(s: string): string {.inline.} =
s.replace("[", "[[").replace("\\", "\\\\")
proc bbEcho*(args: varargs[string, `$`]) {.raises: [IOError]} =
@ -369,8 +383,9 @@ when isMainModule:
hwylCli:
name "bbansi"
settings ShowHelp
usage "[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-V[/]]"
description """
help:
usage "[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-V[/]]"
description """
bbansi "[[yellow] yellow text!"
-> [yellow] yellow text![/]
bbansi "[[bold red] bold red text[[/] plain text..."

View file

@ -131,13 +131,14 @@ when isMainModule:
hwylcli:
name "hwylchoose"
settings ShowHelp
usage "[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]"
description """
hwylchoose a b c d
hwylchoose a,b,c,d -s ,
hwylchoose a,b,c,d --seperator ","
hwylchoose --demo
"""
help:
usage "[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]"
description """
hwylchoose a b c d
hwylchoose a,b,c,d -s ,
hwylchoose a,b,c,d --seperator ","
hwylchoose --demo
"""
hidden demo
flags:
demo "show demo"

View file

@ -44,6 +44,8 @@ func newHwylCliHelp*(
flags: openArray[HwylFlagHelp] = @[],
styles = HwylCliStyles()
): HwylCliHelp =
result.header = dedent(header).strip()
result.footer = dedent(footer).strip()
result.description = dedent(description).strip()
if Aliases in styles.settings:
result.subcmds =
@ -86,7 +88,6 @@ func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
result.add "[" & cli.styles.flagDesc & "]"
result.add f.description
result.add "[/" & cli.styles.flagDesc & "]"
result.add "\n"
func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
result.add " "
@ -95,37 +96,39 @@ func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
result.add "[/]"
result.add " "
result.add subcmd.desc.alignLeft(cli.subcmdDescLen)
result.add "\n"
# TODO: split this into separate procs to make overriding more fluid
func render*(cli: HwylCliHelp): string =
var parts: seq[string]
if cli.header != "":
result.add cli.header
result.add "\n"
parts.add cli.header
if cli.usage != "":
result.add "[" & cli.styles.header & "]"
result.add "usage[/]:\n"
result.add indent(cli.usage, 2 )
result.add "\n"
var part: string
part.add "[" & cli.styles.header & "]"
part.add "usage[/]:\n"
part.add indent(cli.usage, 2 )
parts.add part
if cli.description != "":
result.add "\n"
result.add cli.description
result.add "\n"
parts.add cli.description
if cli.subcmds.len > 0:
result.add "\n"
result.add "[" & cli.styles.header & "]"
result.add "subcommands[/]:\n"
for s in cli.subcmds:
result.add cli.render(s)
var part: string
part.add "[" & cli.styles.header & "]"
part.add "subcommands[/]:\n"
part.add cli.subcmds.mapIt(render(cli,it)).join("\n")
parts.add part
if cli.flags.len > 0:
result.add "\n"
result.add "[" & cli.styles.header & "]"
result.add "flags[/]:\n"
for f in cli.flags:
result.add render(cli,f)
var part: string
part.add "[" & cli.styles.header & "]"
part.add "flags[/]:\n"
part.add cli.flags.mapIt(render(cli, it)).join("\n")
parts.add part
if cli.footer != "":
result.add cli.footer
parts.add cli.footer
parts.join("\n\n")
proc bb*(cli: HwylCliHelp): BbString =
result = bb(render(cli))
@ -655,7 +658,7 @@ func generateCliHelpProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
helpFlags = cfg.flagsArray()
subcmds = cfg.subCmdsArray()
styles = cfg.help.styles or (quote do: HwylCliStyles())
<<< usage
result = quote do:
proc `printHelpName`() =
echo bb(render(newHwylCliHelp(

View file

@ -33,7 +33,8 @@ hwylCli:
echo fmt"{yes=}, {color=}"
subcommands:
[one]
... """
help:
description """
the first subcommand
this command features both an enum flag and a Count flag

View file

@ -43,6 +43,13 @@ suite "basic":
"[blue]Blue[/] [red]Red[/]".bb
check "a plain string" & "[blue] a blue string".bb ==
"a plain string[blue] a blue string".bb
var s = bb("[red]red")
s.add bb("[blue]blue")
check escape($s) == escape($bb("[red]red[/][blue]blue[/]"))
test "spans":
check bb("[red]red[/][blue]blue[/]").spans.len == 2
check bb("[red]red[/red][blue]blue[/]").spans.len == 2
test "style insensitive":
bbCheck "[red]no case sensitivity[/RED]", "\e[38;5;1mno case sensitivity\e[0m"

View file

@ -3,7 +3,7 @@ import std/[
]
import hwylterm, hwylterm/cli
import hwylterm, hwylterm/hwylcli
suite "cli":
test "cli":
@ -13,5 +13,9 @@ suite "cli":
[yellow]-h[/] [magenta]--help [/] []show this help[/]
[yellow]-V[/] [magenta]--version[/] []print version[/]
"""
let cli = newHwylCli("[b]test-program[/] [[args...]",flags = [("h","help","show this help",),("V","version","print version")])
check $cli == $bb(expected)
let cli =
newHwylCliHelp(
header = "[b]test-program[/] [[args...]",
flags = [("h","help","show this help",),("V","version","print version")]
)
check $bb(cli) == $bb(expected)