Skip to main content

[note] ora for terminal

ora @ github

這是一套用來在 Terminal 中出現 spinner 的套件。

const ora = require('ora');
const spinner = ora({
text: 'foobar',
color: 'cyan',
});
spinner.start([text]).stop().succeed([text]).fail([text]).warn([text]).info([text]).clear();

.start() 之後需要呼叫後續的方法才會終止(換行),若單純要產生訊息內容,可以直接使用 spinner.succeed() 等這類方法。使用範例 example.js

'use strict';
const chalk = require('chalk');
const Ora = require('ora');
const spinner = new Ora({
discardStdin: false,
text: 'Loading unicorns, not discarding stdin',
spinner: process.argv[2],
});
const spinnerDiscardingStdin = new Ora({
text: 'Loading unicorns',
spinner: process.argv[2],
});
spinnerDiscardingStdin.start();
setTimeout(() => {
spinnerDiscardingStdin.succeed();
}, 1000);
setTimeout(() => {
spinnerDiscardingStdin.start();
}, 2000);
setTimeout(() => {
spinnerDiscardingStdin.succeed();
spinner.start();
}, 3000);
setTimeout(() => {
spinner.color = 'yellow';
spinner.text = `Loading ${chalk.red('rainbows')}`;
}, 4000);
setTimeout(() => {
spinner.color = 'green';
spinner.indent = 2;
spinner.text = 'Loading with indent';
}, 5000);
setTimeout(() => {
spinner.indent = 0;
spinner.spinner = 'moon';
spinner.text = 'Loading with different spinners';
}, 6000);
setTimeout(() => {
spinner.succeed();
}, 7000);
// $ node example.js nameOfSpinner
Last updated on