add help to hwylchoose

This commit is contained in:
Daylin Morgan 2024-09-26 16:03:43 -05:00
parent b68a35fd78
commit 5673b92863
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -10,6 +10,7 @@
]## ]##
import std/[enumerate, os, strutils, sequtils, sets, terminal] import std/[enumerate, os, strutils, sequtils, sets, terminal]
import ./bbansi
template tryImport*(x, body) = template tryImport*(x, body) =
when not (compiles do: import x): body else: import x when not (compiles do: import x): body else: import x
@ -35,7 +36,7 @@ type
func newState[T](things: openArray[T], height: Natural): State = func newState[T](things: openArray[T], height: Natural): State =
result.max = len(things) - 1 result.max = len(things) - 1
result.height = height result.height = height
result.high = height result.high = min(result.max,height)
func up(s: var State) = func up(s: var State) =
if s.pos > 0: dec s.pos if s.pos > 0: dec s.pos
@ -53,28 +54,28 @@ func down(s: var State) =
inc s.high inc s.high
func pressed(s: var State, k: Key) = s.lastKey = k func pressed(s: var State, k: Key) = s.lastKey = k
func select(s: var State ) = func select(s: var State ) =
s.selections = s.selections =
symmetricDifference(s.selections, toHashSet([s.pos])) symmetricDifference(s.selections, toHashSet([s.pos]))
proc clip(s: string, length: int): string = func clip(s: string, length: int): string =
if s.len > length: s[0..length] if s.len > length: s[0..length]
else: s else: s
# proc addHelp(s: var screen) = func prefix(state: State, i: int): string =
result.add (
if (i + state.low) == state.pos: ">"
else: " "
)
result.add (
if (i + state.low) in state.selections: ">"
else: " "
)
func addThingsWindow[T](state: var State, things: openArray[T]) = func addThingsWindow[T](state: var State, things: openArray[T]) =
var window: string var window: string
for i, t in enumerate(things[state.low..state.high]): for i, t in enumerate(things[state.low..state.high]):
window.add ( window.add prefix(state, i)
if (i + state.low) == state.pos: ">"
else: " "
)
window.add (
if (i + state.low) in state.selections: ">"
else: " "
)
window.add $t window.add $t
window.add "\n" window.add "\n"
state.buffer.add window state.buffer.add window
@ -95,7 +96,7 @@ proc draw(s: var State) =
flushFile stdout flushFile stdout
s.buffer = "" s.buffer = ""
proc getSelections[T](state: State, things: openArray[T]): seq[T] = func getSelections[T](state: State, things: openArray[T]): seq[T] =
if state.selections.len == 0: if state.selections.len == 0:
result.add things[state.pos] result.add things[state.pos]
for i in state.selections: for i in state.selections:
@ -131,32 +132,58 @@ proc choose*[T](things: openArray[T], height: Natural = 6): seq[T] =
when isMainModule: when isMainModule:
# import std/parseopt import std/[parseopt, strformat]
# var func styleFlag(s, l, d: string): string =
# posArgs: seq[string] fmt" [yellow]-{s}[/] [green]--{l.alignLeft(10)}[/] {d}"
# style: string proc writeHelp() =
# showDebug: bool const flags = [
# var p = initOptParser() styleFlag("h", "help", "show this help"),
# for kind, key, val in p.getopt(): styleFlag("s", "seperator", "seperator to split items"),
# case kind ].join("\n")
# of cmdEnd: echo bbfmt"""
# break [bold]hwylchoose[/] \[[green]args...[/]] \[[faint]-h[/]]
# of cmdShortOption, cmdLongOption:
# case key [italic]usage[/]:
# of "help", "h": hwylchoose a b c d
# writeHelp() hwylchoose a,b,c,d -s,
# of cmdArgument: hwylchoose a,b,c,d --seperator ","
# strArgs.add key
# for arg in strArgs: flags:
# let styled = {flags}
# if style != "": """
# arg.bb(style)
# else: var
# arg.bb posArgs: seq[string]
# echo styled sep: string
# if showDebug: var p = initOptParser(
# echo debug(styled) shortNoVal = {'h'}, longNoVal = @["help", "demo"]
let items = LowercaseLetters.toSeq() )
for kind, key, val in p.getopt():
case kind
of cmdEnd:
break
of cmdShortOption, cmdLongOption:
case key
of "help", "h":
writeHelp(); quit 0
of "demo":
posArgs &= LowercaseLetters.toSeq().mapIt($it)
of "seperator","s":
if val == "":
echo bb"[red]ERROR[/]: expected value for --seperator"
quit QuitFailure
sep = val
else:
echo bb"[yellow]warning[/]: unexpected option/value -> ", key, ", ", val
of cmdArgument:
posArgs.add key
if posArgs.len == 0: quit "expected values to choose"
var items: seq[string]
if sep != "":
if posArgs.len != 1: quit "only pass one positional arg when using --sep"
items = posArgs[0].split(sep).mapIt(strip(it))
else:
items = posArgs
let item = choose(items) let item = choose(items)
echo "selected: ", item echo "selected: ", item