Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Last active March 11, 2022 21:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DavidWells/91db5468684d09dc5d6a15e043af93d2 to your computer and use it in GitHub Desktop.
Save DavidWells/91db5468684d09dc5d6a15e043af93d2 to your computer and use it in GitHub Desktop.
Simple tiny colored logged for CLIs
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!')
// 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