sassy-log
Version:
Logging, but with sass, satire, and some serious fun. A developer-first NPM package that replaces boring console.log() statements with snarky, sarcastic, or corporate-smooth one-liners.
99 lines (85 loc) • 2.31 kB
JavaScript
/**
* Color utility functions for cross-platform terminal colors
* Works on Windows, macOS, Linux, and various terminals
*/
// ANSI color codes
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
// Text colors
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
// Bright text colors
brightRed: '\x1b[91m',
brightGreen: '\x1b[92m',
brightYellow: '\x1b[93m',
brightBlue: '\x1b[94m',
brightMagenta: '\x1b[95m',
brightCyan: '\x1b[96m'
};
// Log type to color mapping
const logColors = {
info: colors.cyan,
success: colors.green,
warn: colors.yellow,
error: colors.red
};
/**
* Check if the current environment supports colors
*/
function supportsColor() {
// Check if we're in a browser environment
if (typeof window !== 'undefined') {
return false; // Browser console handles colors differently
}
// Check Node.js environment
if (typeof process !== 'undefined') {
// Check for CI environments that support colors
if (process.env.CI && ['GITHUB_ACTIONS', 'TRAVIS', 'CIRCLECI', 'GITLAB_CI'].some(ci => process.env[ci])) {
return true;
}
// Check if colors are explicitly disabled
if (process.env.NO_COLOR || process.env.NODE_DISABLE_COLORS) {
return false;
}
// Check if colors are explicitly enabled
if (process.env.FORCE_COLOR) {
return true;
}
// Check if we have a TTY
if (process.stdout && process.stdout.isTTY) {
return true;
}
// Check for common terminals that support colors
const term = process.env.TERM;
if (term && (term.includes('color') || term.includes('256') || term === 'xterm' || term === 'screen')) {
return true;
}
// Check for Windows Terminal, VS Code, and other modern terminals
if (process.env.WT_SESSION || process.env.TERM_PROGRAM === 'vscode' || process.env.COLORTERM) {
return true;
}
}
return false;
}
/**
* Get color code for log type
*/
function getColorCode(type) {
return logColors[type] || colors.white;
}
// Export color utilities
module.exports = {
colors,
logColors,
supportsColor,
getColorCode,
resetColor: colors.reset
};