From 0df16a26546071f83ab43d393183498a23e061ba Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 11 Sep 2023 00:38:01 -0500 Subject: [PATCH] add basic cli --- src/bbansi.nim | 50 +++++++++++++++++++++++++++++++++++++------ src/bbansi/styles.nim | 1 + todo.md | 1 + 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/bbansi.nim b/src/bbansi.nim index 21913c3..a119639 100644 --- a/src/bbansi.nim +++ b/src/bbansi.nim @@ -53,9 +53,47 @@ proc bb*(s: string): string = result.add "\e[0m" when isMainModule: - echo bb"[bold]bold" - echo bb"[red]red" - echo bb"[bold red]bold red" - echo bb"[bold red]bold red[reset] no more red" - echo bb"[unknown]this text is red no?" - echo bb"\[red] <- not a pattern " + import std/[strformat, parseopt] + const version = staticExec "git describe --tags --always --dirty=-dev" + let help = &""" +{bb"[bold]bbansi[/] [green][/] [black]<-h|-v>[/]"} + +usage: + bbansi "[yellow] yellow text!" + |-> {bb"[yellow] yellow text!"} + bbansi "[bold red] bold red[/] plain text..." + |-> {bb"[bold red] bold red text[/] plain text..."} +""" + proc writeHelp() = + echo help + quit(QuitSuccess) + proc writeVersion() = + echo "bbansi version -> ", version + quit(QuitSuccess) + var strArgs: seq[string] + var p = initOptParser() + for kind, key, val in p.getopt(): + case kind: + of cmdEnd: break + of cmdShortOption, cmdLongOption: + case key: + of "help", "h": writeHelp() + of "version","v": writeVersion() + else: + echo bb"[red]ERROR[/]: unexpected option/value -> ", key, ", ", val + echo "Option and value: ", key, ", ", val + + of cmdArgument: + strArgs.add key + + if strArgs.len != 0: + for arg in strArgs: + echo arg.bb + else: + echo "[bold]---------------------".bb + echo bb"[bold]bold" + echo bb"[red]red" + echo bb"[bold red]bold red" + echo bb"[bold red]bold red[reset] no more red" + echo bb"[unknown]this text is red no?" + echo bb"\[red] <- not a pattern " diff --git a/src/bbansi/styles.nim b/src/bbansi/styles.nim index 4183fcb..0c5290f 100644 --- a/src/bbansi/styles.nim +++ b/src/bbansi/styles.nim @@ -5,6 +5,7 @@ export strtabs let bbReset* ="\e[0m" +# should these be an enum? let codeMap* = { "reset":"0", diff --git a/todo.md b/todo.md index fe9bb86..5b36614 100644 --- a/todo.md +++ b/todo.md @@ -1,5 +1,6 @@ # bbansi todo's +- [ ] make unknown style error with compile time flag - [ ] use an actual type and `$` mechanism - [ ] add better escaping mechanism for bracketed text - [ ] add support for custom 1-256 numbers and 8-bit numbers and truecolor