notes/quartz/util/log.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

import readline from "readline"
2023-06-04 13:37:43 -04:00
export class QuartzLogger {
verbose: boolean
private spinnerInterval: NodeJS.Timeout | undefined
private spinnerText: string = ""
private spinnerIndex: number = 0
private readonly spinnerChars = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
2023-06-04 13:37:43 -04:00
constructor(verbose: boolean) {
this.verbose = verbose
}
start(text: string) {
this.spinnerText = text
2023-06-04 13:37:43 -04:00
if (this.verbose) {
console.log(text)
} else {
this.spinnerIndex = 0
this.spinnerInterval = setInterval(() => {
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
process.stdout.write(`${this.spinnerChars[this.spinnerIndex]} ${this.spinnerText}`)
this.spinnerIndex = (this.spinnerIndex + 1) % this.spinnerChars.length
}, 100)
2023-06-04 13:37:43 -04:00
}
}
updateText(text: string) {
this.spinnerText = text
}
2023-07-23 11:49:26 -07:00
end(text?: string) {
if (!this.verbose && this.spinnerInterval) {
clearInterval(this.spinnerInterval)
this.spinnerInterval = undefined
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
2023-06-04 13:37:43 -04:00
}
2023-07-23 11:49:26 -07:00
if (text) {
console.log(text)
}
2023-06-04 13:37:43 -04:00
}
}