mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-22 11:00:45 -06:00
Compare commits
6 commits
6082cc3835
...
f1cc95f86e
Author | SHA1 | Date | |
---|---|---|---|
f1cc95f86e | |||
d0d1297f23 | |||
fef319ee52 | |||
447362793e | |||
3de786f003 | |||
1412ad8309 |
6 changed files with 74 additions and 43 deletions
|
@ -149,6 +149,7 @@ func stripAnsi*(s: string): string =
|
||||||
type
|
type
|
||||||
BbSpan* = object
|
BbSpan* = object
|
||||||
styles*: seq[string]
|
styles*: seq[string]
|
||||||
|
# TODO: use actual slice?
|
||||||
slice*: array[2, int]
|
slice*: array[2, int]
|
||||||
|
|
||||||
BbString* = object
|
BbString* = object
|
||||||
|
@ -160,17 +161,24 @@ func shift(s: BbSpan, i: Natural): BbSpan =
|
||||||
inc(result.slice[0],i)
|
inc(result.slice[0],i)
|
||||||
inc(result.slice[1],i)
|
inc(result.slice[1],i)
|
||||||
|
|
||||||
proc len(span: BbSpan): int =
|
proc size(span: BbSpan): int =
|
||||||
span.slice[1] - span.slice[0]
|
span.slice[1] - span.slice[0]
|
||||||
|
|
||||||
|
# TODO: make sure we don't get non-existent spans?
|
||||||
template endSpan(bbs: var BbString) =
|
template endSpan(bbs: var BbString) =
|
||||||
if bbs.spans.len == 0:
|
if bbs.spans.len == 0:
|
||||||
return
|
return
|
||||||
if bbs.plain.len >= 1:
|
|
||||||
bbs.spans[^1].slice[1] = bbs.plain.len - 1
|
if bbs.plain.len == bbs.spans[^1].slice[0]:
|
||||||
if bbs.spans[^1].len == 0 and bbs.plain.len == 0:
|
|
||||||
bbs.spans.delete(bbs.spans.len - 1)
|
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] = @[]) =
|
proc newSpan(bbs: var BbString, styles: seq[string] = @[]) =
|
||||||
bbs.spans.add BbSpan(styles: styles, slice: [bbs.plain.len, 0])
|
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 =
|
func slice(bs: BbString, span: BbSpan): string =
|
||||||
bs.plain[span.slice[0]..span.slice[1]]
|
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
|
if bs.len < len: return bs
|
||||||
for span in bs.spans:
|
for span in bs.spans:
|
||||||
if span.slice[0] >= len: break
|
if span.slice[0] >= len: break
|
||||||
|
@ -325,7 +333,7 @@ proc truncate*(bs: Bbstring, len: Natural): Bbstring =
|
||||||
result.spans.add span
|
result.spans.add span
|
||||||
result.plain.add bs.slice(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.plain.add x.plain
|
||||||
result.spans.add x.spans
|
result.spans.add x.spans
|
||||||
result.plain.add y.plain
|
result.plain.add y.plain
|
||||||
|
@ -333,7 +341,13 @@ proc `&`*(x: BbString, y: BbString): Bbstring =
|
||||||
for span in y.spans:
|
for span in y.spans:
|
||||||
result.spans.add shift(span, i)
|
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("\\", "\\\\")
|
s.replace("[", "[[").replace("\\", "\\\\")
|
||||||
|
|
||||||
proc bbEcho*(args: varargs[string, `$`]) {.raises: [IOError]} =
|
proc bbEcho*(args: varargs[string, `$`]) {.raises: [IOError]} =
|
||||||
|
@ -369,8 +383,9 @@ when isMainModule:
|
||||||
hwylCli:
|
hwylCli:
|
||||||
name "bbansi"
|
name "bbansi"
|
||||||
settings ShowHelp
|
settings ShowHelp
|
||||||
usage "[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-V[/]]"
|
help:
|
||||||
description """
|
usage "[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-V[/]]"
|
||||||
|
description """
|
||||||
bbansi "[[yellow] yellow text!"
|
bbansi "[[yellow] yellow text!"
|
||||||
-> [yellow] yellow text![/]
|
-> [yellow] yellow text![/]
|
||||||
bbansi "[[bold red] bold red text[[/] plain text..."
|
bbansi "[[bold red] bold red text[[/] plain text..."
|
||||||
|
|
|
@ -131,13 +131,14 @@ when isMainModule:
|
||||||
hwylcli:
|
hwylcli:
|
||||||
name "hwylchoose"
|
name "hwylchoose"
|
||||||
settings ShowHelp
|
settings ShowHelp
|
||||||
usage "[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]"
|
help:
|
||||||
description """
|
usage "[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]"
|
||||||
hwylchoose a b c d
|
description """
|
||||||
hwylchoose a,b,c,d -s ,
|
hwylchoose a b c d
|
||||||
hwylchoose a,b,c,d --seperator ","
|
hwylchoose a,b,c,d -s ,
|
||||||
hwylchoose --demo
|
hwylchoose a,b,c,d --seperator ","
|
||||||
"""
|
hwylchoose --demo
|
||||||
|
"""
|
||||||
hidden demo
|
hidden demo
|
||||||
flags:
|
flags:
|
||||||
demo "show demo"
|
demo "show demo"
|
||||||
|
|
|
@ -44,6 +44,8 @@ func newHwylCliHelp*(
|
||||||
flags: openArray[HwylFlagHelp] = @[],
|
flags: openArray[HwylFlagHelp] = @[],
|
||||||
styles = HwylCliStyles()
|
styles = HwylCliStyles()
|
||||||
): HwylCliHelp =
|
): HwylCliHelp =
|
||||||
|
result.header = dedent(header).strip()
|
||||||
|
result.footer = dedent(footer).strip()
|
||||||
result.description = dedent(description).strip()
|
result.description = dedent(description).strip()
|
||||||
if Aliases in styles.settings:
|
if Aliases in styles.settings:
|
||||||
result.subcmds =
|
result.subcmds =
|
||||||
|
@ -86,7 +88,6 @@ func render*(cli: HwylCliHelp, f: HwylFlagHelp): string =
|
||||||
result.add "[" & cli.styles.flagDesc & "]"
|
result.add "[" & cli.styles.flagDesc & "]"
|
||||||
result.add f.description
|
result.add f.description
|
||||||
result.add "[/" & cli.styles.flagDesc & "]"
|
result.add "[/" & cli.styles.flagDesc & "]"
|
||||||
result.add "\n"
|
|
||||||
|
|
||||||
func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
|
func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
|
||||||
result.add " "
|
result.add " "
|
||||||
|
@ -95,37 +96,39 @@ func render*(cli: HwylCliHelp, subcmd: HwylSubCmdHelp): string =
|
||||||
result.add "[/]"
|
result.add "[/]"
|
||||||
result.add " "
|
result.add " "
|
||||||
result.add subcmd.desc.alignLeft(cli.subcmdDescLen)
|
result.add subcmd.desc.alignLeft(cli.subcmdDescLen)
|
||||||
result.add "\n"
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: split this into separate procs to make overriding more fluid
|
# TODO: split this into separate procs to make overriding more fluid
|
||||||
func render*(cli: HwylCliHelp): string =
|
func render*(cli: HwylCliHelp): string =
|
||||||
|
var parts: seq[string]
|
||||||
|
|
||||||
if cli.header != "":
|
if cli.header != "":
|
||||||
result.add cli.header
|
parts.add cli.header
|
||||||
result.add "\n"
|
|
||||||
if cli.usage != "":
|
if cli.usage != "":
|
||||||
result.add "[" & cli.styles.header & "]"
|
var part: string
|
||||||
result.add "usage[/]:\n"
|
part.add "[" & cli.styles.header & "]"
|
||||||
result.add indent(cli.usage, 2 )
|
part.add "usage[/]:\n"
|
||||||
result.add "\n"
|
part.add indent(cli.usage, 2 )
|
||||||
|
parts.add part
|
||||||
if cli.description != "":
|
if cli.description != "":
|
||||||
result.add "\n"
|
parts.add cli.description
|
||||||
result.add cli.description
|
|
||||||
result.add "\n"
|
|
||||||
if cli.subcmds.len > 0:
|
if cli.subcmds.len > 0:
|
||||||
result.add "\n"
|
var part: string
|
||||||
result.add "[" & cli.styles.header & "]"
|
part.add "[" & cli.styles.header & "]"
|
||||||
result.add "subcommands[/]:\n"
|
part.add "subcommands[/]:\n"
|
||||||
for s in cli.subcmds:
|
part.add cli.subcmds.mapIt(render(cli,it)).join("\n")
|
||||||
result.add cli.render(s)
|
parts.add part
|
||||||
if cli.flags.len > 0:
|
if cli.flags.len > 0:
|
||||||
result.add "\n"
|
var part: string
|
||||||
result.add "[" & cli.styles.header & "]"
|
part.add "[" & cli.styles.header & "]"
|
||||||
result.add "flags[/]:\n"
|
part.add "flags[/]:\n"
|
||||||
for f in cli.flags:
|
part.add cli.flags.mapIt(render(cli, it)).join("\n")
|
||||||
result.add render(cli,f)
|
parts.add part
|
||||||
if cli.footer != "":
|
if cli.footer != "":
|
||||||
result.add cli.footer
|
parts.add cli.footer
|
||||||
|
|
||||||
|
parts.join("\n\n")
|
||||||
|
|
||||||
|
|
||||||
proc bb*(cli: HwylCliHelp): BbString =
|
proc bb*(cli: HwylCliHelp): BbString =
|
||||||
result = bb(render(cli))
|
result = bb(render(cli))
|
||||||
|
@ -655,7 +658,7 @@ func generateCliHelpProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
|
||||||
helpFlags = cfg.flagsArray()
|
helpFlags = cfg.flagsArray()
|
||||||
subcmds = cfg.subCmdsArray()
|
subcmds = cfg.subCmdsArray()
|
||||||
styles = cfg.help.styles or (quote do: HwylCliStyles())
|
styles = cfg.help.styles or (quote do: HwylCliStyles())
|
||||||
<<< usage
|
|
||||||
result = quote do:
|
result = quote do:
|
||||||
proc `printHelpName`() =
|
proc `printHelpName`() =
|
||||||
echo bb(render(newHwylCliHelp(
|
echo bb(render(newHwylCliHelp(
|
||||||
|
|
|
@ -33,7 +33,8 @@ hwylCli:
|
||||||
echo fmt"{yes=}, {color=}"
|
echo fmt"{yes=}, {color=}"
|
||||||
subcommands:
|
subcommands:
|
||||||
[one]
|
[one]
|
||||||
... """
|
help:
|
||||||
|
description """
|
||||||
the first subcommand
|
the first subcommand
|
||||||
|
|
||||||
this command features both an enum flag and a Count flag
|
this command features both an enum flag and a Count flag
|
||||||
|
|
|
@ -43,6 +43,13 @@ suite "basic":
|
||||||
"[blue]Blue[/] [red]Red[/]".bb
|
"[blue]Blue[/] [red]Red[/]".bb
|
||||||
check "a plain string" & "[blue] a blue string".bb ==
|
check "a plain string" & "[blue] a blue string".bb ==
|
||||||
"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":
|
test "style insensitive":
|
||||||
bbCheck "[red]no case sensitivity[/RED]", "\e[38;5;1mno case sensitivity\e[0m"
|
bbCheck "[red]no case sensitivity[/RED]", "\e[38;5;1mno case sensitivity\e[0m"
|
||||||
|
|
|
@ -3,7 +3,7 @@ import std/[
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
import hwylterm, hwylterm/cli
|
import hwylterm, hwylterm/hwylcli
|
||||||
|
|
||||||
suite "cli":
|
suite "cli":
|
||||||
test "cli":
|
test "cli":
|
||||||
|
@ -13,5 +13,9 @@ suite "cli":
|
||||||
[yellow]-h[/] [magenta]--help [/] []show this help[/]
|
[yellow]-h[/] [magenta]--help [/] []show this help[/]
|
||||||
[yellow]-V[/] [magenta]--version[/] []print version[/]
|
[yellow]-V[/] [magenta]--version[/] []print version[/]
|
||||||
"""
|
"""
|
||||||
let cli = newHwylCli("[b]test-program[/] [[args...]",flags = [("h","help","show this help",),("V","version","print version")])
|
let cli =
|
||||||
check $cli == $bb(expected)
|
newHwylCliHelp(
|
||||||
|
header = "[b]test-program[/] [[args...]",
|
||||||
|
flags = [("h","help","show this help",),("V","version","print version")]
|
||||||
|
)
|
||||||
|
check $bb(cli) == $bb(expected)
|
||||||
|
|
Loading…
Reference in a new issue