ora vs cli-spinners
命令行加载指示器
oracli-spinners类似的npm包:
命令行加载指示器

命令行加载指示器是用于在终端中显示进度或状态的工具,旨在提升用户体验并提供视觉反馈。它们通常用于长时间运行的任务,以便用户能够看到操作正在进行,而不是等待没有反馈的状态。'cli-spinners'和'ora'都是流行的选择,各自具有独特的功能和设计理念,适用于不同的使用场景。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
ora22,396,6989,54830.4 kB94 个月前MIT
cli-spinners18,635,3672,66133.2 kB43 个月前MIT
功能对比: ora vs cli-spinners

自定义选项

  • ora:

    ora提供了一些预设的旋转器样式,但自定义选项相对较少。它更专注于简化使用过程,提供易于使用的API来快速实现加载指示器,而不是深度自定义。

  • cli-spinners:

    cli-spinners提供多种旋转器样式和颜色选项,允许开发者根据自己的需求进行高度自定义。你可以轻松选择不同的动画效果,甚至可以创建自己的旋转器样式,以适应特定的项目需求。

易用性

  • ora:

    ora的使用也非常简单,特别是在处理异步操作时,它能够自动处理加载状态,减少了手动管理状态的复杂性,非常适合需要快速反馈的场景。

  • cli-spinners:

    cli-spinners的API设计简单直观,易于上手,适合初学者和需要快速实现的开发者。只需几行代码即可集成到项目中,快速展示加载效果。

动画效果

  • ora:

    ora的动画效果相对固定,虽然也很美观,但不如cli-spinners灵活。它更注重于提供一致的用户体验,而不是多样化的动画选择。

  • cli-spinners:

    cli-spinners提供多种动画效果,用户可以选择不同的旋转器样式,甚至可以自定义动画的速度和颜色。这使得它在视觉上更加灵活和吸引人。

社区支持

  • ora:

    ora同样有良好的社区支持,尤其是在Node.js生态系统中,许多开发者使用它并分享使用经验,文档也相对完善。

  • cli-spinners:

    cli-spinners拥有活跃的社区支持,开发者可以在GitHub上找到大量的示例和文档,便于快速解决问题和获取灵感。

性能

  • ora:

    ora在性能上也表现出色,尤其是在处理长时间运行的异步操作时,能够有效地减少用户等待的焦虑感。

  • cli-spinners:

    cli-spinners在性能上表现良好,适合需要频繁更新的场景,能够快速响应用户操作,保持流畅的视觉效果。

如何选择: ora vs cli-spinners
  • ora:

    选择ora如果你希望有一个更具现代感的加载指示器,支持自动处理进度和状态更新,并且希望在终端中显示友好的文本提示。它适合需要更复杂交互和状态反馈的应用场景。

  • cli-spinners:

    选择cli-spinners如果你需要一个简单且灵活的解决方案,支持多种旋转器样式,并且希望能够自定义动画和颜色。它适合需要快速实现加载指示器的场景。

ora的README

ora

Elegant terminal spinner



Install

npm install ora

Check out yocto-spinner for a smaller alternative.

Usage

import ora from 'ora';

const spinner = ora('Loading unicorns').start();

setTimeout(() => {
	spinner.color = 'yellow';
	spinner.text = 'Loading rainbows';
}, 1000);

API

ora(text)

ora(options)

If a string is provided, it is treated as a shortcut for options.text.

options

Type: object

text

Type: string

The text to display next to the spinner.

prefixText

Type: string | () => 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.

suffixText

Type: string | () => string

Text or a function that returns text to display after the spinner text. No suffix text will be displayed if set to an empty string.

spinner

Type: string | object
Default: 'dots'

The name of one of the provided spinners. See example.js in this repo if you want to test out different spinners. On Windows (except for Windows Terminal), it will always use the line spinner as the Windows command-line doesn't have proper Unicode support.

Or an object like:

{
	frames: ['-', '+', '-'],
	interval: 80 // Optional
}
color

Type: string | boolean
Default: 'cyan'
Values: 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray' | boolean

The color of the spinner. Set to false to disable coloring.

hideCursor

Type: boolean
Default: true

Set to false to stop Ora from hiding the cursor.

indent

Type: number
Default: 0

Indent the spinner with the given number of spaces.

interval

Type: number
Default: Provided by the spinner or 100

Interval between each frame.

Spinners provide their own recommended interval, so you don't really need to specify this.

stream

Type: stream.Writable
Default: process.stderr

Stream to write the output.

You could for example set this to process.stdout instead.

isEnabled

Type: boolean

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.

isSilent

Type: boolean
Default: false

Disable the spinner and all log text. All output is suppressed and isEnabled will be considered false.

discardStdin

Type: boolean
Default: true

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 is no good way to implement discarding stdin properly there.

Instance

.text get/set

Change the text displayed after the spinner.

.prefixText get/set

Change the text before the spinner.

No prefix text will be displayed if set to an empty string.

.suffixText get/set

Change the text after the spinner text.

No suffix text will be displayed if set to an empty string.

.color get/set

Change the spinner color.

.spinner get/set

Change the spinner.

.indent get/set

Change the spinner indent.

.isSpinning get

A boolean indicating whether the instance is currently spinning.

.interval get

The interval between each frame.

The interval is decided by the chosen spinner.

.start(text?)

Start the spinner. Returns the instance. Set the current text if text is provided.

.stop()

Stop and clear the spinner. Returns the instance.

.succeed(text?)

Stop the spinner, change it to a green and persist the current text, or text if provided. Returns the instance. See the GIF below.

.fail(text?)

Stop the spinner, change it to a red and persist the current text, or text if provided. Returns the instance. See the GIF below.

.warn(text?)

Stop the spinner, change it to a yellow and persist the current text, or text if provided. Returns the instance.

.info(text?)

Stop the spinner, change it to a blue and persist the current text, or text if provided. Returns the instance.

.stopAndPersist(options?)

Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.

options

Type: object

symbol

Type: string
Default: ' '

Symbol to replace the spinner with.

text

Type: string
Default: Current 'text'

Text to be persisted after the symbol.

prefixText

Type: string | () => string
Default: Current prefixText

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.

suffixText

Type: string | () => string
Default: Current suffixText

Text or a function that returns text to be persisted after the text after the symbol. No suffix text will be displayed if set to an empty string.

.clear()

Clear the spinner. Returns the instance.

.render()

Manually render a new frame. Returns the instance.

.frame()

Get a new frame.

oraPromise(action, text)

oraPromise(action, options)

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.

import {oraPromise} from 'ora';

await oraPromise(somePromise);

action

Type: Promise | ((spinner: ora.Ora) => Promise)

options

Type: object

All of the 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.

spinners

Type: Record<string, Spinner>

All provided spinners.

FAQ

How do I change the color of the text?

Use chalk or yoctocolors:

import ora from 'ora';
import chalk from 'chalk';

const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();

Why does the spinner freeze?

JavaScript is single-threaded, so any synchronous operations will block the spinner's animation. To avoid this, prefer using asynchronous operations.

Can I display multiple spinners simultaneously?

No. Ora is designed to display a single spinner at a time. For multiple concurrent progress indicators, consider alternatives like listr2 or spinnies.

Can I use Ora with log-update?

Yes, use the .frame() method to get the current spinner frame and include it in your log-update output.

Does Ora work in Node.js Worker threads?

No. Ora requires an interactive terminal environment and Worker threads are not considered interactive, so the spinner will not animate. Run the spinner in the main thread and control it via worker messages:

// main.js
import {Worker} from 'node:worker_threads';
import ora from 'ora';

const spinner = ora().start();
const worker = new Worker('./worker.js');

worker.on('message', message => {
	switch (message.type) {
		case 'ora:text':
			spinner.text = message.text;
			break;
		case 'ora:succeed':
			spinner.succeed(message.text);
			break;
		case 'ora:fail':
			spinner.fail(message.text);
			break;
	}
});
// worker.js
import {parentPort} from 'node:worker_threads';

parentPort.postMessage({type: 'ora:text', text: 'Working...'});

// Do work...

parentPort.postMessage({type: 'ora:succeed', text: 'Done!'});

Related

Ports