Added support for rendering style tagged strings

This commit is contained in:
Stefano Azzolini 2015-05-17 00:03:03 +02:00
parent e3272e8449
commit 5dae42e9c5
4 changed files with 60 additions and 0 deletions

View file

@ -77,6 +77,24 @@ function applyStyle() {
return str;
}
function colorize(text){
// Permit a map of aliases for long styles
var styleAliases = {
'b' : 'bold',
'u' : 'underline',
'i' : 'italic',
'inv' : 'inverse',
'h' : 'hidden',
's' : 'strikethrough',
'd' : 'dim'
};
return text.replace(/<(\/?)(\w+)>/g,function(full,closed,tag){
var color = ansiStyles[(tag in styleAliases) ? styleAliases[tag] : tag];
return color ? color[closed ? 'close' : 'open'] : full;
});
};
function init() {
var ret = {};
@ -98,3 +116,4 @@ module.exports.styles = ansiStyles;
module.exports.hasColor = hasAnsi;
module.exports.stripColor = stripAnsi;
module.exports.supportsColor = supportsColor;
module.exports.colorize = colorize;