remove spinner colorize procs

This commit is contained in:
Daylin Morgan 2024-09-09 08:44:28 -05:00
parent 24829d6de2
commit 6e31339e3c
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -1,49 +1,6 @@
import std/[os, locks, sequtils, terminal] import std/[os, locks, sequtils, terminal]
# https://github.com/molnarmark/colorize import bbansi
proc reset(): string = "\e[0m"
# foreground colors
proc fgRed*(s: string): string = "\e[31m" & s & reset()
proc fgBlack*(s: string): string = "\e[30m" & s & reset()
proc fgGreen*(s: string): string = "\e[32m" & s & reset()
proc fgYellow*(s: string): string = "\e[33m" & s & reset()
proc fgBlue*(s: string): string = "\e[34m" & s & reset()
proc fgMagenta*(s: string): string = "\e[35m" & s & reset()
proc fgCyan*(s: string): string = "\e[36m" & s & reset()
proc fgLightGray*(s: string): string = "\e[37m" & s & reset()
proc fgDarkGray*(s: string): string = "\e[90m" & s & reset()
proc fgLightRed*(s: string): string = "\e[91m" & s & reset()
proc fgLightGreen*(s: string): string = "\e[92m" & s & reset()
proc fgLightYellow*(s: string): string = "\e[93m" & s & reset()
proc fgLightBlue*(s: string): string = "\e[94m" & s & reset()
proc fgLightMagenta*(s: string): string = "\e[95m" & s & reset()
proc fgLightCyan*(s: string): string = "\e[96m" & s & reset()
proc fgWhite*(s: string): string = "\e[97m" & s & reset()
# background colors
proc bgBlack*(s: string): string = "\e[40m" & s & reset()
proc bgRed*(s: string): string = "\e[41m" & s & reset()
proc bgGreen*(s: string): string = "\e[42m" & s & reset()
proc bgYellow*(s: string): string = "\e[43m" & s & reset()
proc bgBlue*(s: string): string = "\e[44m" & s & reset()
proc bgMagenta*(s: string): string = "\e[45m" & s & reset()
proc bgCyan*(s: string): string = "\e[46m" & s & reset()
proc bgLightGray*(s: string): string = "\e[47m" & s & reset()
proc bgDarkGray*(s: string): string = "\e[100m" & s & reset()
proc bgLightRed*(s: string): string = "\e[101m" & s & reset()
proc bgLightGreen*(s: string): string = "\e[102m" & s & reset()
proc bgLightYellow*(s: string): string = "\e[103m" & s & reset()
proc bgLightBlue*(s: string): string = "\e[104m" & s & reset()
proc bgLightMagenta*(s: string): string = "\e[105m" & s & reset()
proc bgLightCyan*(s: string): string = "\e[106m" & s & reset()
proc bgWhite*(s: string): string = "\e[107m" & s & reset()
# formatting functions
proc bold*(s: string): string = "\e[1m" & s & reset()
proc underline*(s: string): string = "\e[4m" & s & reset()
proc hidden*(s: string): string = "\e[8m" & s & reset()
proc invert*(s: string): string = "\e[7m" & s & reset()
type type
SpinnerKind* = enum SpinnerKind* = enum
@ -71,10 +28,10 @@ type
frame: string frame: string
interval: int interval: int
customSymbol: bool customSymbol: bool
style: string
EventKind = enum EventKind = enum
Stop, StopSuccess, StopError, Stop, SymbolChange, TextChange,
SymbolChange, TextChange,
SpinnyEvent = object SpinnyEvent = object
kind: EventKind kind: EventKind
@ -83,19 +40,21 @@ type
var spinnyChannel: Channel[SpinnyEvent] var spinnyChannel: Channel[SpinnyEvent]
proc newSpinny*(text: string, s: Spinner): Spinny = proc newSpinny*(text: string, s: Spinner): Spinny =
let style = "bold blue"
Spinny( Spinny(
text: text, text: text,
running: true, running: true,
frames: s.frames, frames: mapIt(s.frames, $bb(it, style)),
customSymbol: false, customSymbol: false,
interval: s.interval interval: s.interval,
style: "bold blue"
) )
proc newSpinny*(text: string, spinType: SpinnerKind): Spinny = proc newSpinny*(text: string, spinType: SpinnerKind): Spinny =
newSpinny(text, Spinners[spinType]) newSpinny(text, Spinners[spinType])
proc setSymbolColor*(spinny: Spinny, color: proc(x: string): string) = proc setSymbolColor*(spinny: Spinny, style: string) =
spinny.frames = mapIt(spinny.frames, color(it)) spinny.frames = mapIt(spinny.frames, $bb(it, style))
proc setSymbol*(spinny: Spinny, symbol: string) = proc setSymbol*(spinny: Spinny, symbol: string) =
spinnyChannel.send(SpinnyEvent(kind: SymbolChange, payload: symbol)) spinnyChannel.send(SpinnyEvent(kind: SymbolChange, payload: symbol))
@ -113,14 +72,6 @@ proc handleEvent(spinny: Spinny, eventData: SpinnyEvent): bool =
spinny.frame = eventData.payload spinny.frame = eventData.payload
of TextChange: of TextChange:
spinny.text = eventData.payload spinny.text = eventData.payload
of StopSuccess:
spinny.customSymbol = true
spinny.frame = "".bold.fgGreen
spinny.text = eventData.payload.bold.fgGreen
of StopError:
spinny.customSymbol = true
spinny.frame = "".bold.fgRed
spinny.text = eventData.payload.bold.fgRed
proc spinnyLoop(spinny: Spinny) {.thread.} = proc spinnyLoop(spinny: Spinny) {.thread.} =
var frameCounter = 0 var frameCounter = 0
@ -147,7 +98,7 @@ proc spinnyLoop(spinny: Spinny) {.thread.} =
stdout.write(spinny.frame & " " & spinny.text) stdout.write(spinny.frame & " " & spinny.text)
stdout.flushFile() stdout.flushFile()
sleep(spinny.interval) sleep spinny.interval
if frameCounter >= spinny.frames.len - 1: if frameCounter >= spinny.frames.len - 1:
frameCounter = 0 frameCounter = 0
@ -155,14 +106,14 @@ proc spinnyLoop(spinny: Spinny) {.thread.} =
frameCounter += 1 frameCounter += 1
proc start*(spinny: Spinny) = proc start*(spinny: Spinny) =
initLock(spinny.lock) initLock spinny.lock
spinnyChannel.open() spinnyChannel.open()
createThread(spinny.t, spinnyLoop, spinny) createThread(spinny.t, spinnyLoop, spinny)
proc stop(spinny: Spinny, kind: EventKind, payload = "") = proc stop(spinny: Spinny, kind: EventKind, payload = "") =
spinnyChannel.send(SpinnyEvent(kind: kind, payload: payload)) spinnyChannel.send(SpinnyEvent(kind: kind, payload: payload))
spinnyChannel.send(SpinnyEvent(kind: Stop)) spinnyChannel.send(SpinnyEvent(kind: Stop))
joinThread(spinny.t) joinThread spinny.t
eraseLine stdout eraseLine stdout
flushFile stdout flushFile stdout
@ -170,15 +121,8 @@ proc stop(spinny: Spinny, kind: EventKind, payload = "") =
proc stop*(spinny: Spinny) = proc stop*(spinny: Spinny) =
spinny.stop(Stop) spinny.stop(Stop)
proc success*(spinny: Spinny, msg: string) =
spinny.stop(StopSuccess, msg)
proc error*(spinny: Spinny, msg: string) =
spinny.stop(StopError, msg)
template withSpinner*(msg: string = "", body: untyped): untyped = template withSpinner*(msg: string = "", body: untyped): untyped =
var spinner {.inject.} = newSpinny(msg, Dots) var spinner {.inject.} = newSpinny(msg, Dots)
spinner.setSymbolColor(fgBlue)
if isatty(stdout): # don't spin if it's not a tty if isatty(stdout): # don't spin if it's not a tty
start spinner start spinner