Comparing version 5.4.1 to 6.0.0
431
index.d.ts
import {SpinnerName} from 'cli-spinners'; | ||
declare namespace ora { | ||
interface Spinner { | ||
readonly interval?: number; | ||
readonly frames: string[]; | ||
} | ||
export interface Spinner { | ||
readonly interval?: number; | ||
readonly frames: string[]; | ||
} | ||
type Color = | ||
| 'black' | ||
| 'red' | ||
| 'green' | ||
| 'yellow' | ||
| 'blue' | ||
| 'magenta' | ||
| 'cyan' | ||
| 'white' | ||
| 'gray'; | ||
export type Color = | ||
| 'black' | ||
| 'red' | ||
| 'green' | ||
| 'yellow' | ||
| 'blue' | ||
| 'magenta' | ||
| 'cyan' | ||
| 'white' | ||
| 'gray'; | ||
type PrefixTextGenerator = () => string; | ||
export type PrefixTextGenerator = () => string; | ||
interface Options { | ||
/** | ||
Text to display after the spinner. | ||
*/ | ||
readonly text?: string; | ||
export interface Options { | ||
/** | ||
Text to display after the spinner. | ||
*/ | ||
readonly text?: string; | ||
/** | ||
Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string. | ||
*/ | ||
readonly prefixText?: string | PrefixTextGenerator; | ||
/** | ||
Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string. | ||
*/ | ||
readonly prefixText?: string | PrefixTextGenerator; | ||
/** | ||
Name of one of the provided spinners. See [`example.js`](https://github.com/BendingBender/ora/blob/main/example.js) in this repo if you want to test out different spinners. On Windows, it will always use the line spinner as the Windows command-line doesn't have proper Unicode support. | ||
/** | ||
Name of one of the provided spinners. See [`example.js`](https://github.com/BendingBender/ora/blob/main/example.js) in this repo if you want to test out different spinners. On Windows, it will always use the line spinner as the Windows command-line doesn't have proper Unicode support. | ||
@default 'dots' | ||
@default 'dots' | ||
Or an object like: | ||
Or an object like: | ||
@example | ||
``` | ||
{ | ||
interval: 80, // Optional | ||
frames: ['-', '+', '-'] | ||
} | ||
``` | ||
*/ | ||
readonly spinner?: SpinnerName | Spinner; | ||
@example | ||
``` | ||
{ | ||
interval: 80, // Optional | ||
frames: ['-', '+', '-'] | ||
} | ||
``` | ||
*/ | ||
readonly spinner?: SpinnerName | Spinner; | ||
/** | ||
Color of the spinner. | ||
/** | ||
Color of the spinner. | ||
@default 'cyan' | ||
*/ | ||
readonly color?: Color; | ||
@default 'cyan' | ||
*/ | ||
readonly color?: Color; | ||
/** | ||
Set to `false` to stop Ora from hiding the cursor. | ||
/** | ||
Set to `false` to stop Ora from hiding the cursor. | ||
@default true | ||
*/ | ||
readonly hideCursor?: boolean; | ||
@default true | ||
*/ | ||
readonly hideCursor?: boolean; | ||
/** | ||
Indent the spinner with the given number of spaces. | ||
/** | ||
Indent the spinner with the given number of spaces. | ||
@default 0 | ||
*/ | ||
readonly indent?: number; | ||
@default 0 | ||
*/ | ||
readonly indent?: number; | ||
/** | ||
Interval between each frame. | ||
/** | ||
Interval between each frame. | ||
Spinners provide their own recommended interval, so you don't really need to specify this. | ||
Spinners provide their own recommended interval, so you don't really need to specify this. | ||
Default: Provided by the spinner or `100`. | ||
*/ | ||
readonly interval?: number; | ||
Default: Provided by the spinner or `100`. | ||
*/ | ||
readonly interval?: number; | ||
/** | ||
Stream to write the output. | ||
/** | ||
Stream to write the output. | ||
You could for example set this to `process.stdout` instead. | ||
You could for example set this to `process.stdout` instead. | ||
@default process.stderr | ||
*/ | ||
readonly stream?: NodeJS.WritableStream; | ||
@default process.stderr | ||
*/ | ||
readonly stream?: NodeJS.WritableStream; | ||
/** | ||
Force enable/disable the spinner. If not specified, the spinner will be enabled if the `stream` is being run inside a TTY context (not spawned or piped) and/or not in a CI environment. | ||
/** | ||
Force enable/disable the spinner. If not specified, the spinner will be enabled if the `stream` is being run inside a TTY context (not spawned or piped) and/or not in a CI environment. | ||
Note that `{isEnabled: false}` doesn't mean it won't output anything. It just means it won't output the spinner, colors, and other ansi escape codes. It will still log text. | ||
*/ | ||
readonly isEnabled?: boolean; | ||
Note that `{isEnabled: false}` doesn't mean it won't output anything. It just means it won't output the spinner, colors, and other ansi escape codes. It will still log text. | ||
*/ | ||
readonly isEnabled?: boolean; | ||
/** | ||
Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`. | ||
/** | ||
Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`. | ||
@default false | ||
*/ | ||
readonly isSilent?: boolean; | ||
@default false | ||
*/ | ||
readonly isSilent?: boolean; | ||
/** | ||
Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on `Enter` key presses, and prevents buffering of input while the spinner is running. | ||
/** | ||
Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on `Enter` key presses, and prevents buffering of input while the spinner is running. | ||
This has no effect on Windows as there's no good way to implement discarding stdin properly there. | ||
This has no effect on Windows as there's no good way to implement discarding stdin properly there. | ||
@default true | ||
*/ | ||
readonly discardStdin?: boolean; | ||
} | ||
@default true | ||
*/ | ||
readonly discardStdin?: boolean; | ||
} | ||
interface PersistOptions { | ||
/** | ||
Symbol to replace the spinner with. | ||
export interface PersistOptions { | ||
/** | ||
Symbol to replace the spinner with. | ||
@default ' ' | ||
*/ | ||
readonly symbol?: string; | ||
@default ' ' | ||
*/ | ||
readonly symbol?: string; | ||
/** | ||
Text to be persisted after the symbol. | ||
/** | ||
Text to be persisted after the symbol. | ||
Default: Current `text`. | ||
*/ | ||
readonly text?: string; | ||
Default: Current `text`. | ||
*/ | ||
readonly text?: string; | ||
/** | ||
Text or a function that returns text to be persisted before the symbol. No prefix text will be displayed if set to an empty string. | ||
/** | ||
Text or a function that returns text to be persisted before the symbol. No prefix text will be displayed if set to an empty string. | ||
Default: Current `prefixText`. | ||
*/ | ||
readonly prefixText?: string | PrefixTextGenerator; | ||
} | ||
Default: Current `prefixText`. | ||
*/ | ||
readonly prefixText?: string | PrefixTextGenerator; | ||
} | ||
interface Ora { | ||
/** | ||
A boolean of whether the instance is currently spinning. | ||
*/ | ||
readonly isSpinning: boolean; | ||
export interface PromiseOptions<T> extends Options { | ||
/** | ||
The new text of the spinner when the promise is resolved. | ||
/** | ||
Change the text after the spinner. | ||
*/ | ||
text: string; | ||
Keeps the existing text if `undefined`. | ||
*/ | ||
successText?: string | ((result: T) => string) | undefined; | ||
/** | ||
Change the text or function that returns text before the spinner. No prefix text will be displayed if set to an empty string. | ||
*/ | ||
prefixText: string | PrefixTextGenerator; | ||
/** | ||
The new text of the spinner when the promise is rejected. | ||
/** | ||
Change the spinner color. | ||
*/ | ||
color: Color; | ||
Keeps the existing text if `undefined`. | ||
*/ | ||
failText?: string | ((error: Error) => string) | undefined; | ||
} | ||
/** | ||
Change the spinner. | ||
*/ | ||
spinner: SpinnerName | Spinner; | ||
export interface Ora { | ||
/** | ||
A boolean of whether the instance is currently spinning. | ||
*/ | ||
readonly isSpinning: boolean; | ||
/** | ||
Change the spinner indent. | ||
*/ | ||
indent: number; | ||
/** | ||
Change the text after the spinner. | ||
*/ | ||
text: string; | ||
/** | ||
Start the spinner. | ||
/** | ||
Change the text or function that returns text before the spinner. No prefix text will be displayed if set to an empty string. | ||
*/ | ||
prefixText: string | PrefixTextGenerator; | ||
@param text - Set the current text. | ||
@returns The spinner instance. | ||
*/ | ||
start(text?: string): Ora; | ||
/** | ||
Change the spinner color. | ||
*/ | ||
color: Color; | ||
/** | ||
Stop and clear the spinner. | ||
/** | ||
Change the spinner. | ||
*/ | ||
spinner: SpinnerName | Spinner; | ||
@returns The spinner instance. | ||
*/ | ||
stop(): Ora; | ||
/** | ||
Change the spinner indent. | ||
*/ | ||
indent: number; | ||
/** | ||
Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. | ||
/** | ||
Start the spinner. | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
succeed(text?: string): Ora; | ||
@param text - Set the current text. | ||
@returns The spinner instance. | ||
*/ | ||
start(text?: string): Ora; | ||
/** | ||
Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. | ||
/** | ||
Stop and clear the spinner. | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
fail(text?: string): Ora; | ||
@returns The spinner instance. | ||
*/ | ||
stop(): Ora; | ||
/** | ||
Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. | ||
/** | ||
Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
warn(text?: string): Ora; | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
succeed(text?: string): Ora; | ||
/** | ||
Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. | ||
/** | ||
Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
info(text?: string): Ora; | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
fail(text?: string): Ora; | ||
/** | ||
Stop the spinner and change the symbol or text. | ||
/** | ||
Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. | ||
@returns The spinner instance. | ||
*/ | ||
stopAndPersist(options?: PersistOptions): Ora; | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
warn(text?: string): Ora; | ||
/** | ||
Clear the spinner. | ||
/** | ||
Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. | ||
@returns The spinner instance. | ||
*/ | ||
clear(): Ora; | ||
@param text - Will persist text if provided. | ||
@returns The spinner instance. | ||
*/ | ||
info(text?: string): Ora; | ||
/** | ||
Manually render a new frame. | ||
/** | ||
Stop the spinner and change the symbol or text. | ||
@returns The spinner instance. | ||
*/ | ||
render(): Ora; | ||
@returns The spinner instance. | ||
*/ | ||
stopAndPersist(options?: PersistOptions): Ora; | ||
/** | ||
Get a new frame. | ||
/** | ||
Clear the spinner. | ||
@returns The spinner instance text. | ||
*/ | ||
frame(): string; | ||
} | ||
} | ||
@returns The spinner instance. | ||
*/ | ||
clear(): Ora; | ||
declare const ora: { | ||
/** | ||
Elegant terminal spinner. | ||
Manually render a new frame. | ||
@param options - If a string is provided, it is treated as a shortcut for `options.text`. | ||
@example | ||
``` | ||
import ora = require('ora'); | ||
const spinner = ora('Loading unicorns').start(); | ||
setTimeout(() => { | ||
spinner.color = 'yellow'; | ||
spinner.text = 'Loading rainbows'; | ||
}, 1000); | ||
``` | ||
@returns The spinner instance. | ||
*/ | ||
(options?: ora.Options | string): ora.Ora; | ||
render(): Ora; | ||
/** | ||
Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. | ||
Get a new frame. | ||
@param action - The promise to start the spinner for. | ||
@param options - If a string is provided, it is treated as a shortcut for `options.text`. | ||
@returns The spinner instance. | ||
@returns The spinner instance text. | ||
*/ | ||
promise( | ||
action: PromiseLike<unknown>, | ||
options?: ora.Options | string | ||
): ora.Ora; | ||
}; | ||
frame(): string; | ||
} | ||
export = ora; | ||
/** | ||
Elegant terminal spinner. | ||
@param options - If a string is provided, it is treated as a shortcut for `options.text`. | ||
@example | ||
``` | ||
import ora from 'ora'; | ||
const spinner = ora('Loading unicorns').start(); | ||
setTimeout(() => { | ||
spinner.color = 'yellow'; | ||
spinner.text = 'Loading rainbows'; | ||
}, 1000); | ||
``` | ||
*/ | ||
export default function ora(options?: string | Options): Ora; | ||
/** | ||
Starts a spinner for a promise or promise-returning function. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. | ||
@param action - The promise to start the spinner for or a promise-returning function. | ||
@param options - If a string is provided, it is treated as a shortcut for `options.text`. | ||
@returns The given promise. | ||
@example | ||
``` | ||
import {oraPromise} from 'ora'; | ||
await oraPromise(somePromise); | ||
``` | ||
*/ | ||
export function oraPromise<T>( | ||
action: PromiseLike<T> | ((spinner: Ora) => PromiseLike<T>), | ||
options?: string | PromiseOptions<T> | ||
): Promise<T>; |
97
index.js
@@ -1,12 +0,12 @@ | ||
'use strict'; | ||
const readline = require('readline'); | ||
const chalk = require('chalk'); | ||
const cliCursor = require('cli-cursor'); | ||
const cliSpinners = require('cli-spinners'); | ||
const logSymbols = require('log-symbols'); | ||
const stripAnsi = require('strip-ansi'); | ||
const wcwidth = require('wcwidth'); | ||
const isInteractive = require('is-interactive'); | ||
const isUnicodeSupported = require('is-unicode-supported'); | ||
const {BufferListStream} = require('bl'); | ||
import process from 'node:process'; | ||
import readline from 'node:readline'; | ||
import chalk from 'chalk'; | ||
import cliCursor from 'cli-cursor'; | ||
import cliSpinners from 'cli-spinners'; | ||
import logSymbols from 'log-symbols'; | ||
import stripAnsi from 'strip-ansi'; | ||
import wcwidth from 'wcwidth'; | ||
import isInteractive from 'is-interactive'; | ||
import isUnicodeSupported from 'is-unicode-supported'; | ||
import {BufferListStream} from 'bl'; | ||
@@ -17,2 +17,4 @@ const TEXT = Symbol('text'); | ||
// TODO: Use class fields when ESLint 8 is out. | ||
class StdinDiscarder { | ||
@@ -72,3 +74,3 @@ constructor() { | ||
input: process.stdin, | ||
output: this.mutedStream | ||
output: this.mutedStream, | ||
}); | ||
@@ -106,3 +108,3 @@ | ||
options = { | ||
text: options | ||
text: options, | ||
}; | ||
@@ -116,3 +118,3 @@ } | ||
discardStdin: true, | ||
...options | ||
...options, | ||
}; | ||
@@ -149,2 +151,3 @@ | ||
this._indent = indent; | ||
this.updateLineCount(); | ||
} | ||
@@ -223,3 +226,3 @@ | ||
this.lineCount = 0; | ||
for (const line of stripAnsi(fullPrefixText + '--' + this[TEXT]).split('\n')) { | ||
for (const line of stripAnsi(' '.repeat(this.indent) + fullPrefixText + '--' + this[TEXT]).split('\n')) { | ||
this.lineCount += Math.max(1, Math.ceil(wcwidth(line) / columns)); | ||
@@ -273,11 +276,17 @@ } | ||
for (let i = 0; i < this.linesToClear; i++) { | ||
if (i > 0) { | ||
this.stream.cursorTo(0); | ||
for (let index = 0; index < this.linesToClear; index++) { | ||
if (index > 0) { | ||
this.stream.moveCursor(0, -1); | ||
} | ||
this.stream.clearLine(); | ||
this.stream.clearLine(1); | ||
} | ||
if (this.indent || this.lastIndent !== this.indent) { | ||
this.stream.cursorTo(this.indent); | ||
} | ||
this.lastIndent = this.indent; | ||
this.linesToClear = 0; | ||
@@ -389,27 +398,41 @@ | ||
const oraFactory = function (options) { | ||
export default function ora(options) { | ||
return new Ora(options); | ||
}; | ||
} | ||
module.exports = oraFactory; | ||
export async function oraPromise(action, options) { | ||
const actionIsFunction = typeof action === 'function'; | ||
// eslint-disable-next-line promise/prefer-await-to-then | ||
const actionIsPromise = typeof action.then === 'function'; | ||
module.exports.promise = (action, options) => { | ||
// eslint-disable-next-line promise/prefer-await-to-then | ||
if (typeof action.then !== 'function') { | ||
throw new TypeError('Parameter `action` must be a Promise'); | ||
if (!actionIsFunction && !actionIsPromise) { | ||
throw new TypeError('Parameter `action` must be a Function or a Promise'); | ||
} | ||
const spinner = new Ora(options); | ||
spinner.start(); | ||
const {successText, failText} = typeof options === 'object' | ||
? options | ||
: {successText: undefined, failText: undefined}; | ||
(async () => { | ||
try { | ||
await action; | ||
spinner.succeed(); | ||
} catch { | ||
spinner.fail(); | ||
} | ||
})(); | ||
const spinner = ora(options).start(); | ||
return spinner; | ||
}; | ||
try { | ||
const promise = actionIsFunction ? action(spinner) : action; | ||
const result = await promise; | ||
spinner.succeed( | ||
successText === undefined | ||
? undefined | ||
: (typeof successText === 'string' ? successText : successText(result)), | ||
); | ||
return result; | ||
} catch (error) { | ||
spinner.fail( | ||
failText === undefined | ||
? undefined | ||
: (typeof failText === 'string' ? failText : failText(error)), | ||
); | ||
throw error; | ||
} | ||
} |
{ | ||
"name": "ora", | ||
"version": "5.4.1", | ||
"version": "6.0.0", | ||
"description": "Elegant terminal spinner", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -41,19 +43,20 @@ "scripts": { | ||
"dependencies": { | ||
"bl": "^4.1.0", | ||
"chalk": "^4.1.0", | ||
"cli-cursor": "^3.1.0", | ||
"cli-spinners": "^2.5.0", | ||
"is-interactive": "^1.0.0", | ||
"is-unicode-supported": "^0.1.0", | ||
"log-symbols": "^4.1.0", | ||
"strip-ansi": "^6.0.0", | ||
"bl": "^5.0.0", | ||
"chalk": "^4.1.2", | ||
"cli-cursor": "^4.0.0", | ||
"cli-spinners": "^2.6.0", | ||
"is-interactive": "^2.0.0", | ||
"is-unicode-supported": "^1.1.0", | ||
"log-symbols": "^5.0.0", | ||
"strip-ansi": "^7.0.0", | ||
"wcwidth": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.14.35", | ||
"ava": "^2.4.0", | ||
"get-stream": "^6.0.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.2" | ||
"@types/node": "^16.7.1", | ||
"ava": "^3.15.0", | ||
"get-stream": "^6.0.1", | ||
"transform-tty": "^1.0.11", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -20,3 +20,3 @@ # ora | ||
```js | ||
const ora = require('ora'); | ||
import ora from 'ora'; | ||
@@ -227,11 +227,39 @@ const spinner = ora('Loading unicorns').start(); | ||
### ora.promise(action, text) | ||
### ora.promise(action, options) | ||
### oraPromise(action, text) | ||
### oraPromise(action, options) | ||
Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the spinner instance. | ||
Starts a spinner for a promise or promise-returning function. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the promise. | ||
```js | ||
import {oraPromise} from 'ora'; | ||
await oraPromise(somePromise); | ||
``` | ||
#### action | ||
Type: `Promise` | ||
Type: `Promise | ((spinner: ora.Ora) => Promise)` | ||
#### options | ||
Type: `object` | ||
All of the [options](#options) plus the following: | ||
##### successText | ||
Type: `string | ((result: T) => string) | undefined` | ||
The new text of the spinner when the promise is resolved. | ||
Keeps the existing text if `undefined`. | ||
##### failText | ||
Type: `string | ((error: Error) => string) | undefined` | ||
The new text of the spinner when the promise is rejected. | ||
Keeps the existing text if `undefined`. | ||
## FAQ | ||
@@ -244,4 +272,4 @@ | ||
```js | ||
const ora = require('ora'); | ||
const chalk = require('chalk'); | ||
import ora from 'ora'; | ||
import chalk from 'chalk'; | ||
@@ -248,0 +276,0 @@ const spinner = ora(`Loading ${chalk.red('unicorns')}`).start(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25028
559
293
Yes
6
+ Addedansi-regex@6.1.0(transitive)
+ Addedbl@5.1.0(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedchalk@5.3.0(transitive)
+ Addedcli-cursor@4.0.0(transitive)
+ Addedis-interactive@2.0.0(transitive)
+ Addedis-unicode-supported@1.3.0(transitive)
+ Addedlog-symbols@5.1.0(transitive)
+ Addedrestore-cursor@4.0.0(transitive)
+ Addedstrip-ansi@7.1.0(transitive)
- Removedansi-regex@5.0.1(transitive)
- Removedbl@4.1.0(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedcli-cursor@3.1.0(transitive)
- Removedis-interactive@1.0.0(transitive)
- Removedis-unicode-supported@0.1.0(transitive)
- Removedlog-symbols@4.1.0(transitive)
- Removedrestore-cursor@3.1.0(transitive)
- Removedstrip-ansi@6.0.1(transitive)
Updatedbl@^5.0.0
Updatedchalk@^4.1.2
Updatedcli-cursor@^4.0.0
Updatedcli-spinners@^2.6.0
Updatedis-interactive@^2.0.0
Updatedis-unicode-supported@^1.1.0
Updatedlog-symbols@^5.0.0
Updatedstrip-ansi@^7.0.0