Add support for nested styles

This commit is contained in:
Sindre Sorhus 2013-08-03 18:47:47 +02:00
parent d60eeb9df3
commit c323bf0caf
4 changed files with 33 additions and 21 deletions

View file

@ -6,11 +6,9 @@ var styles = (function () {
var ret = {}; var ret = {};
Object.keys(ansi).forEach(function (key) { Object.keys(ansi).forEach(function (key) {
var code = ansi[key];
ret[key] = { ret[key] = {
enumerable: true,
get: function () { get: function () {
this._styles.push(code); this._styles.push(key);
return this; return this;
} }
}; };
@ -25,19 +23,17 @@ function init() {
var ret = {}; var ret = {};
Object.keys(styles).forEach(function (name) { Object.keys(styles).forEach(function (name) {
var code = styles[name];
ret[name] = { ret[name] = {
enumerable: true,
get: function () { get: function () {
var obj = defineProps(function self(str) { var obj = defineProps(function self(str) {
if (!chalk.enabled) { if (!chalk.enabled) {
return str; return str;
} }
return self._styles.reduce(function (str, code) { return self._styles.reduce(function (str, name) {
return code + (str || ''); var code = ansi[name];
}, str) + ansi['reset']; return code[0] + (str || '') + code[1];
}, str);
}, styles); }, styles);
obj._styles = []; obj._styles = [];

View file

@ -44,7 +44,7 @@
}, },
"dependencies": { "dependencies": {
"has-color": "~0.1.0", "has-color": "~0.1.0",
"ansi-styles": "~0.1.0" "ansi-styles": "~0.2.0"
}, },
"devDependencies": { "devDependencies": {
"mocha": "~1.12.0" "mocha": "~1.12.0"

View file

@ -2,7 +2,7 @@
> Terminal string styling done right. > Terminal string styling done right.
[colors.js](https://github.com/Marak/colors.js) is currently the most popular coloring module, but it has serious deficiencies like extending String.prototype which causes all kinds of problems. Although there are other ones, they either do too much or not enough. [colors.js](https://github.com/Marak/colors.js) is currently the most popular string styling module, but it has serious deficiencies like extending String.prototype which causes all kinds of problems. Although there are other ones, they either do too much or not enough.
**Chalk is a clean and focused alternative.** **Chalk is a clean and focused alternative.**
@ -13,6 +13,7 @@
- **Doesn't extend String.prototype** - **Doesn't extend String.prototype**
- Expressive API - Expressive API
- Clean and focused
- Auto-detects color support - Auto-detects color support
- Actively maintained - Actively maintained
@ -37,6 +38,9 @@ console.log(chalk.blue('Hello') + 'World' + chalk.red('!'));
// compose multiple styles using the chainable API // compose multiple styles using the chainable API
console.log(chalk.blue.bgRed.bold('Hello world!')); console.log(chalk.blue.bgRed.bold('Hello world!'));
// nest styles
chalk.red('Hello' + chalk.underline.bgBlue('world') + '!');
``` ```
You can easily define your own themes. You can easily define your own themes.
@ -73,8 +77,12 @@ Exposes the styles as [ANSI escape codes](https://github.com/sindresorhus/ansi-s
```js ```js
var chalk = require('chalk'); var chalk = require('chalk');
console.log(chalk.styles.red); console.log(chalk.styles.red);
//=> \x1b[31m //=> ['\x1b[31m', '\x1b[39m']
console.log(chalk.styles.red[0] + 'Hello' + chalk.styles.red[1]);
// first item is the style escape code and second is the reset escape code
``` ```
### chalk.stripColor(string) ### chalk.stripColor(string)
@ -90,7 +98,6 @@ Strip color from a string.
- bold - bold
- italic - italic
- underline - underline
- blink
- inverse - inverse
- strikethrough - strikethrough
@ -104,7 +111,6 @@ Strip color from a string.
- magenta - magenta
- cyan - cyan
- white - white
- default
- gray - gray
### Background colors ### Background colors
@ -117,7 +123,6 @@ Strip color from a string.
- bgMagenta - bgMagenta
- bgCyan - bgCyan
- bgWhite - bgWhite
- bgDefault
## License ## License

23
test.js
View file

@ -5,14 +5,25 @@ var chalk = require('./chalk');
describe('chalk', function () { describe('chalk', function () {
it('should style string', function () { it('should style string', function () {
assert.equal(chalk.underline('foo'), '\x1b[4mfoo\x1b[0m'); assert.equal(chalk.underline('foo'), '\x1b[4mfoo\x1b[24m');
assert.equal(chalk.red('foo'), '\x1b[31mfoo\x1b[0m'); assert.equal(chalk.red('foo'), '\x1b[31mfoo\x1b[39m');
assert.equal(chalk.bgRed('foo'), '\x1b[41mfoo\x1b[0m'); assert.equal(chalk.bgRed('foo'), '\x1b[41mfoo\x1b[49m');
}); });
it('should support applying multiple styles at once', function () { it('should support applying multiple styles at once', function () {
assert.equal(chalk.red.bgGreen.underline('foo'), '\x1b[4m\x1b[42m\x1b[31mfoo\x1b[0m'); assert.equal(chalk.red.bgGreen.underline('foo'), '\x1b[4m\x1b[42m\x1b[31mfoo\x1b[39m\x1b[49m\x1b[24m');
assert.equal(chalk.underline.red.bgGreen('foo'), '\x1b[42m\x1b[31m\x1b[4mfoo\x1b[0m'); assert.equal(chalk.underline.red.bgGreen('foo'), '\x1b[42m\x1b[31m\x1b[4mfoo\x1b[24m\x1b[39m\x1b[49m');
});
it('should support nesting styles', function () {
assert.equal(
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
'\x1b[31mfoo\x1b[44m\x1b[4mbar\x1b[24m\x1b[49m!\x1b[39m'
);
});
it('should reset all styles with `.reset()`', function () {
assert.equal(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\x1b[0m\x1b[4m\x1b[42m\x1b[31mfoo\x1b[39m\x1b[49m\x1b[24mfoo\x1b[0m');
}); });
}); });
@ -26,7 +37,7 @@ describe('chalk.enabled', function () {
describe('chalk.styles', function () { describe('chalk.styles', function () {
it('should expose the styles as ANSI escape codes', function () { it('should expose the styles as ANSI escape codes', function () {
assert.equal(chalk.styles.red, '\x1b[31m'); assert.equal(chalk.styles.red[0], '\x1b[31m');
}); });
}); });