chalk 0.5.0 → 1.1.1

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 +62 -22
  2. package/license +21 -0
  3. package/package.json +31 -14
  4. package/readme.md +65 -27
package/index.js CHANGED
@@ -5,20 +5,27 @@ var stripAnsi = require('strip-ansi');
5
5
  var hasAnsi = require('has-ansi');
6
6
  var supportsColor = require('supports-color');
7
7
  var defineProps = Object.defineProperties;
8
- var chalk = module.exports;
8
+ var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
9
+
10
+ function Chalk(options) {
11
+ // detect mode if not set manually
12
+ this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
13
+ }
14
+
15
+ // use bright blue on Windows as the normal blue color is illegible
16
+ if (isSimpleWindowsTerm) {
17
+ ansiStyles.blue.open = '\u001b[94m';
18
+ }
9
19
 
10
20
  var styles = (function () {
11
21
  var ret = {};
12
22
 
13
- ansiStyles.grey = ansiStyles.gray;
14
-
15
23
  Object.keys(ansiStyles).forEach(function (key) {
16
24
  ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
17
25
 
18
26
  ret[key] = {
19
27
  get: function () {
20
- this._styles.push(key);
21
- return this;
28
+ return build.call(this, this._styles.concat(key));
22
29
  }
23
30
  };
24
31
  });
@@ -26,24 +33,63 @@ var styles = (function () {
26
33
  return ret;
27
34
  })();
28
35
 
36
+ var proto = defineProps(function chalk() {}, styles);
37
+
38
+ function build(_styles) {
39
+ var builder = function () {
40
+ return applyStyle.apply(builder, arguments);
41
+ };
42
+
43
+ builder._styles = _styles;
44
+ builder.enabled = this.enabled;
45
+ // __proto__ is used because we must return a function, but there is
46
+ // no way to create a function with a different prototype.
47
+ /* eslint-disable no-proto */
48
+ builder.__proto__ = proto;
49
+
50
+ return builder;
51
+ }
52
+
29
53
  function applyStyle() {
30
54
  // 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(' ');
55
+ var args = arguments;
56
+ var argsLen = args.length;
57
+ var str = argsLen !== 0 && String(arguments[0]);
58
+
59
+ if (argsLen > 1) {
60
+ // don't slice `arguments`, it prevents v8 optimizations
61
+ for (var a = 1; a < argsLen; a++) {
62
+ str += ' ' + args[a];
63
+ }
64
+ }
32
65
 
33
- if (!chalk.enabled || !str) {
66
+ if (!this.enabled || !str) {
34
67
  return str;
35
68
  }
36
69
 
37
- var nestedStyles = applyStyle._styles;
70
+ var nestedStyles = this._styles;
71
+ var i = nestedStyles.length;
72
+
73
+ // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
74
+ // see https://github.com/chalk/chalk/issues/58
75
+ // If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
76
+ var originalDim = ansiStyles.dim.open;
77
+ if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
78
+ ansiStyles.dim.open = '';
79
+ }
38
80
 
39
- for (var i = 0; i < nestedStyles.length; i++) {
81
+ while (i--) {
40
82
  var code = ansiStyles[nestedStyles[i]];
83
+
41
84
  // Replace any instances already present with a re-opening code
42
85
  // otherwise only the part of the string until said closing code
43
86
  // will be colored, and the rest will simply be 'plain'.
44
87
  str = code.open + str.replace(code.closeRe, code.open) + code.close;
45
88
  }
46
89
 
90
+ // Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
91
+ ansiStyles.dim.open = originalDim;
92
+
47
93
  return str;
48
94
  }
49
95
 
@@ -51,11 +97,9 @@ function init() {
51
97
  var ret = {};
52
98
 
53
99
  Object.keys(styles).forEach(function (name) {
54
- var style = defineProps(applyStyle, styles);
55
100
  ret[name] = {
56
101
  get: function () {
57
- style._styles = [];
58
- return style[name];
102
+ return build.call(this, [name]);
59
103
  }
60
104
  };
61
105
  });
@@ -63,14 +107,10 @@ function init() {
63
107
  return ret;
64
108
  }
65
109
 
66
- defineProps(chalk, init());
110
+ defineProps(Chalk.prototype, init());
67
111
 
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
- }
112
+ module.exports = new Chalk();
113
+ module.exports.styles = ansiStyles;
114
+ module.exports.hasColor = hasAnsi;
115
+ module.exports.stripColor = stripAnsi;
116
+ module.exports.supportsColor = supportsColor;
package/license ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/package.json CHANGED
@@ -1,19 +1,22 @@
1
1
  {
2
2
  "name": "chalk",
3
- "version": "0.5.0",
4
- "description": "Terminal string styling done right. Created because the `colors` module does some really horrible things.",
3
+ "version": "1.1.1",
4
+ "description": "Terminal string styling done right. Much color.",
5
5
  "license": "MIT",
6
- "repository": "sindresorhus/chalk",
6
+ "repository": "chalk/chalk",
7
7
  "maintainers": [
8
- "Sindre Sorhus <sindresorhus@gmail.com> (http://sindresorhus.com)",
9
- "Joshua Appelman <joshua@jbna.nl>"
8
+ "Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)",
9
+ "Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)",
10
+ "JD Ballard <i.am.qix@gmail.com> (github.com/qix-)"
10
11
  ],
11
12
  "engines": {
12
13
  "node": ">=0.10.0"
13
14
  },
14
15
  "scripts": {
15
- "test": "mocha",
16
- "bench": "matcha benchmark.js"
16
+ "test": "xo && mocha",
17
+ "bench": "matcha benchmark.js",
18
+ "coverage": "nyc npm test && nyc report",
19
+ "coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
17
20
  },
18
21
  "files": [
19
22
  "index.js"
@@ -26,7 +29,9 @@
26
29
  "console",
27
30
  "cli",
28
31
  "string",
32
+ "str",
29
33
  "ansi",
34
+ "style",
30
35
  "styles",
31
36
  "tty",
32
37
  "formatting",
@@ -40,14 +45,26 @@
40
45
  "text"
41
46
  ],
42
47
  "dependencies": {
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
+ "ansi-styles": "^2.1.0",
49
+ "escape-string-regexp": "^1.0.2",
50
+ "has-ansi": "^2.0.0",
51
+ "strip-ansi": "^3.0.0",
52
+ "supports-color": "^2.0.0"
48
53
  },
49
54
  "devDependencies": {
50
- "matcha": "^0.5.0",
51
- "mocha": "*"
55
+ "coveralls": "^2.11.2",
56
+ "matcha": "^0.6.0",
57
+ "mocha": "*",
58
+ "nyc": "^3.0.0",
59
+ "require-uncached": "^1.0.2",
60
+ "resolve-from": "^1.0.0",
61
+ "semver": "^4.3.3",
62
+ "xo": "*"
63
+ },
64
+ "xo": {
65
+ "envs": [
66
+ "node",
67
+ "mocha"
68
+ ]
52
69
  }
53
70
  }
package/readme.md CHANGED
@@ -1,32 +1,41 @@
1
- # <img width="300" src="https://cdn.rawgit.com/sindresorhus/chalk/77ae94f63ab1ac61389b190e5a59866569d1a376/logo.svg" alt="chalk">
1
+ <h1 align="center">
2
+ <br>
3
+ <br>
4
+ <img width="360" src="https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk">
5
+ <br>
6
+ <br>
7
+ <br>
8
+ </h1>
2
9
 
3
10
  > Terminal string styling done right
4
11
 
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)
12
+ [![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk)
13
+ [![Coverage Status](https://coveralls.io/repos/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/r/chalk/chalk?branch=master)
14
+ [![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4)
7
15
 
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.
16
+
17
+ [colors.js](https://github.com/Marak/colors.js) used to be 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.
9
18
 
10
19
  **Chalk is a clean and focused alternative.**
11
20
 
12
- ![screenshot](https://github.com/sindresorhus/ansi-styles/raw/master/screenshot.png)
21
+ ![](https://github.com/chalk/ansi-styles/raw/master/screenshot.png)
13
22
 
14
23
 
15
24
  ## Why
16
25
 
17
26
  - Highly performant
18
- - Doesn't extend String.prototype
27
+ - Doesn't extend `String.prototype`
19
28
  - Expressive API
20
29
  - Ability to nest styles
21
30
  - Clean and focused
22
31
  - Auto-detects color support
23
32
  - Actively maintained
24
- - [Used by 1000+ modules](https://npmjs.org/browse/depended/chalk)
33
+ - [Used by ~4500 modules](https://www.npmjs.com/browse/depended/chalk) as of July 15, 2015
25
34
 
26
35
 
27
36
  ## Install
28
37
 
29
- ```sh
38
+ ```
30
39
  $ npm install --save chalk
31
40
  ```
32
41
 
@@ -39,22 +48,26 @@ Chalk comes with an easy to use composable API where you just chain and nest the
39
48
  var chalk = require('chalk');
40
49
 
41
50
  // style a string
42
- console.log( chalk.blue('Hello world!') );
51
+ chalk.blue('Hello world!');
43
52
 
44
53
  // combine styled and normal strings
45
- console.log( chalk.blue('Hello'), 'World' + chalk.red('!') );
54
+ chalk.blue('Hello') + 'World' + chalk.red('!');
46
55
 
47
56
  // compose multiple styles using the chainable API
48
- console.log( chalk.blue.bgRed.bold('Hello world!') );
57
+ chalk.blue.bgRed.bold('Hello world!');
49
58
 
50
59
  // pass in multiple arguments
51
- console.log( chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz') );
60
+ chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
52
61
 
53
62
  // nest styles
54
- console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') );
63
+ chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
55
64
 
56
65
  // 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!') );
66
+ chalk.green(
67
+ 'I am a green line ' +
68
+ chalk.blue.underline.bold('with a blue substring') +
69
+ ' that becomes green again!'
70
+ );
58
71
  ```
59
72
 
60
73
  Easily define your own themes.
@@ -80,27 +93,31 @@ console.log(chalk.green('Hello %s'), name);
80
93
 
81
94
  Example: `chalk.red.bold.underline('Hello', 'world');`
82
95
 
83
- Chain [styles](#styles) and call the last one as a method with a string argument. Order doesn't matter.
96
+ Chain [styles](#styles) and call the last one as a method with a string argument. Order doesn't matter, and later styles take precedent in case of a conflict. This simply means that `Chalk.red.yellow.green` is equivalent to `Chalk.green`.
84
97
 
85
98
  Multiple arguments will be separated by space.
86
99
 
87
100
  ### chalk.enabled
88
101
 
89
- Color support is automatically detected, but you can override it.
102
+ Color support is automatically detected, but you can override it by setting the `enabled` property. You should however only do this in your own code as it applies globally to all chalk consumers.
90
103
 
91
- ### chalk.supportsColor
104
+ If you need to change this in a reusable module create a new instance:
105
+
106
+ ```js
107
+ var ctx = new chalk.constructor({enabled: false});
108
+ ```
92
109
 
93
- Detect whether the terminal [supports color](https://github.com/sindresorhus/supports-color).
110
+ ### chalk.supportsColor
94
111
 
95
- Can be overridden by the user with the flags `--color` and `--no-color`.
112
+ Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
96
113
 
97
- Used internally and handled for you, but exposed for convenience.
114
+ Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, add an environment variable `FORCE_COLOR` with any value to force color. Trumps `--no-color`.
98
115
 
99
116
  ### chalk.styles
100
117
 
101
- Exposes the styles as [ANSI escape codes](https://github.com/sindresorhus/ansi-styles).
118
+ Exposes the styles as [ANSI escape codes](https://github.com/chalk/ansi-styles).
102
119
 
103
- Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with yours.
120
+ Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own.
104
121
 
105
122
  ```js
106
123
  var chalk = require('chalk');
@@ -113,11 +130,11 @@ console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
113
130
 
114
131
  ### chalk.hasColor(string)
115
132
 
116
- Check whether a string [has color](https://github.com/sindresorhus/has-ansi).
133
+ Check whether a string [has color](https://github.com/chalk/has-ansi).
117
134
 
118
135
  ### chalk.stripColor(string)
119
136
 
120
- [Strip color](https://github.com/sindresorhus/strip-ansi) from a string.
137
+ [Strip color](https://github.com/chalk/strip-ansi) from a string.
121
138
 
122
139
  Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
123
140
 
@@ -135,7 +152,7 @@ if (!chalk.supportsColor) {
135
152
 
136
153
  ## Styles
137
154
 
138
- ### General
155
+ ### Modifiers
139
156
 
140
157
  - `reset`
141
158
  - `bold`
@@ -146,13 +163,13 @@ if (!chalk.supportsColor) {
146
163
  - `hidden`
147
164
  - `strikethrough` *(not widely supported)*
148
165
 
149
- ### Text colors
166
+ ### Colors
150
167
 
151
168
  - `black`
152
169
  - `red`
153
170
  - `green`
154
171
  - `yellow`
155
- - `blue`
172
+ - `blue` *(on Windows the bright version is used as normal blue is illegible)*
156
173
  - `magenta`
157
174
  - `cyan`
158
175
  - `white`
@@ -170,6 +187,27 @@ if (!chalk.supportsColor) {
170
187
  - `bgWhite`
171
188
 
172
189
 
190
+ ## 256-colors
191
+
192
+ Chalk does not support anything other than the base eight colors, which guarantees it will work on all terminals and systems. Some terminals, specifically `xterm` compliant ones, will support the full range of 8-bit colors. For this the lower level [ansi-256-colors](https://github.com/jbnicolai/ansi-256-colors) package can be used.
193
+
194
+
195
+ ## Windows
196
+
197
+ If you're on Windows, do yourself a favor and use [`cmder`](http://bliker.github.io/cmder/) instead of `cmd.exe`.
198
+
199
+
200
+ ## Related
201
+
202
+ - [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
203
+ - [ansi-styles](https://github.com/chalk/ansi-styles/) - ANSI escape codes for styling strings in the terminal
204
+ - [supports-color](https://github.com/chalk/supports-color/) - Detect whether a terminal supports color
205
+ - [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
206
+ - [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
207
+ - [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
208
+ - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
209
+
210
+
173
211
  ## License
174
212
 
175
213
  MIT © [Sindre Sorhus](http://sindresorhus.com)