PJCHENder 未整理筆記

[npm] ora for terminal

2020-02-13

[npm] ora for terminal

keywords: package, npm, CLI, NodeJS, spinner, terminal

ora @ github

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'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

掃描二維條碼,分享此文章