Last active
March 11, 2022 21:20
-
-
Save DavidWells/91db5468684d09dc5d6a15e043af93d2 to your computer and use it in GitHub Desktop.
Simple tiny colored logged for CLIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const process = require('process') | |
const styles = require('ansi-styles') | |
// via https://github.com/sindresorhus/is-unicode-supported/blob/main/index.js | |
function isUnicodeSupported() { | |
if (process.platform !== 'win32') return process.env.TERM !== 'linux'; // Linux console (kernel) | |
return Boolean(process.env.CI) | |
|| Boolean(process.env.WT_SESSION) // Windows Terminal | |
|| process.env.ConEmuTask === '{cmd::Cmder}' // ConEmu and cmder | |
|| process.env.TERM_PROGRAM === 'vscode' || process.env.TERM === 'xterm-256color' || process.env.TERM === 'alacritty'; | |
} | |
const allowed = isUnicodeSupported() | |
const SPACES = ' ' | |
const SUCCESS = allowed ? '✔' : '√' | |
const INFO = allowed ? 'ℹ' : 'i' | |
const WARNING = allowed ? '⚠' : '‼' | |
const ERROR = allowed ? '✖' : '×' | |
const colors = { | |
default: ['white', ''], | |
success: ['greenBright', `${SUCCESS}${SPACES}`], | |
info: ['cyanBright', `${INFO}${SPACES}`], | |
warning: ['yellowBright', `${WARNING}${SPACES}`], | |
error: ['redBright', `${ERROR}${SPACES}`] | |
} | |
function log(type, msg) { | |
const [color, prefix] = colors[type] || colors.default | |
console.log(`${styles[color].open}${prefix}${msg}${styles[color].close}`) | |
} | |
const success = log.bind(null, 'success') | |
const info = log.bind(null, 'info') | |
const warning = log.bind(null, 'warning') | |
const error = log.bind(null, 'error') | |
console.log('Nice logs') | |
success('Success! Yay it worked') | |
info('Info: Additional details here') | |
warning('Warning: Watch out') | |
error('Error: Oh no!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/chalk/ansi-styles/blob/main/index.js | |
// https://github.com/suchipi/pretty-print-error | |
const ansi = require('ansi-styles') | |
const process = require('process') | |
// via https://github.com/sindresorhus/is-unicode-supported/blob/main/index.js | |
function isUnicodeSupported() { | |
if (process.platform !== 'win32') return process.env.TERM !== 'linux' // Linux console (kernel) | |
return Boolean(process.env.CI) | |
|| Boolean(process.env.WT_SESSION) // Windows Terminal | |
|| process.env.ConEmuTask === '{cmd::Cmder}' // ConEmu and cmder | |
|| process.env.TERM_PROGRAM === 'vscode' || process.env.TERM === 'xterm-256color' || process.env.TERM === 'alacritty' | |
} | |
const isObject = (obj) => obj !== null && typeof obj === 'object' | |
function neverNull(obj) { | |
const match = (some, none = () => {}) => (obj !== null) ? some(obj) : none() | |
return new Proxy((some, none) => { | |
if (some) return some === 'string' ? '' : some | |
if (!some && !none) return obj | |
return match(some, none) | |
}, | |
{ | |
get: (target, key) => { | |
const obj = target() | |
if (isObject(obj)) return neverNull(obj[key]) | |
return neverNull() | |
}, | |
set: (target, key, val) => { | |
const obj = target() | |
if (isObject(obj)) obj[key] = val | |
return true | |
}, | |
}) | |
} | |
function safeColors(disableColors) { | |
return (disableColors) ? neverNull(ansi) : ansi | |
} | |
const allowed = isUnicodeSupported() | |
const styles = safeColors(true || process.env.DISABLE_COLOR) | |
const SPACES = ' ' | |
const SUCCESS = allowed ? '✔' : '√' | |
const INFO = allowed ? 'ℹ' : 'i' | |
const WARNING = allowed ? '⚠' : '‼' | |
const ERROR = allowed ? '✖' : '×' | |
const colors = { | |
default: ['white', ''], | |
success: ['greenBright', `${SUCCESS}${SPACES}`], | |
info: ['cyanBright', `${INFO}${SPACES}`], | |
warning: ['yellowBright', `${WARNING}${SPACES}`], | |
error: ['redBright', `${ERROR}${SPACES}`] | |
} | |
function log(type, msg) { | |
const [color, prefix] = colors[type] || colors.default | |
console.log(`${styles[color].open}${prefix}${msg}${styles[color].close}`) | |
} | |
const success = log.bind(null, 'success') | |
const info = log.bind(null, 'info') | |
const warning = log.bind(null, 'warning') | |
const error = log.bind(null, 'error') | |
/* | |
// Usage: | |
console.log('Nice logs') | |
success('Success! Yay it worked') | |
info('Info: Additional details here') | |
warning('Warning: Watch out') | |
error('Error: Oh no!') | |
/**/ | |
module.exports ={ | |
success, | |
info, | |
warning, | |
error, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment