From 5dae42e9c5c0e8efbcbbe1d5016b0cc01891b97d Mon Sep 17 00:00:00 2001 From: Stefano Azzolini Date: Sun, 17 May 2015 00:03:03 +0200 Subject: [PATCH 1/3] Added support for rendering style tagged strings --- .DS_Store | Bin 0 -> 6148 bytes index.js | 19 +++++++++++++++++++ readme.md | 32 ++++++++++++++++++++++++++++++++ test.js | 9 +++++++++ 4 files changed, 60 insertions(+) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0/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; diff --git a/readme.md b/readme.md index 147b1d1..9d95175 100644 --- a/readme.md +++ b/readme.md @@ -81,6 +81,12 @@ console.log(chalk.green('Hello %s'), name); //=> Hello Sindre ``` +You can also render colored text using an HTML-like string with tags. + +```js +var chalk = require('chalk'); +console.log(chalk.text('Error!')); +``` ## API @@ -144,6 +150,32 @@ if (!chalk.supportsColor) { } ``` +### chalk.colorize(string) + +Returns color styled text from a tagged string. + +Tag names are the same used for accessing colors in `chalk.styles`. + +Example: + +```js +var chalk = require('chalk'); + +console.log( chalk.colorize("Hello, this is a test!") ); +``` + +You can also use a shorthand for some modifiers : + +| Alias | Modifier | +|-------|---------------| +| b | bold | +| u | underline | +| i | italic | +| inv | inverse | +| h | hidden | +| s | strikethrough | +| d | dim | + ## Styles diff --git a/test.js b/test.js index b7359bf..94c02d9 100644 --- a/test.js +++ b/test.js @@ -137,3 +137,12 @@ describe('chalk.stripColor()', function () { assert.equal(chalk.stripColor(chalk.underline.red.bgGreen('foo')), 'foo'); }); }); + +describe('chalk.colorize()', function () { + it('should render correctly colors tagged within string', function () { + assert.equal(chalk.colorize("foo"), + '\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39m'); + assert.equal(chalk.colorize("foo"), + '\u001b[4m\u001b[31m\u001b[42mfoo\u001b[49m\u001b[39m\u001b[24m'); + }); +}); From 4988e0808fbba6316e3ad5c72f0c37a12dd6ae41 Mon Sep 17 00:00:00 2001 From: Stefano Azzolini Date: Sun, 17 May 2015 00:04:28 +0200 Subject: [PATCH 2/3] Delete .DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Sun, 17 May 2015 01:44:21 +0200 Subject: [PATCH 3/3] Typo in readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 9d95175..604629e 100644 --- a/readme.md +++ b/readme.md @@ -85,7 +85,7 @@ You can also render colored text using an HTML-like string with tags. ```js var chalk = require('chalk'); -console.log(chalk.text('Error!')); +console.log(chalk.colorize('Error!')); ``` ## API