Skip to content

Commit

Permalink
Update yoctospinner
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Feb 8, 2025
1 parent cb41dae commit 59bd3e7
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions packages/npm/yocto-spinner/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const process = require('node:process')
const { stripVTControlCharacters } = require('node:util')

const yoctocolors = require('yoctocolors-cjs')

Expand Down Expand Up @@ -91,7 +92,7 @@ class YoctoSpinner {

clear() {
if (!this.#isInteractive) {
return
return this
}
this.#stream.cursorTo(0)
for (let index = 0; index < this.#lines; index += 1) {
Expand All @@ -101,6 +102,7 @@ class YoctoSpinner {
this.#stream.clearLine(1)
}
this.#lines = 0
return this
}

error(text) {
Expand All @@ -121,11 +123,9 @@ class YoctoSpinner {
this.#hideCursor()
this.#render()
this.#subscribeToProcessEvents()

this.#timer = setInterval(() => {
this.#render()
}, this.#interval)

return this
}

Expand All @@ -138,7 +138,6 @@ class YoctoSpinner {
this.#showCursor()
this.clear()
this.#unsubscribeFromProcessEvents()

if (finalText) {
this.#stream.write(`${finalText}\n`)
}
Expand All @@ -153,47 +152,41 @@ class YoctoSpinner {
return this.#symbolStop(warningSymbol, text)
}

#exitHandler(signal) {
#exitHandler() {
if (this.isSpinning) {
this.stop()
}
// SIGINT: 128 + 2
// SIGTERM: 128 + 15
const exitCode = signal === 'SIGINT' ? 130 : signal === 'SIGTERM' ? 143 : 1
process.exitCode = exitCode
}

#hideCursor() {
if (this.#isInteractive) {
this.#write('\u001B[?25l')
}
}

#lineCount(text) {
const width = this.#stream.columns ?? 80
const lines = text.split('\n')

const lines = stripVTControlCharacters(text).split('\n')
let lineCount = 0
for (const line of lines) {
lineCount += Math.max(1, Math.ceil(line.length / width))
}
return lineCount
}

#hideCursor() {
if (this.#isInteractive) {
this.#write('\u001B[?25l')
}
}

#render() {
const currentTime = Date.now()
// Ensure we only update the spinner frame at the wanted interval,
// even if the render method is called more often.
// even if the frame method is called more often.
const now = Date.now()
if (
this.#currentFrame === -1 ||
currentTime - this.#lastSpinnerFrameTime >= this.#interval
now - this.#lastSpinnerFrameTime >= this.#interval
) {
this.#currentFrame = (this.#currentFrame + 1) % this.#frames.length
this.#lastSpinnerFrameTime = currentTime
this.#lastSpinnerFrameTime = now
}
const applyColor = yoctocolors[this.#color] ?? yoctocolors.cyan
const frame = this.#frames[this.#currentFrame]

let string = `${applyColor(frame)} ${this.#text}`
if (!this.#isInteractive) {
string += '\n'
Expand Down

0 comments on commit 59bd3e7

Please sign in to comment.