Jacky Zhao
2025-03-13 5928d82a561d74c4656a633d8af4f03f4b97dd73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import readline from "readline"
 
export class QuartzLogger {
  verbose: boolean
  private spinnerInterval: NodeJS.Timeout | undefined
  private spinnerText: string = ""
  private spinnerIndex: number = 0
  private readonly spinnerChars = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
 
  constructor(verbose: boolean) {
    this.verbose = verbose
  }
 
  start(text: string) {
    this.spinnerText = text
    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
      }, 20)
    }
  }
 
  updateText(text: string) {
    this.spinnerText = text
  }
 
  end(text?: string) {
    if (!this.verbose && this.spinnerInterval) {
      clearInterval(this.spinnerInterval)
      this.spinnerInterval = undefined
      readline.clearLine(process.stdout, 0)
      readline.cursorTo(process.stdout, 0)
    }
 
    if (text) {
      console.log(text)
    }
  }
}