mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-22 11:00:45 -06:00
Compare commits
No commits in common. "f1cc95f86edcc00665fc8280f57edc0e83d461f9" and "6082cc3835ca9ef25570dcea3a43f565627145cd" have entirely different histories.
f1cc95f86e
...
6082cc3835
6 changed files with 43 additions and 74 deletions
|
@ -149,7 +149,6 @@ 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
|
||||||
|
@ -161,23 +160,16 @@ 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 size(span: BbSpan): int =
|
proc len(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:
|
||||||
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
|
bbs.spans[^1].slice[1] = bbs.plain.len - 1
|
||||||
|
if bbs.spans[^1].len == 0 and bbs.plain.len == 0:
|
||||||
# I think this is covered by the first condition now?
|
bbs.spans.delete(bbs.spans.len - 1)
|
||||||
# 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])
|
||||||
|
@ -320,7 +312,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]]
|
||||||
|
|
||||||
func truncate*(bs: Bbstring, len: Natural): Bbstring =
|
proc 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
|
||||||
|
@ -333,7 +325,7 @@ func 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)
|
||||||
|
|
||||||
func `&`*(x: BbString, y: BbString): Bbstring =
|
proc `&`*(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
|
||||||
|
@ -341,13 +333,7 @@ func `&`*(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)
|
||||||
|
|
||||||
func add*(x: var Bbstring, y :Bbstring) =
|
proc bbEscape*(s: string): string {.inline.} =
|
||||||
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]} =
|
||||||
|
@ -383,9 +369,8 @@ when isMainModule:
|
||||||
hwylCli:
|
hwylCli:
|
||||||
name "bbansi"
|
name "bbansi"
|
||||||
settings ShowHelp
|
settings ShowHelp
|
||||||
help:
|
usage "[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-V[/]]"
|
||||||
usage "[bold]bbansi[/] [[[green]args...[/]] [[[faint]-h|-V[/]]"
|
description """
|
||||||
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,14 +131,13 @@ when isMainModule:
|
||||||
hwylcli:
|
hwylcli:
|
||||||
name "hwylchoose"
|
name "hwylchoose"
|
||||||
settings ShowHelp
|
settings ShowHelp
|
||||||
help:
|
usage "[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]"
|
||||||
usage "[bold]hwylchoose[/] [[[green]args...[/]] [[[faint]-h[/]]"
|
description """
|
||||||
description """
|
hwylchoose a b c d
|
||||||
hwylchoose a b c d
|
hwylchoose a,b,c,d -s ,
|
||||||
hwylchoose a,b,c,d -s ,
|
hwylchoose a,b,c,d --seperator ","
|
||||||
hwylchoose a,b,c,d --seperator ","
|
hwylchoose --demo
|
||||||
hwylchoose --demo
|
"""
|
||||||
"""
|
|
||||||
hidden demo
|
hidden demo
|
||||||
flags:
|
flags:
|
||||||
demo "show demo"
|
demo "show demo"
|
||||||
|
|
|
@ -44,8 +44,6 @@ 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 =
|
||||||
|
@ -88,6 +86,7 @@ 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 " "
|
||||||
|
@ -96,39 +95,37 @@ 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 != "":
|
||||||
parts.add cli.header
|
result.add cli.header
|
||||||
|
result.add "\n"
|
||||||
if cli.usage != "":
|
if cli.usage != "":
|
||||||
var part: string
|
result.add "[" & cli.styles.header & "]"
|
||||||
part.add "[" & cli.styles.header & "]"
|
result.add "usage[/]:\n"
|
||||||
part.add "usage[/]:\n"
|
result.add indent(cli.usage, 2 )
|
||||||
part.add indent(cli.usage, 2 )
|
result.add "\n"
|
||||||
parts.add part
|
|
||||||
if cli.description != "":
|
if cli.description != "":
|
||||||
parts.add cli.description
|
result.add "\n"
|
||||||
|
result.add cli.description
|
||||||
|
result.add "\n"
|
||||||
if cli.subcmds.len > 0:
|
if cli.subcmds.len > 0:
|
||||||
var part: string
|
result.add "\n"
|
||||||
part.add "[" & cli.styles.header & "]"
|
result.add "[" & cli.styles.header & "]"
|
||||||
part.add "subcommands[/]:\n"
|
result.add "subcommands[/]:\n"
|
||||||
part.add cli.subcmds.mapIt(render(cli,it)).join("\n")
|
for s in cli.subcmds:
|
||||||
parts.add part
|
result.add cli.render(s)
|
||||||
if cli.flags.len > 0:
|
if cli.flags.len > 0:
|
||||||
var part: string
|
result.add "\n"
|
||||||
part.add "[" & cli.styles.header & "]"
|
result.add "[" & cli.styles.header & "]"
|
||||||
part.add "flags[/]:\n"
|
result.add "flags[/]:\n"
|
||||||
part.add cli.flags.mapIt(render(cli, it)).join("\n")
|
for f in cli.flags:
|
||||||
parts.add part
|
result.add render(cli,f)
|
||||||
if cli.footer != "":
|
if cli.footer != "":
|
||||||
parts.add cli.footer
|
result.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))
|
||||||
|
@ -658,7 +655,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,8 +33,7 @@ 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,13 +43,6 @@ 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/hwylcli
|
import hwylterm, hwylterm/cli
|
||||||
|
|
||||||
suite "cli":
|
suite "cli":
|
||||||
test "cli":
|
test "cli":
|
||||||
|
@ -13,9 +13,5 @@ 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 =
|
let cli = newHwylCli("[b]test-program[/] [[args...]",flags = [("h","help","show this help",),("V","version","print version")])
|
||||||
newHwylCliHelp(
|
check $cli == $bb(expected)
|
||||||
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