chalk 0.3.0 → 1.0.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 (5) hide show
  1. package/index.js +100 -0
  2. package/license +21 -0
  3. package/package.json +25 -27
  4. package/readme.md +116 -53
  5. package/chalk.js +0 -64
package/index.js ADDED
@@ -0,0 +1,100 @@
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
+
9
+ function Chalk(options) {
10
+ // detect mode if not set manually
11
+ this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
12
+ }
13
+
14
+ // use bright blue on Windows as the normal blue color is illegible
15
+ if (process.platform === 'win32') {
16
+ ansiStyles.blue.open = '\u001b[94m';
17
+ }
18
+
19
+ function build(_styles) {
20
+ var builder = function builder() {
21
+ return applyStyle.apply(builder, arguments);
22
+ };
23
+ builder._styles = _styles;
24
+ builder.enabled = this.enabled;
25
+ // __proto__ is used because we must return a function, but there is
26
+ // no way to create a function with a different prototype.
27
+ builder.__proto__ = proto;
28
+ return builder;
29
+ }
30
+
31
+ var styles = (function () {
32
+ var ret = {};
33
+
34
+ Object.keys(ansiStyles).forEach(function (key) {
35
+ ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
36
+
37
+ ret[key] = {
38
+ get: function () {
39
+ return build.call(this, this._styles.concat(key));
40
+ }
41
+ };
42
+ });
43
+
44
+ return ret;
45
+ })();
46
+
47
+ var proto = defineProps(function chalk() {}, styles);
48
+
49
+ function applyStyle() {
50
+ // support varags, but simply cast to string in case there's only one arg
51
+ var args = arguments;
52
+ var argsLen = args.length;
53
+ var str = argsLen !== 0 && String(arguments[0]);
54
+ if (argsLen > 1) {
55
+ // don't slice `arguments`, it prevents v8 optimizations
56
+ for (var a = 1; a < argsLen; a++) {
57
+ str += ' ' + args[a];
58
+ }
59
+ }
60
+
61
+ if (!this.enabled || !str) {
62
+ return str;
63
+ }
64
+
65
+ /*jshint validthis: true */
66
+ var nestedStyles = this._styles;
67
+
68
+ var i = nestedStyles.length;
69
+ while (i--) {
70
+ var code = ansiStyles[nestedStyles[i]];
71
+ // Replace any instances already present with a re-opening code
72
+ // otherwise only the part of the string until said closing code
73
+ // will be colored, and the rest will simply be 'plain'.
74
+ str = code.open + str.replace(code.closeRe, code.open) + code.close;
75
+ }
76
+
77
+ return str;
78
+ }
79
+
80
+ function init() {
81
+ var ret = {};
82
+
83
+ Object.keys(styles).forEach(function (name) {
84
+ ret[name] = {
85
+ get: function () {
86
+ return build.call(this, [name]);
87
+ }
88
+ };
89
+ });
90
+
91
+ return ret;
92
+ }
93
+
94
+ defineProps(Chalk.prototype, init());
95
+
96
+ module.exports = new Chalk();
97
+ module.exports.styles = ansiStyles;
98
+ module.exports.hasColor = hasAnsi;
99
+ module.exports.stripColor = stripAnsi;
100
+ 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,7 +1,23 @@
1
1
  {
2
2
  "name": "chalk",
3
- "version": "0.3.0",
4
- "description": "Terminal string styling done right",
3
+ "version": "1.0.0",
4
+ "description": "Terminal string styling done right. Much color.",
5
+ "license": "MIT",
6
+ "repository": "sindresorhus/chalk",
7
+ "maintainers": [
8
+ "Sindre Sorhus <sindresorhus@gmail.com> (http://sindresorhus.com)",
9
+ "Joshua Appelman <jappelman@xebia.com> (http://jbnicolai.com)"
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": "^2.0.1",
44
+ "escape-string-regexp": "^1.0.2",
45
+ "has-ansi": "^1.0.3",
46
+ "strip-ansi": "^2.0.1",
47
+ "supports-color": "^1.3.0"
48
48
  },
49
49
  "devDependencies": {
50
- "mocha": "~1.12.0"
51
- },
52
- "engines": {
53
- "node": ">=0.8.0"
50
+ "matcha": "^0.6.0",
51
+ "mocha": "*"
54
52
  }
55
53
  }
package/readme.md CHANGED
@@ -1,52 +1,71 @@
1
- # chalk [![Build Status](https://secure.travis-ci.org/sindresorhus/chalk.png?branch=master)](http://travis-ci.org/sindresorhus/chalk)
1
+ <h1 align="center">
2
+ <br>
3
+ <img width="360" src="https://cdn.rawgit.com/sindresorhus/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk">
4
+ <br>
5
+ <br>
6
+ </h1>
2
7
 
3
- > Terminal string styling done right.
8
+ > Terminal string styling done right
4
9
 
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.
10
+ [![Build Status](https://travis-ci.org/sindresorhus/chalk.svg?branch=master)](https://travis-ci.org/sindresorhus/chalk) [![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg?style=flat)](https://www.youtube.com/watch?v=Sm368W0OsHo)
11
+
12
+ [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.
6
13
 
7
14
  **Chalk is a clean and focused alternative.**
8
15
 
9
- ![screenshot](screenshot.png)
16
+ ![screenshot](https://github.com/sindresorhus/ansi-styles/raw/master/screenshot.png)
10
17
 
11
18
 
12
19
  ## Why
13
20
 
14
- - **Doesn't extend String.prototype**
21
+ - Highly performant
22
+ - Doesn't extend `String.prototype`
15
23
  - Expressive API
24
+ - Ability to nest styles
16
25
  - Clean and focused
17
26
  - Auto-detects color support
18
27
  - Actively maintained
28
+ - [Used by ~3000 modules](https://www.npmjs.com/browse/depended/chalk)
19
29
 
20
30
 
21
31
  ## Install
22
32
 
23
- Install with [npm](https://npmjs.org/package/chalk): `npm install --save chalk`
33
+ ```
34
+ $ npm install --save chalk
35
+ ```
24
36
 
25
37
 
26
- ## Example
38
+ ## Usage
27
39
 
28
- Chalk comes with an easy to use composable API where you just chain the styles you want.
40
+ Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
29
41
 
30
42
  ```js
31
43
  var chalk = require('chalk');
32
44
 
33
45
  // style a string
34
- console.log(chalk.blue('Hello world!'));
46
+ chalk.blue('Hello world!');
35
47
 
36
48
  // combine styled and normal strings
37
- console.log(chalk.blue('Hello'), 'World' + chalk.red('!'));
49
+ chalk.blue('Hello') + 'World' + chalk.red('!');
38
50
 
39
51
  // compose multiple styles using the chainable API
40
- console.log(chalk.blue.bgRed.bold('Hello world!'));
52
+ chalk.blue.bgRed.bold('Hello world!');
53
+
54
+ // pass in multiple arguments
55
+ chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
41
56
 
42
57
  // nest styles
43
58
  chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
44
59
 
45
- // pass in multiple arguments
46
- console.log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'))
60
+ // nest styles of the same type even (color, underline, background)
61
+ chalk.green(
62
+ 'I am a green line ' +
63
+ chalk.blue.underline.bold('with a blue substring') +
64
+ ' that becomes green again!'
65
+ );
47
66
  ```
48
67
 
49
- You can easily define your own themes.
68
+ Easily define your own themes.
50
69
 
51
70
  ```js
52
71
  var chalk = require('chalk');
@@ -54,81 +73,125 @@ var error = chalk.bold.red;
54
73
  console.log(error('Error!'));
55
74
  ```
56
75
 
76
+ Take advantage of console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
77
+
78
+ ```js
79
+ var name = 'Sindre';
80
+ console.log(chalk.green('Hello %s'), name);
81
+ //=> Hello Sindre
82
+ ```
83
+
57
84
 
58
85
  ## API
59
86
 
60
- ### chalk.\<style>\[.\<style>...](string)
87
+ ### chalk.`<style>[.<style>...](string, [string...])`
61
88
 
62
- Chain [styles](#styles) and call the last one as a method with a string argument.
89
+ Example: `chalk.red.bold.underline('Hello', 'world');`
63
90
 
64
- Multiple arguments are also supported. Chalk will add a space between each one.
91
+ 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`.
92
+
93
+ Multiple arguments will be separated by space.
65
94
 
66
95
  ### chalk.enabled
67
96
 
68
- Color support is automatically detected, but you can override it.
97
+ 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.
69
98
 
70
- ### chalk.supportsColor
99
+ If you need to change this in a reusable module create a new instance:
71
100
 
72
- Detect whether the terminal [supports color](https://github.com/sindresorhus/has-color).
101
+ ```js
102
+ var ctx = new chalk.constructor({enabled: false});
103
+ ```
104
+
105
+ ### chalk.supportsColor
73
106
 
74
- Can be overridden by the user with the flags `--color` and `--no-color`.
107
+ Detect whether the terminal [supports color](https://github.com/sindresorhus/supports-color). Used internally and handled for you, but exposed for convenience.
75
108
 
76
- Used internally and handled for you, but exposed for convenience.
109
+ 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`.
77
110
 
78
111
  ### chalk.styles
79
112
 
80
113
  Exposes the styles as [ANSI escape codes](https://github.com/sindresorhus/ansi-styles).
81
114
 
115
+ Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own.
116
+
82
117
  ```js
83
118
  var chalk = require('chalk');
84
119
 
85
120
  console.log(chalk.styles.red);
86
- //=> ['\x1b[31m', '\x1b[39m']
121
+ //=> {open: '\u001b[31m', close: '\u001b[39m'}
87
122
 
88
- console.log(chalk.styles.red[0] + 'Hello' + chalk.styles.red[1]);
89
- // first item is the style escape code and second is the reset escape code
123
+ console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
90
124
  ```
91
125
 
126
+ ### chalk.hasColor(string)
127
+
128
+ Check whether a string [has color](https://github.com/sindresorhus/has-ansi).
129
+
92
130
  ### chalk.stripColor(string)
93
131
 
94
- Strip color from a string.
132
+ [Strip color](https://github.com/sindresorhus/strip-ansi) from a string.
95
133
 
134
+ Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
96
135
 
97
- ## Styles
136
+ Example:
98
137
 
99
- ### General
138
+ ```js
139
+ var chalk = require('chalk');
140
+ var styledString = getText();
100
141
 
101
- - reset
102
- - bold
103
- - italic
104
- - underline
105
- - inverse
106
- - strikethrough
142
+ if (!chalk.supportsColor) {
143
+ styledString = chalk.stripColor(styledString);
144
+ }
145
+ ```
107
146
 
108
- ### Text colors
109
147
 
110
- - black
111
- - red
112
- - green
113
- - yellow
114
- - blue
115
- - magenta
116
- - cyan
117
- - white
118
- - gray
148
+ ## Styles
149
+
150
+ ### Modifiers
151
+
152
+ - `reset`
153
+ - `bold`
154
+ - `dim`
155
+ - `italic` *(not widely supported)*
156
+ - `underline`
157
+ - `inverse`
158
+ - `hidden`
159
+ - `strikethrough` *(not widely supported)*
160
+
161
+ ### Colors
162
+
163
+ - `black`
164
+ - `red`
165
+ - `green`
166
+ - `yellow`
167
+ - `blue` *(on Windows the bright version is used as normal blue is illegible)*
168
+ - `magenta`
169
+ - `cyan`
170
+ - `white`
171
+ - `gray`
119
172
 
120
173
  ### Background colors
121
174
 
122
- - bgBlack
123
- - bgRed
124
- - bgGreen
125
- - bgYellow
126
- - bgBlue
127
- - bgMagenta
128
- - bgCyan
129
- - bgWhite
175
+ - `bgBlack`
176
+ - `bgRed`
177
+ - `bgGreen`
178
+ - `bgYellow`
179
+ - `bgBlue`
180
+ - `bgMagenta`
181
+ - `bgCyan`
182
+ - `bgWhite`
183
+
184
+
185
+ ## 256-colors
186
+
187
+ Chalk does not support 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.
188
+
189
+
190
+ ## Windows
191
+
192
+ If you're on Windows, do yourself a favor and use [`cmder`](http://bliker.github.io/cmder/) instead of `cmd.exe`.
130
193
 
131
194
 
132
195
  ## License
133
196
 
134
- MIT License • © [Sindre Sorhus](http://sindresorhus.com)
197
+ MIT © [Sindre Sorhus](http://sindresorhus.com)
package/chalk.js DELETED
@@ -1,64 +0,0 @@
1
- 'use strict';
2
- var ansi = require('ansi-styles');
3
- var defineProps = Object.defineProperties;
4
-
5
- ansi.grey = ansi.gray;
6
-
7
- var styles = (function () {
8
- var ret = {};
9
-
10
- Object.keys(ansi).forEach(function (key) {
11
- ret[key] = {
12
- get: function () {
13
- this._styles.push(key);
14
- return this;
15
- }
16
- };
17
- });
18
-
19
- return ret;
20
- })();
21
-
22
- var chalk = module.exports = defineProps({}, init());
23
-
24
- function init() {
25
- var ret = {};
26
-
27
- Object.keys(styles).forEach(function (name) {
28
- ret[name] = {
29
- get: function () {
30
- var obj = defineProps(function self() {
31
- var str = [].slice.call(arguments).join(' ');
32
-
33
- if (!chalk.enabled) {
34
- return str;
35
- }
36
-
37
- return self._styles.reduce(function (str, name) {
38
- var code = ansi[name];
39
- return code[0] + (str || '') + code[1];
40
- }, str);
41
- }, styles);
42
-
43
- obj._styles = [];
44
-
45
- return obj[name];
46
- }
47
- }
48
- });
49
-
50
- return ret;
51
- }
52
-
53
- chalk.styles = ansi;
54
-
55
- chalk.stripColor = function (str) {
56
- return typeof str === 'string' ? str.replace(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/g, '') : str;
57
- };
58
-
59
- chalk.supportsColor = require('has-color');
60
-
61
- // detect mode if not set manually
62
- if (chalk.enabled === undefined) {
63
- chalk.enabled = chalk.supportsColor;
64
- }