From 859b8fb20525b44a94ab5072405c6c9d6df4da5b Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 18 Jan 2022 19:45:09 -0400 Subject: initial working changes for big data restructure --- src/util/cli.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src/util/cli.js') diff --git a/src/util/cli.js b/src/util/cli.js index 7f84be7..b633572 100644 --- a/src/util/cli.js +++ b/src/util/cli.js @@ -3,18 +3,47 @@ // A 8unch of these depend on process.stdout 8eing availa8le, so they won't // work within the 8rowser. +const { process } = globalThis; + +export const ENABLE_COLOR = process && ( + (process.env.CLICOLOR_FORCE && process.env.CLICOLOR_FORCE === '1') + ?? (process.env.CLICOLOR && process.env.CLICOLOR === '1' && process.stdout.hasColors && process.stdout.hasColors()) + ?? (process.stdout.hasColors ? process.stdout.hasColors() : true)); + +const C = n => (ENABLE_COLOR + ? text => `\x1b[${n}m${text}\x1b[0m` + : text => text); + +export const color = { + bright: C('1'), + dim: C('2'), + black: C('30'), + red: C('31'), + green: C('32'), + yellow: C('33'), + blue: C('34'), + magenta: C('35'), + cyan: C('36'), + white: C('37') +}; + const logColor = color => (literals, ...values) => { const w = s => process.stdout.write(s); - w(`\x1b[${color}m`); + const wc = text => { + if (ENABLE_COLOR) w(text); + }; + + wc(`\x1b[${color}m`); for (let i = 0; i < literals.length; i++) { w(literals[i]); if (values[i] !== undefined) { - w(`\x1b[1m`); + wc(`\x1b[1m`); w(String(values[i])); - w(`\x1b[0;${color}m`); + wc(`\x1b[0;${color}m`); } } - w(`\x1b[0m\n`); + wc(`\x1b[0m`); + w('\n'); }; export const logInfo = logColor(2); -- cgit 1.3.0-6-gf8a5