Compare commits

..

No commits in common. "3cf572ea4afa6f895eb0897bfb8e6e35f8f1e953" and "ef573dc0f8bf39f80704f92cfab39e1b9af82492" have entirely different histories.

3 changed files with 33 additions and 41 deletions

View file

@ -516,13 +516,8 @@ func pasrseCliAlias(cfg: var CliCfg, node: NimNode) =
func propagate(c: var CliCfg) =
for child in c.subcommands.mitems:
# push the preSub to the lowest subcommand
if child.subcommands.len != 0 and child.preSub == nil:
child.preSub = c.preSub
child.postSub = c.postSub
else:
child.pre = c.preSub
child.post = c.postSub
child.pre = c.preSub
child.post = c.postSub
child.inheritFrom(c)
propagate(child)

View file

@ -5,50 +5,47 @@ type
Spinny = ref object
t: Thread[Spinny]
lock: Lock
text: BbString
text: string
running: bool
frames: seq[string]
bbFrames: seq[BbString]
frame: BbString
frame: string
interval: int
customSymbol: bool
style: string
file: File
EventKind = enum
Stop
SymbolChange
TextChange
SpinnyEvent = object
kind: EventKind
payload: BbString
payload: string
var spinnyChannel: Channel[SpinnyEvent]
proc newSpinny*(text: string | Bbstring, s: Spinner): Spinny =
proc newSpinny*(text: string, s: Spinner): Spinny =
let style = "bold blue"
Spinny(
text: bb(text),
text: text,
running: true,
frames: s.frames,
bbFrames: mapIt(s.frames, bb(bbEscape(it), style)),
frames: mapIt(s.frames, $bb(bbEscape(it), style)),
customSymbol: false,
interval: s.interval,
style: "bold blue",
file: stderr,
)
proc newSpinny*(text: string | Bbstring, spinType: SpinnerKind): Spinny =
proc newSpinny*(text: string, spinType: SpinnerKind): Spinny =
newSpinny(text, Spinners[spinType])
proc setSymbolColor*(spinny: Spinny, style: string) =
spinny.bbFrames = mapIt(spinny.frames, bb(bbEscape(it), style))
spinny.frames = mapIt(spinny.frames, $bb(it, style))
proc setSymbol*(spinny: Spinny, symbol: string) =
spinnyChannel.send(SpinnyEvent(kind: SymbolChange, payload: bb(symbol)))
spinnyChannel.send(SpinnyEvent(kind: SymbolChange, payload: symbol))
proc setText*(spinny: Spinny, text: string | BbString) =
spinnyChannel.send(SpinnyEvent(kind: TextChange, payload: bb(text)))
proc setText*(spinny: Spinny, text: string) =
spinnyChannel.send(SpinnyEvent(kind: TextChange, payload: text))
proc handleEvent(spinny: Spinny, eventData: SpinnyEvent): bool =
result = true
@ -77,15 +74,14 @@ proc spinnyLoop(spinny: Spinny) {.thread.} =
spinny.running = false
break
flushFile spinny.file
stdout.flushFile()
if not spinny.customSymbol:
spinny.frame = spinny.bbFrames[frameCounter]
spinny.frame = spinny.frames[frameCounter]
# TODO: instead of truncating support multiline text, need custom wrapping and cleanup then
withLock spinny.lock:
eraseLine spinny.file
spinny.file.write $((spinny.frame & " " & spinny.text).truncate(terminalWidth())) # needs to be truncated
flushFile spinny.file
eraseLine()
stdout.write(spinny.frame & " " & spinny.text)
stdout.flushFile()
sleep spinny.interval
@ -100,35 +96,37 @@ proc start*(spinny: Spinny) =
createThread(spinny.t, spinnyLoop, spinny)
proc stop(spinny: Spinny, kind: EventKind, payload = "") =
spinnyChannel.send(SpinnyEvent(kind: kind, payload: bb(payload)))
spinnyChannel.send(SpinnyEvent(kind: kind, payload: payload))
spinnyChannel.send(SpinnyEvent(kind: Stop))
joinThread spinny.t
eraseLine spinny.file
flushFile spinny.file
eraseLine stdout
flushFile stdout
proc stop*(spinny: Spinny) =
spinny.stop(Stop)
template withSpinner*(msg: string = "", body: untyped): untyped =
var spinner {.inject.} = newSpinny(msg, Dots)
if isatty(spinner.file): # don't spin if it's not a tty
if isatty(stdout): # don't spin if it's not a tty
start spinner
body
body
if isatty(stdout):
stop spinner
else:
body
template withSpinner*(body: untyped): untyped =
withSpinner("", body)
template with*(kind: SpinnerKind, msg: string, body: untyped): untyped =
var spinner {.inject.} = newSpinny(msg, kind)
if isatty(spinner.file): # don't spin if it's not a tty
if isatty(stdout): # don't spin if it's not a tty
start spinner
body
body
if isatty(stdout):
stop spinner
else:
body
when isMainModule:
for kind, _ in Spinners:

View file

@ -25,7 +25,7 @@ hwylCli:
? "path to config file"
* @["config.yml"]
preSub:
echo "this is run after subcommand parsing but before final run block"
echo "this is run after subcommand parsing but before its run block"
echo fmt"{yes=}, {color=}"
run:
echo "this is always run prior to subcommand parsing"
@ -53,7 +53,6 @@ hwylCli:
flags:
^config
run:
echo "hello from `example one subsub` command"
echo fmt"{color=}"
run: