ansicolorBETA
A quality library for the ANSI color/style management.
npm install ansicolor
Why another one?
Other tools lack consistency, failing to solve the simple hierarchy problem:
// most popular color utility console
WTF, bar
is not rendered red! It sucks. Ansicolor arranges styles in stack and reconstructs proper linear form from that stack:
nice // .nice for unsafe String extensions console
Nice!
Cross-platform rendering
Other tools provide output (rendering), but not input (parsing). Inspection of ANSI colors in arbitrary strings is essential when implementing cross-platform logging — that works not only in terminal, but in browsers too. Modern browsers support color logging with console.log
, but it does not understand ANSI colors — having a proprietary CSS-based format instead.
Ansicolor solves that problem by converting color codes to argument lists that are understandable by browser's consoles:
parsed = color parsedbrowserConsoleArguments /* = [ "%cfoo%cbar%cbaz", "", "font-weight: bold;font-style: underline;background:rgba(255,51,0,1);color:rgba(0,204,0,1);", "background:rgba(0,204,0,1);"] */ console // prints with colors in Chrome!
Crash course
String wrapping (safe):
color = console
String wrapping (unsafe):
nice console
All supported options:
'foreground colors' blackredgreenyellowbluemagentacyanwhite
'background colors' bgBlackbgRedbgGreenbgYellowbgBluebgMagentabgCyanbgWhite
'bright background colors' bgBrightBlackbgBrightRedbgBrightGreenbgBrightYellowbgBrightBluebgBrightMagentabgBrightCyanbgBrightWhite
'styles' brightdimitalicunderlineinverse // italic may lack support on your platform
Converting to CSS
Parsing arbitrary strings styled with ANSI escape codes:
parsed = color
Will return a pseudo-array of styled spans (iterable with for ... of
and convertable to an array with spread operator):
css: 'background:rgba(255,51,0,1);' text: 'foo' css: '' text: 'bar' )
Converting parsed array to argument list (acceptable by Chrome's console.log
):
console
Happy logging!