Skip to content

Commit

Permalink
feat: allow creating a silent spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 27, 2023
1 parent 6cd28fa commit c48fa6c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/logger/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export class Spinner {
return
}

/**
* Do not write when in silent mode
*/
if (this.#message.silent) {
return
}

const frame = this.#animator.getFrame()

if (this.#spinnerWriter) {
Expand Down Expand Up @@ -121,7 +128,7 @@ export class Spinner {
this.#state = 'stopped'
this.#animator.index = 0

if (!this.#spinnerWriter) {
if (!this.#spinnerWriter && !this.#message.silent) {
this.getRenderer().logUpdate(`${this.#message.render()} ${this.#animator.frames[2]}`)
this.getRenderer().logUpdatePersist()
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ export type TableRow =
export type LoggerMessageOptions = {
prefix?: string
suffix?: string
silent?: boolean
}

/**
* The message accepted by the spinner
*/
export type SpinnerMessage = {
text: string
silent?: boolean
render(): string
}
44 changes: 44 additions & 0 deletions tests/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,48 @@ test.group('Logger | await', () => {
},
])
})

test('do not write in silent mode', ({ assert }) => {
const logger = new Logger({})
const renderer = new MemoryRenderer()

logger.useRenderer(renderer)
logger.useColors(useColors({ raw: true }))
const spinner = logger.await('installing', { silent: true })
spinner.start()
spinner.stop()

assert.deepEqual(renderer.getLogs(), [])
})

test('do not write after update in silent mode', ({ assert }) => {
const logger = new Logger({})
const renderer = new MemoryRenderer()

logger.useRenderer(renderer)
logger.useColors(useColors({ raw: true }))
const spinner = logger.await('installing', { silent: true })
spinner.start()
spinner.update('foo')
spinner.stop()

assert.deepEqual(renderer.getLogs(), [])
})

test('do not invoke tap callback in silent mode', ({ assert }) => {
const logger = new Logger({})
const renderer = new MemoryRenderer()

logger.useRenderer(renderer)
logger.useColors(useColors({ raw: true }))
const spinner = logger.await('installing', { silent: true })
spinner.tap(() => {
throw new Error('Never expected to be called')
})
spinner.start()
spinner.update('foo')
spinner.stop()

assert.deepEqual(renderer.getLogs(), [])
})
})

0 comments on commit c48fa6c

Please sign in to comment.