This commit is contained in:
Sindre Sorhus 2013-08-03 02:16:26 +02:00
commit cffc3552b0
10 changed files with 319 additions and 0 deletions

12
.editorconfig Normal file
View file

@ -0,0 +1,12 @@
# editorconfig.org
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
* text=auto

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

20
.jshintrc Normal file
View file

@ -0,0 +1,20 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true
}

4
.travis.yml Normal file
View file

@ -0,0 +1,4 @@
language: node_js
node_js:
- '0.10'
- '0.8'

64
chalk.js Normal file
View file

@ -0,0 +1,64 @@
'use strict';
var ansi = require('ansi-styles');
var defineProps = Object.defineProperties;
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);
return this;
}
};
});
return ret;
})();
var chalk = module.exports = defineProps({}, init());
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'];
}, styles);
obj._styles = [];
return obj[name];
}
}
});
return ret;
}
chalk.styles = ansi;
chalk.stripColor = function (str) {
return str.replace(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/g, '');
};
chalk.supportsColor = require('has-color');
// detect mode if not set manually
if (chalk.enabled === undefined) {
chalk.enabled = chalk.supportsColor;
}

55
package.json Normal file
View file

@ -0,0 +1,55 @@
{
"name": "chalk",
"version": "0.0.0",
"description": "Terminal string styling done right",
"keywords": [
"color",
"colour",
"colors",
"terminal",
"console",
"cli",
"string",
"ansi",
"styles",
"tty",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"log",
"logging",
"command-line",
"text"
],
"homepage": "https://github.com/sindresorhus/chalk",
"bugs": "https://github.com/sindresorhus/chalk/issues",
"license": "MIT",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
},
"files": [
"chalk.js"
],
"main": "chalk",
"repository": {
"type": "git",
"url": "git://github.com/sindresorhus/chalk.git"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"has-color": "~0.1.0",
"ansi-styles": "~0.1.0"
},
"devDependencies": {
"mocha": "~1.12.0"
},
"engines": {
"node": ">=0.8.0"
}
}

125
readme.md Normal file
View file

@ -0,0 +1,125 @@
# chalk [![Build Status](https://secure.travis-ci.org/sindresorhus/chalk.png?branch=master)](http://travis-ci.org/sindresorhus/chalk)
> 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.
**Chalk is a clean and focused alternative.**
![screenshot](screenshot.png)
## Why
- **Doesn't extend String.prototype**
- Expressive API
- Auto-detects color support
- Actively maintained
## Install
Install with [npm](https://npmjs.org/package/chalk): `npm install --save chalk`
## Example
Chalk comes with an easy to use composable API where you just chain the styles you want.
```js
var chalk = require('chalk');
// style a string
console.log(chalk.blue('Hello world!'));
// combine styled and normal strings
console.log(chalk.blue('Hello') + 'World' + chalk.red('!'));
// compose multiple styles using the chainable API
console.log(chalk.blue.bgRed.bold('Hello world!'));
```
You can easily define your own themes.
```js
var chalk = require('chalk');
var error = chalk.bold.red;
console.log(error('Error!'));
```
## API
### chalk.\<style\>\[.\<style\>...\](string)
Chain [styles](#Styles) and call the last one as a method with a string argument.
### chalk.enabled
Color support is automatically detected, but you can override it.
### chalk.supportsColor
Detect whether the terminal [supports color](https://github.com/sindresorhus/has-color).
Can be overridden by the user with the flags `--color` and `--no-color`.
Used internally and handled for you, but exposed for convenience.
### chalk.styles
Exposes the styles as [ANSI escape codes](https://github.com/sindresorhus/ansi-styles).
```js
var chalk = require('chalk');
console.log(chalk.styles.red);
//=> \x1b[31m
```
### chalk.stripColor(string)
Strip color from a string.
## Styles
### General
- reset
- bold
- italic
- underline
- blink
- inverse
- strikethrough
### Text colors
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- default
- gray
### Background colors
- bgBlack
- bgRed
- bgGreen
- bgYellow
- bgBlue
- bgMagenta
- bgCyan
- bgWhite
- bgDefault
## License
MIT License • © [Sindre Sorhus](http://sindresorhus.com)

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

37
test.js Normal file
View file

@ -0,0 +1,37 @@
/*global describe, it */
'use strict';
var assert = require('assert');
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');
});
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');
});
});
describe('chalk.enabled', function () {
it('should not output colors when manually disabled', function () {
chalk.enabled = false;
assert.equal(chalk.red('foo'), 'foo');
chalk.enabled = true;
});
});
describe('chalk.styles', function () {
it('should expose the styles as ANSI escape codes', function () {
assert.equal(chalk.styles.red, '\x1b[31m');
});
});
describe('chalk.stripColor()', function () {
it('should strip color from string', function () {
assert.equal(chalk.stripColor(chalk.underline.red.bgGreen('foo')), 'foo');
});
});