Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 3, 2013
0 parents commit cffc355
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- '0.10'
- '0.8'
64 changes: 64 additions & 0 deletions chalk.js
Original file line number Diff line number Diff line change
@@ -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 changes: 55 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 125 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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)
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit cffc355

Please sign in to comment.