fix bbstring handling

This commit is contained in:
Daylin Morgan 2024-12-30 11:03:22 -06:00
parent 38cbe9f020
commit dc78efe1d8
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -25,10 +25,10 @@ type
var spinnyChannel: Channel[SpinnyEvent]
proc newSpinny*(text: string | Bbstring, s: Spinner): Spinny =
proc newSpinny*(text: Bbstring, 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)),
@ -38,6 +38,9 @@ proc newSpinny*(text: string | Bbstring, s: Spinner): Spinny =
file: stderr,
)
proc newSpinny*(text: string, s: Spinner): Spinny =
newSpinny(bb(text), s)
proc newSpinny*(text: string | Bbstring, spinType: SpinnerKind): Spinny =
newSpinny(text, Spinners[spinType])
@ -110,6 +113,7 @@ proc stop*(spinny: Spinny) =
spinny.stop(Stop)
template withSpinner*(msg: string = "", body: untyped): untyped =
block:
var spinner {.inject.} = newSpinny(msg, Dots)
if isatty(spinner.file): # don't spin if it's not a tty
start spinner
@ -118,10 +122,22 @@ template withSpinner*(msg: string = "", body: untyped): untyped =
else:
body
template withSpinner*(msg: BbString = bb"", body: untyped): untyped =
block:
var spinner {.inject.} = newSpinny(msg, Dots)
if isatty(spinner.file): # don't spin if it's not a tty
start spinner
body
stop spinner
else:
body
template withSpinner*(body: untyped): untyped =
withSpinner("", body)
template with*(kind: SpinnerKind, msg: string, body: untyped): untyped =
block:
var spinner {.inject.} = newSpinny(msg, kind)
if isatty(spinner.file): # don't spin if it's not a tty
start spinner
@ -130,8 +146,18 @@ template with*(kind: SpinnerKind, msg: string, body: untyped): untyped =
else:
body
template with*(kind: SpinnerKind, msg: BbString, body: untyped): untyped =
block:
var spinner {.inject.} = newSpinny(msg, kind)
if isatty(spinner.file): # don't spin if it's not a tty
start spinner
body
stop spinner
else:
body
when isMainModule:
for kind, _ in Spinners:
with(kind, $kind):
sleep 1 * 1000