chalk 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/index.js +76 -0
  2. package/package.json +25 -27
  3. package/readme.md +89 -44
  4. package/chalk.js +0 -60
package/index.js ADDED
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+ var escapeStringRegexp = require('escape-string-regexp');
3
+ var ansiStyles = require('ansi-styles');
4
+ var stripAnsi = require('strip-ansi');
5
+ var hasAnsi = require('has-ansi');
6
+ var supportsColor = require('supports-color');
7
+ var defineProps = Object.defineProperties;
8
+ var chalk = module.exports;
9
+
10
+ var styles = (function () {
11
+ var ret = {};
12
+
13
+ ansiStyles.grey = ansiStyles.gray;
14
+
15
+ Object.keys(ansiStyles).forEach(function (key) {
16
+ ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
17
+
18
+ ret[key] = {
19
+ get: function () {
20
+ this._styles.push(key);
21
+ return this;
22
+ }
23
+ };
24
+ });
25
+
26
+ return ret;
27
+ })();
28
+
29
+ function applyStyle() {
30
+ // support varags, but simply cast to string in case there's only one arg
31
+ var str = arguments.length === 1 ? String(arguments[0]) : [].slice.call(arguments).join(' ');
32
+
33
+ if (!chalk.enabled || !str) {
34
+ return str;
35
+ }
36
+
37
+ var nestedStyles = applyStyle._styles;
38
+
39
+ for (var i = 0; i < nestedStyles.length; i++) {
40
+ var code = ansiStyles[nestedStyles[i]];
41
+ // Replace any instances already present with a re-opening code
42
+ // otherwise only the part of the string until said closing code
43
+ // will be colored, and the rest will simply be 'plain'.
44
+ str = code.open + str.replace(code.closeRe, code.open) + code.close;
45
+ }
46
+
47
+ return str;
48
+ }
49
+
50
+ function init() {
51
+ var ret = {};
52
+
53
+ Object.keys(styles).forEach(function (name) {
54
+ var style = defineProps(applyStyle, styles);
55
+ ret[name] = {
56
+ get: function () {
57
+ style._styles = [];
58
+ return style[name];
59
+ }
60
+ };
61
+ });
62
+
63
+ return ret;
64
+ }
65
+
66
+ defineProps(chalk, init());
67
+
68
+ chalk.styles = ansiStyles;
69
+ chalk.hasColor = hasAnsi;
70
+ chalk.stripColor = stripAnsi;
71
+ chalk.supportsColor = supportsColor;
72
+
73
+ // detect mode if not set manually
74
+ if (chalk.enabled === undefined) {
75
+ chalk.enabled = chalk.supportsColor;
76
+ }
package/package.json CHANGED
@@ -1,7 +1,23 @@
1
1
  {
2
2
  "name": "chalk",
3
- "version": "0.2.0",
4
- "description": "Terminal string styling done right",
3
+ "version": "0.5.0",
4
+ "description": "Terminal string styling done right. Created because the `colors` module does some really horrible things.",
5
+ "license": "MIT",
6
+ "repository": "sindresorhus/chalk",
7
+ "maintainers": [
8
+ "Sindre Sorhus <sindresorhus@gmail.com> (http://sindresorhus.com)",
9
+ "Joshua Appelman <joshua@jbna.nl>"
10
+ ],
11
+ "engines": {
12
+ "node": ">=0.10.0"
13
+ },
14
+ "scripts": {
15
+ "test": "mocha",
16
+ "bench": "matcha benchmark.js"
17
+ },
18
+ "files": [
19
+ "index.js"
20
+ ],
5
21
  "keywords": [
6
22
  "color",
7
23
  "colour",
@@ -23,33 +39,15 @@
23
39
  "command-line",
24
40
  "text"
25
41
  ],
26
- "homepage": "https://github.com/sindresorhus/chalk",
27
- "bugs": "https://github.com/sindresorhus/chalk/issues",
28
- "license": "MIT",
29
- "author": {
30
- "name": "Sindre Sorhus",
31
- "email": "sindresorhus@gmail.com",
32
- "url": "http://sindresorhus.com"
33
- },
34
- "files": [
35
- "chalk.js"
36
- ],
37
- "main": "chalk",
38
- "repository": {
39
- "type": "git",
40
- "url": "git://github.com/sindresorhus/chalk.git"
41
- },
42
- "scripts": {
43
- "test": "mocha"
44
- },
45
42
  "dependencies": {
46
- "has-color": "~0.1.0",
47
- "ansi-styles": "~0.2.0"
43
+ "ansi-styles": "^1.1.0",
44
+ "escape-string-regexp": "^1.0.0",
45
+ "has-ansi": "^0.1.0",
46
+ "strip-ansi": "^0.3.0",
47
+ "supports-color": "^0.2.0"
48
48
  },
49
49
  "devDependencies": {
50
- "mocha": "~1.12.0"
51
- },
52
- "engines": {
53
- "node": ">=0.8.0"
50
+ "matcha": "^0.5.0",
51
+ "mocha": "*"
54
52
  }
55
53
  }
package/readme.md CHANGED
@@ -1,49 +1,63 @@
1
- # chalk [![Build Status](https://secure.travis-ci.org/sindresorhus/chalk.png?branch=master)](http://travis-ci.org/sindresorhus/chalk)
1
+ # <img width="300" src="https://cdn.rawgit.com/sindresorhus/chalk/77ae94f63ab1ac61389b190e5a59866569d1a376/logo.svg" alt="chalk">
2
2
 
3
- > Terminal string styling done right.
3
+ > Terminal string styling done right
4
4
 
5
- [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.
5
+ [![Build Status](https://travis-ci.org/sindresorhus/chalk.svg?branch=master)](https://travis-ci.org/sindresorhus/chalk)
6
+ ![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg)
7
+
8
+ [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](https://github.com/yeoman/yo/issues/68). Although there are other ones, they either do too much or not enough.
6
9
 
7
10
  **Chalk is a clean and focused alternative.**
8
11
 
9
- ![screenshot](screenshot.png)
12
+ ![screenshot](https://github.com/sindresorhus/ansi-styles/raw/master/screenshot.png)
10
13
 
11
14
 
12
15
  ## Why
13
16
 
14
- - **Doesn't extend String.prototype**
17
+ - Highly performant
18
+ - Doesn't extend String.prototype
15
19
  - Expressive API
20
+ - Ability to nest styles
16
21
  - Clean and focused
17
22
  - Auto-detects color support
18
23
  - Actively maintained
24
+ - [Used by 1000+ modules](https://npmjs.org/browse/depended/chalk)
19
25
 
20
26
 
21
27
  ## Install
22
28
 
23
- Install with [npm](https://npmjs.org/package/chalk): `npm install --save chalk`
29
+ ```sh
30
+ $ npm install --save chalk
31
+ ```
24
32
 
25
33
 
26
- ## Example
34
+ ## Usage
27
35
 
28
- Chalk comes with an easy to use composable API where you just chain the styles you want.
36
+ Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
29
37
 
30
38
  ```js
31
39
  var chalk = require('chalk');
32
40
 
33
41
  // style a string
34
- console.log(chalk.blue('Hello world!'));
42
+ console.log( chalk.blue('Hello world!') );
35
43
 
36
44
  // combine styled and normal strings
37
- console.log(chalk.blue('Hello') + 'World' + chalk.red('!'));
45
+ console.log( chalk.blue('Hello'), 'World' + chalk.red('!') );
38
46
 
39
47
  // compose multiple styles using the chainable API
40
- console.log(chalk.blue.bgRed.bold('Hello world!'));
48
+ console.log( chalk.blue.bgRed.bold('Hello world!') );
49
+
50
+ // pass in multiple arguments
51
+ console.log( chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz') );
41
52
 
42
53
  // nest styles
43
- chalk.red('Hello' + chalk.underline.bgBlue('world') + '!');
54
+ console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') );
55
+
56
+ // nest styles of the same type even (color, underline, background)
57
+ console.log( chalk.green('I am a green line ' + chalk.blue('with a blue substring') + ' that becomes green again!') );
44
58
  ```
45
59
 
46
- You can easily define your own themes.
60
+ Easily define your own themes.
47
61
 
48
62
  ```js
49
63
  var chalk = require('chalk');
@@ -51,13 +65,24 @@ var error = chalk.bold.red;
51
65
  console.log(error('Error!'));
52
66
  ```
53
67
 
68
+ Take advantage of console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
69
+
70
+ ```js
71
+ var name = 'Sindre';
72
+ console.log(chalk.green('Hello %s'), name);
73
+ //=> Hello Sindre
74
+ ```
75
+
54
76
 
55
77
  ## API
56
78
 
57
- ### chalk.\<style\>\[.\<style\>...\](string)
79
+ ### chalk.`<style>[.<style>...](string, [string...])`
58
80
 
59
- Chain [styles](#styles) and call the last one as a method with a string argument.
81
+ Example: `chalk.red.bold.underline('Hello', 'world');`
60
82
 
83
+ Chain [styles](#styles) and call the last one as a method with a string argument. Order doesn't matter.
84
+
85
+ Multiple arguments will be separated by space.
61
86
 
62
87
  ### chalk.enabled
63
88
 
@@ -65,7 +90,7 @@ Color support is automatically detected, but you can override it.
65
90
 
66
91
  ### chalk.supportsColor
67
92
 
68
- Detect whether the terminal [supports color](https://github.com/sindresorhus/has-color).
93
+ Detect whether the terminal [supports color](https://github.com/sindresorhus/supports-color).
69
94
 
70
95
  Can be overridden by the user with the flags `--color` and `--no-color`.
71
96
 
@@ -75,56 +100,76 @@ Used internally and handled for you, but exposed for convenience.
75
100
 
76
101
  Exposes the styles as [ANSI escape codes](https://github.com/sindresorhus/ansi-styles).
77
102
 
103
+ Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with yours.
104
+
78
105
  ```js
79
106
  var chalk = require('chalk');
80
107
 
81
108
  console.log(chalk.styles.red);
82
- //=> ['\x1b[31m', '\x1b[39m']
109
+ //=> {open: '\u001b[31m', close: '\u001b[39m'}
83
110
 
84
- console.log(chalk.styles.red[0] + 'Hello' + chalk.styles.red[1]);
85
- // first item is the style escape code and second is the reset escape code
111
+ console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
86
112
  ```
87
113
 
114
+ ### chalk.hasColor(string)
115
+
116
+ Check whether a string [has color](https://github.com/sindresorhus/has-ansi).
117
+
88
118
  ### chalk.stripColor(string)
89
119
 
90
- Strip color from a string.
120
+ [Strip color](https://github.com/sindresorhus/strip-ansi) from a string.
121
+
122
+ Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
123
+
124
+ Example:
125
+
126
+ ```js
127
+ var chalk = require('chalk');
128
+ var styledString = getText();
129
+
130
+ if (!chalk.supportsColor) {
131
+ styledString = chalk.stripColor(styledString);
132
+ }
133
+ ```
91
134
 
92
135
 
93
136
  ## Styles
94
137
 
95
138
  ### General
96
139
 
97
- - reset
98
- - bold
99
- - italic
100
- - underline
101
- - inverse
102
- - strikethrough
140
+ - `reset`
141
+ - `bold`
142
+ - `dim`
143
+ - `italic` *(not widely supported)*
144
+ - `underline`
145
+ - `inverse`
146
+ - `hidden`
147
+ - `strikethrough` *(not widely supported)*
103
148
 
104
149
  ### Text colors
105
150
 
106
- - black
107
- - red
108
- - green
109
- - yellow
110
- - blue
111
- - magenta
112
- - cyan
113
- - white
114
- - gray
151
+ - `black`
152
+ - `red`
153
+ - `green`
154
+ - `yellow`
155
+ - `blue`
156
+ - `magenta`
157
+ - `cyan`
158
+ - `white`
159
+ - `gray`
115
160
 
116
161
  ### Background colors
117
162
 
118
- - bgBlack
119
- - bgRed
120
- - bgGreen
121
- - bgYellow
122
- - bgBlue
123
- - bgMagenta
124
- - bgCyan
125
- - bgWhite
163
+ - `bgBlack`
164
+ - `bgRed`
165
+ - `bgGreen`
166
+ - `bgYellow`
167
+ - `bgBlue`
168
+ - `bgMagenta`
169
+ - `bgCyan`
170
+ - `bgWhite`
126
171
 
127
172
 
128
173
  ## License
129
174
 
130
- MIT License • © [Sindre Sorhus](http://sindresorhus.com)
175
+ MIT © [Sindre Sorhus](http://sindresorhus.com)
package/chalk.js DELETED
@@ -1,60 +0,0 @@
1
- 'use strict';
2
- var ansi = require('ansi-styles');
3
- var defineProps = Object.defineProperties;
4
-
5
- var styles = (function () {
6
- var ret = {};
7
-
8
- Object.keys(ansi).forEach(function (key) {
9
- ret[key] = {
10
- get: function () {
11
- this._styles.push(key);
12
- return this;
13
- }
14
- };
15
- });
16
-
17
- return ret;
18
- })();
19
-
20
- var chalk = module.exports = defineProps({}, init());
21
-
22
- function init() {
23
- var ret = {};
24
-
25
- Object.keys(styles).forEach(function (name) {
26
- ret[name] = {
27
- get: function () {
28
- var obj = defineProps(function self(str) {
29
- if (!chalk.enabled) {
30
- return str;
31
- }
32
-
33
- return self._styles.reduce(function (str, name) {
34
- var code = ansi[name];
35
- return code[0] + (str || '') + code[1];
36
- }, str);
37
- }, styles);
38
-
39
- obj._styles = [];
40
-
41
- return obj[name];
42
- }
43
- }
44
- });
45
-
46
- return ret;
47
- }
48
-
49
- chalk.styles = ansi;
50
-
51
- chalk.stripColor = function (str) {
52
- return str.replace(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/g, '');
53
- };
54
-
55
- chalk.supportsColor = require('has-color');
56
-
57
- // detect mode if not set manually
58
- if (chalk.enabled === undefined) {
59
- chalk.enabled = chalk.supportsColor;
60
- }