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

View file

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

View file

@ -2,7 +2,7 @@
> 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.**
@ -13,6 +13,7 @@
- **Doesn't extend String.prototype**
- Expressive API
- Clean and focused
- Auto-detects color support
- Actively maintained
@ -37,6 +38,9 @@ console.log(chalk.blue('Hello') + 'World' + chalk.red('!'));
// compose multiple styles using the chainable API
console.log(chalk.blue.bgRed.bold('Hello world!'));
// nest styles
chalk.red('Hello' + chalk.underline.bgBlue('world') + '!');
```
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
var chalk = require('chalk');
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)
@ -90,7 +98,6 @@ Strip color from a string.
- bold
- italic
- underline
- blink
- inverse
- strikethrough
@ -104,7 +111,6 @@ Strip color from a string.
- magenta
- cyan
- white
- default
- gray
### Background colors
@ -117,7 +123,6 @@ Strip color from a string.
- bgMagenta
- bgCyan
- bgWhite
- bgDefault
## License

23
test.js
View file

@ -5,14 +5,25 @@ var chalk = require('./chalk');
describe('chalk', function () {
it('should style string', function () {
assert.equal(chalk.underline('foo'), '\x1b[4mfoo\x1b[0m');
assert.equal(chalk.red('foo'), '\x1b[31mfoo\x1b[0m');
assert.equal(chalk.bgRed('foo'), '\x1b[41mfoo\x1b[0m');
assert.equal(chalk.underline('foo'), '\x1b[4mfoo\x1b[24m');
assert.equal(chalk.red('foo'), '\x1b[31mfoo\x1b[39m');
assert.equal(chalk.bgRed('foo'), '\x1b[41mfoo\x1b[49m');
});
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.underline.red.bgGreen('foo'), '\x1b[42m\x1b[31m\x1b[4mfoo\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[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 () {
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');
});
});