Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·67 lines (55 loc) · 1.4 KB
/
Copy pathcli.js
File metadata and controls
executable file
·67 lines (55 loc) · 1.4 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const cliCursor = require('cli-cursor');
const firstRun = require('first-run');
const indentString = require('indent-string');
const meow = require('meow');
const progressControl = require('progress-control');
const toDecimal = require('to-decimal');
const toPercent = require('to-percent');
const vol = require('vol');
const cli = meow(`
Usage
$ vol (Interactive CLI)
$ vol <level>
$ vol mute
Examples
$ vol 65
`);
const updateBar = (level, bar) => vol.set(level).then(() => {
const str = toPercent(level) + '%';
const maxLength = 4;
bar.update(level, {level: indentString(str, maxLength - str.length, ' ')});
});
const getBar = (level, text) => {
const bar = progressControl(text, {total: 10}, {
up() {
level = Math.min(Math.round((level + 0.1) * 10) / 10, 1);
updateBar(level, bar);
},
down() {
level = Math.max(Math.round((level - 0.1) * 10) / 10, 0);
updateBar(level, bar);
}
});
return bar;
};
const main = level => {
if (!process.stdin.isTTY) {
console.log(level);
return;
}
let text = '[:bar] :level';
if (firstRun()) {
text += ` ${chalk.dim('Use up/down arrows')}`;
}
const bar = getBar(level, text);
cliCursor.hide();
updateBar(level, bar);
};
if (cli.input.length === 0) {
vol.get().then(level => main(level));
} else {
vol.set(cli.input[0] === 'mute' ? 0 : toDecimal(cli.input[0]));
}