Skip to content

Instantly share code, notes, and snippets.

@sixertoy
Last active February 28, 2018 07:09
Show Gist options
  • Save sixertoy/f26c7177e84bbef9308c96da54325f66 to your computer and use it in GitHub Desktop.
Save sixertoy/f26c7177e84bbef9308c96da54325f66 to your computer and use it in GitHub Desktop.
JS Console Utils

JS Console Utils

  • Percent preloader
  • Colors in console
const colorize = (msg, col) => {
// `$ echo -e "\033[1mThis is a BOLD line\033[0m"`
const getcolor = (msg, code) => `\x1b[${code}m${msg}\x1b[39m`;
switch (col) {
case 'bold':
return `\u001b[0;1m${msg}\u001b[0;0m`;
case 'white':
return getcolor(msg, 37);
case 'cyan':
return getcolor(msg, 36);
case 'magenta':
return getcolor(msg, 35);
case 'blue':
return getcolor(msg, 34);
case 'yellow':
return getcolor(msg, 33);
case 'green':
return getcolor(msg, 32);
case 'red':
return getcolor(msg, 31);
case 'black':
return getcolor(msg, 30);
case 'grey':
case 'gray':
default:
return getcolor(msg, 90);
return;
}
}
const preloader = (count, total) => {
const percent = (total > 0)
? Math.round(((count * 100) / total) * 10) / 10
: 0;
process.stdout.write(`\r> loading... ${percent}%`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment