Skip to content

Commit

Permalink
perf: ⚡️ get rid of cli-spinners, cli-cursor, and cursor-restore
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Mar 4, 2020
1 parent a22ab34 commit 18d5424
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/spinner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import chalk from "chalk"
import * as cliCursor from "cli-cursor"
// eslint-disable-next-line import/default
import cliSpinners from "cli-spinners"
import { performance } from "perf_hooks"
import symbols from "./symbols"
import { Terminal } from "./terminal"
Expand All @@ -12,6 +10,19 @@ export enum SpinnerResult {
warning,
}

function showCursor(stream = process.stderr) {
stream.isTTY && stream.write("\u001B[?25h")
}

function hideCursor(stream = process.stderr) {
if (!stream.isTTY) return
;(["SIGTERM", "SIGINT"] as const).forEach(event =>
process.once(event, () => showCursor(stream))
)
process.once("exit", () => showCursor(stream))
stream.write("\u001B[?25l")
}

export class Spinner {
result?: SpinnerResult
start: number
Expand Down Expand Up @@ -40,7 +51,17 @@ export class Spinner {

export class OutputSpinner {
/* c8 ignore next */
spinner = process.platform === "win32" ? cliSpinners.line : cliSpinners.dots
spinner =
process.platform === "win32"
? {
interval: 130,
frames: ["-", "\\", "|", "/"],
}
: {
interval: 80,
frames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
}

frame = 0
interval: NodeJS.Timeout | undefined
running = false
Expand Down Expand Up @@ -139,7 +160,7 @@ export class OutputSpinner {
/* c8 ignore next */
if (this.running) return
this.running = true
cliCursor.hide(this.stream)
hideCursor(this.stream)
this.interval = setInterval(() => {
/* c8 ignore next 2 */
this.frame = ++this.frame % this.spinner.frames.length
Expand All @@ -152,7 +173,7 @@ export class OutputSpinner {
if (this.interval) clearInterval(this.interval)
this.render(true)
this.interval = undefined
cliCursor.show(this.stream)
showCursor(this.stream)
this.running = false
this.spinnerMap.clear()
}
Expand Down

0 comments on commit 18d5424

Please sign in to comment.