fuse-box
Version:
Fuse-Box a bundler that does it right
81 lines (77 loc) • 2.16 kB
JavaScript
const readline = require('readline');
const SPINNERS = [
'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏',
'⠋⠙⠚⠞⠖⠦⠴⠲⠳⠓',
'⠄⠆⠇⠋⠙⠸⠰⠠⠰⠸⠙⠋⠇⠆',
'⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋',
'⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁',
'⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈',
'⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈',
'⢄⢂⢁⡁⡈⡐⡠',
'⢹⢺⢼⣸⣇⡧⡗⡏',
'⣾⣽⣻⢿⡿⣟⣯⣷',
'⠁⠂⠄⡀⢀⠠⠐⠈',
];
const Colours = {
RESET: '\x1b[0m',
BRIGHT: '\x1b[1m',
DIM: '\x1b[2m',
UNDERSCORE: '\x1b[4m',
BLINK: '\x1b[5m',
REVERSE: '\x1b[7m',
HIDDEN: '\x1b[8m',
FG_BLACK: '\x1b[30m',
FG_RED: '\x1b[31m',
FG_GREEN: '\x1b[32m',
FG_YELLOW: '\x1b[33m',
FG_BLUE: '\x1b[34m',
FG_MAGENTA: '\x1b[35m',
FG_CYAN: '\x1b[36m',
FG_WHITE: '\x1b[37m',
BG_BLACK: '\x1b[40m',
BG_RED: '\x1b[41m',
BG_GREEN: '\x1b[42m',
BG_YELLOW: '\x1b[43m',
BG_BLUE: '\x1b[44m',
BG_MAGENTA: '\x1b[45m',
BG_CYAN: '\x1b[46m',
BG_WHITE: '\x1b[47m',
};
function colourise(type, text) {
return type + text + Colours.RESET;
}
module.exports.getSpinner = function() {
function onTick(msg) {
clearLine();
process.stdout.write(msg);
}
function clearLine() {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
}
let userText = '';
let interval;
return {
start: index => {
const spinnerChars = SPINNERS[index || 0];
let current = 0;
function iteration() {
const spinnerText = colourise(
Colours.FG_YELLOW,
' ' +
(userText.indexOf('%s') > -1 ? userText.replace('%s', spinnerChars[current]) : spinnerChars[current] + ' '),
);
onTick(spinnerText + userText);
current = ++current % spinnerChars.length;
}
iteration();
interval = setInterval(iteration, 50);
},
setText: t => (userText = t),
stop: () => {
clearInterval(interval);
clearLine();
process.exit(0);
},
};
};