| 17 | const showCursor = '\x1b[?25h'; |
| 18 | |
| 19 | export class Spinner { |
| 20 | /** Whether the spinner is marked as completed. */ |
| 21 | private completed = false; |
| 22 | /** The id of the interval being used to trigger frame printing. */ |
| 23 | private intervalId = setInterval(() => this.printFrame(), IS_CI ? 2500 : 125); |
| 24 | /** The characters to iterate through to create the appearance of spinning in the spinner. */ |
| 25 | private spinnerCharacters = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; |
| 26 | /** The index of the spinner character used in the frame. */ |
| 27 | private currentSpinnerCharacterIndex = 0; |
| 28 | /** The current text of the spinner. */ |
| 29 | private _text: string = ''; |
| 30 | private set text(text: string | undefined) { |
| 31 | this._text = text || this._text; |
| 32 | this.printFrame(this.getNextSpinnerCharacter(), text); |
| 33 | } |
| 34 | private get text(): string { |
| 35 | return this._text; |
| 36 | } |
| 37 | |
| 38 | constructor(); |
| 39 | constructor(text: string); |
| 40 | constructor(text?: string) { |
| 41 | this.hideCursor(); |
| 42 | this.text = text; |
| 43 | } |
| 44 | |
| 45 | /** Updates the spinner text with the provided text. */ |
| 46 | update(text: string) { |
| 47 | this.text = text; |
| 48 | } |
| 49 | |
| 50 | /** Completes the spinner marking it as successful with a `✓`. */ |
| 51 | success(text: string): void { |
| 52 | this._complete(green('✓'), text); |
| 53 | } |
| 54 | |
| 55 | /** Completes the spinner marking it as failing with an `✘`. */ |
| 56 | failure(text: string): void { |
| 57 | this._complete(red('✘'), text); |
| 58 | } |
| 59 | |
| 60 | /** Completes the spinner. */ |
| 61 | complete() { |
| 62 | this._complete('', this.text); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Internal implementation for completing the spinner, marking it as completed, and printing the |
| 67 | * final frame. |
| 68 | */ |
| 69 | private _complete(prefix: string, text: string) { |
| 70 | if (this.completed) { |
| 71 | return; |
| 72 | } |
| 73 | clearInterval(this.intervalId); |
| 74 | this.printFrame(prefix, text); |
| 75 | process.stdout.write('\n'); |
| 76 | this.showCursor(); |
nothing calls this directly
no test coverage detected