| 11 | import { isTTY } from './tty'; |
| 12 | |
| 13 | export class Spinner { |
| 14 | private readonly spinner: Ora; |
| 15 | |
| 16 | /** When false, only fail messages will be displayed. */ |
| 17 | enabled = true; |
| 18 | readonly #isTTY = isTTY(); |
| 19 | |
| 20 | constructor(text?: string) { |
| 21 | this.spinner = ora({ |
| 22 | text: text === undefined ? undefined : text + '\n', |
| 23 | // The below 2 options are needed because otherwise CTRL+C will be delayed |
| 24 | // when the underlying process is sync. |
| 25 | hideCursor: false, |
| 26 | discardStdin: false, |
| 27 | isEnabled: this.#isTTY, |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | set text(text: string) { |
| 32 | this.spinner.text = text; |
| 33 | } |
| 34 | |
| 35 | get isSpinning(): boolean { |
| 36 | return this.spinner.isSpinning || !this.#isTTY; |
| 37 | } |
| 38 | |
| 39 | succeed(text?: string): void { |
| 40 | if (this.enabled) { |
| 41 | this.spinner.succeed(text); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fail(text?: string): void { |
| 46 | this.spinner.fail(text && colors.redBright(text)); |
| 47 | } |
| 48 | |
| 49 | stop(): void { |
| 50 | this.spinner.stop(); |
| 51 | } |
| 52 | |
| 53 | start(text?: string): void { |
| 54 | if (this.enabled) { |
| 55 | this.spinner.start(text); |
| 56 | } |
| 57 | } |
| 58 | } |