Skip to content

Commit

Permalink
feat: extend start method and improve types (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivodolenc authored Oct 15, 2023
1 parent 2f2d1d7 commit 66a8c1b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
52 changes: 26 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import process, { stdout } from 'node:process'
import { green, red } from './utils.js'
import type {
Options,
Spinner,
StartOptions,
UpdateOptions,
MethodOptions,
} from './types/index.js'
import type { Options, Spinner, UpdateOptions } from './types/index.js'

/**
* Creates a tiny and super customizable CLI spinner for Node.
Expand Down Expand Up @@ -75,35 +69,41 @@ export function createSpinner(options: Options = {}): Spinner {
process.exit()
}

function _update(options?: UpdateOptions): void {
_frames = options?.frames || _frames
_message = options?.message || _message
_template = options?.template || _template
}

function _clear(int?: number): void {
if (int) {
_interval = int
clearInterval(_intervalId)
_intervalId = setInterval(_render, _interval)
}
}

const spinner: Spinner = {
start(options?: StartOptions): void {
if (_intervalId) return
start(options) {
_update(options)

_message = options?.message || _message
_cursor.hide()
_intervalId = setInterval(_render, _interval)
},

update(options?: UpdateOptions): void {
_frames = options?.frames || _frames
_message = options?.message || _message
_template = options?.template || _template

if (options?.interval) {
_interval = options.interval
clearInterval(_intervalId)
_intervalId = setInterval(_render, _interval)
}
_clear(options?.interval)
},

stop(options?: MethodOptions): void {
if (!_intervalId) return
update(options) {
_update(options)
_clear(options?.interval)
},

const mark = `${options?.mark || stop.mark || green('✔')} `
stop(options) {
const mark = options?.mark || stop.mark || green('✔')
const message = options?.message || stop.message || 'Done!'
let line = `${mark}${message} \n`
let line = `${mark} ${message}\n`

if (options?.template) line = `${options.template(mark, message)} \n`
if (options?.template) line = `${options.template(mark, message)}\n`

_cursor.show()
clearInterval(_intervalId)
Expand Down
20 changes: 9 additions & 11 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
export interface StartOptions {
message?: string
}

export interface UpdateOptions {
frames?: string[]
interval?: number
message?: string
template?: (animation?: string, message?: string) => string
}

export interface MethodOptions {
export interface StopOptions {
mark?: string
message?: string
template?: (mark?: string, message?: string) => string
Expand All @@ -33,24 +29,26 @@ export interface Options {
/**
* Specifies global options for the `.start()` method.
*/
start?: StartOptions
start?: UpdateOptions
/**
* Specifies global options for the `.stop()` method.
*/
stop?: MethodOptions
stop?: StopOptions
/**
* Specifies global options for the Node `exit` event.
*
* It's activated when the user explicitly cancels the process in the terminal (`ctrl` + `c`).
*/
cancel?: MethodOptions
cancel?: StopOptions
}

export interface Spinner {
/**
* Starts the spinner.
*
* Also, it can customize spinner options individually.
*/
start(options?: StartOptions): void
start(options?: UpdateOptions): void
/**
* Dynamically updates the spinner on the fly.
*
Expand All @@ -61,10 +59,10 @@ export interface Spinner {
/**
* Stops the spinner with a custom mark and message.
*
* Also, use this method as _success_, _warning_, _cancel_, _error_ or similar events,
* Also, this method can be used as _success_, _warning_, _cancel_, _error_ or similar events,
* since it is very customizable.
*/
stop(options?: MethodOptions): void
stop(options?: StopOptions): void
}

// Auto-generated
Expand Down

0 comments on commit 66a8c1b

Please sign in to comment.