A little function I wrote to make using ANSI Escape sequences a bit more easy/pretty to use in javascript.
Colorize parses HTML style tags to insert ANSI sequences in their position.
Here is a comparison between using colorize and raw escape sequences.
ANSI Escape Sequences:
console.log('\x1b[33mHello \x1b[35mWorld!\x1b[0m');Colorize:
colorize('<yellow>Hello <magenta>World!<clear>');
// OR
'<yellow>Hello <magenta>World!<clear>'.colorize();To use colorize simply download the script from the github website directly or from the command line:
wget https://raw.githubusercontent.com/ajekt/colorize/master/colorize.js
// OR
curl https://raw.githubusercontent.com/ajekt/colorize/master/colorize.js > colorize.js
NOTE Colorize is still pretty rough around the edges, if you find any problems make an issue or pull request.
All tags are encapsulated in angle brackets <tag> just like HTML. Using a closing tag </tag> will remove the tags effect.
This will remove all effects, excluding bright mode and background mode.
Example:
colorize('<green>Very cool<clear> text')Note that there is no need to close the <green> tag after using clear.
Bright mode will increase the brightness of all selected text inside the tag.
This function changes the values of the colors tags below, which could lead to some unexpected results.
It is being worked on to make this mode have more consistent behavior.
colorize('<b><yellow>Bright Yellow</yellow></b>')Background mode will turn all colors inside the tag to background colors.
colorize('<bg><red>Red Background</red></bg>')
// OR
'<bg><red>Red Background</red></bg>'.colorize();Adds a bold effect to text enclosed in the tag.
Dims the color of the text, not as commonly supported as bold.
Adds a slant effect to text, not commonly supported.
Adds an underline the text.
Reverses the foreground and background color of the text.
Adds a strike through the selected text.
| Tag Names |
|---|
| black |
| red |
| green |
| yellow |
| blue |
| magenta |
| cyan |
| white |
Returns the length of actual text inside a string which uses colorize syntax.
Example:
console.log('<red>red</red>'.getLength());
// Output: 3