commit cffc3552b0853c75f41b92ed2c032988df018442 Author: Sindre Sorhus Date: Sat Aug 3 02:16:26 2013 +0200 init diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b0d7fd9 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..ac35c13 --- /dev/null +++ b/.jshintrc @@ -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 +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2cdacaf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - '0.10' + - '0.8' diff --git a/chalk.js b/chalk.js new file mode 100644 index 0000000..d4887e1 --- /dev/null +++ b/chalk.js @@ -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; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..9433840 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..8fba39d --- /dev/null +++ b/readme.md @@ -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.\\[.\...\](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) diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..a794efa Binary files /dev/null and b/screenshot.png differ diff --git a/test.js b/test.js new file mode 100644 index 0000000..ba37141 --- /dev/null +++ b/test.js @@ -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'); + }); +});