Adds 256 color support.

This commit is contained in:
Joshua Appelman 2014-07-14 00:23:49 +02:00
parent 03b92b1bb8
commit 79c6bdcfd8
3 changed files with 21 additions and 0 deletions

View file

@ -1,5 +1,6 @@
'use strict';
var escapeStringRegexp = require('escape-string-regexp');
var ansi8bit = require('ansi-8-bit');
var ansiStyles = require('ansi-styles');
var stripAnsi = require('strip-ansi');
var hasAnsi = require('has-ansi');
@ -84,6 +85,14 @@ function init() {
defineProps(chalk, init());
chalk.foreground = function (r, g, b, str) {
return ansi8bit.fg.getRgb(r, g, b) + str + ansi8bit.reset;
};
chalk.background = function (r, g, b, str) {
return ansi8bit.bg.getRgb(r, g, b) + str + ansi8bit.reset;
};
chalk.styles = ansiStyles;
chalk.hasColor = hasAnsi;
chalk.stripColor = stripAnsi;

View file

@ -40,6 +40,7 @@
"text"
],
"dependencies": {
"ansi-8-bit": "^1.0.1",
"ansi-styles": "^1.1.0",
"escape-string-regexp": "^1.0.1",
"has-ansi": "^0.1.0",

11
test.js
View file

@ -86,3 +86,14 @@ describe('chalk.stripColor()', function () {
assert.equal(chalk.stripColor(chalk.underline.red.bgGreen('foo')), 'foo');
});
});
describe('chalk.foreground() and chalk.background()', function () {
it('should style according to their given rgb values', function () {
assert.equal(chalk.foreground(2, 5, 3, 'foobar'), '\u001b[;38;5;121mfoobar\u001b[0m');
});
it('should play nice with other chalk styles', function () {
assert.equal(chalk.background(1, 3, 3, chalk.red("Unicorns, attack!")), '\u001b[;48;5;73m\u001b[31mUnicorns, attack!\u001b[39m\u001b[0m');
});
});