chalk 0.2.1 → 0.5.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.
- package/index.js +95 -0
- package/package.json +25 -27
- package/readme.md +89 -44
- package/chalk.js +0 -62
package/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
function build(_styles) {
|
|
11
|
+
var builder = function builder() {
|
|
12
|
+
return applyStyle.apply(builder, arguments);
|
|
13
|
+
};
|
|
14
|
+
builder._styles = _styles;
|
|
15
|
+
// __proto__ is used because we must return a function, but there is
|
|
16
|
+
// no way to create a function with a different prototype.
|
|
17
|
+
builder.__proto__ = proto;
|
|
18
|
+
return builder;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
var styles = (function () {
|
|
22
|
+
var ret = {};
|
|
23
|
+
|
|
24
|
+
ansiStyles.grey = ansiStyles.gray;
|
|
25
|
+
|
|
26
|
+
Object.keys(ansiStyles).forEach(function (key) {
|
|
27
|
+
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
|
28
|
+
|
|
29
|
+
ret[key] = {
|
|
30
|
+
get: function () {
|
|
31
|
+
return build(this._styles.concat(key));
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return ret;
|
|
37
|
+
})();
|
|
38
|
+
|
|
39
|
+
var proto = defineProps(function chalk() {}, styles);
|
|
40
|
+
|
|
41
|
+
function applyStyle() {
|
|
42
|
+
// support varags, but simply cast to string in case there's only one arg
|
|
43
|
+
var args = arguments;
|
|
44
|
+
var argsLen = args.length;
|
|
45
|
+
var str = argsLen !== 0 && String(arguments[0]);
|
|
46
|
+
if (argsLen > 1) {
|
|
47
|
+
// don't slice `arguments`, it prevents v8 optimizations
|
|
48
|
+
for (var a = 1; a < argsLen; a++) {
|
|
49
|
+
str += ' ' + args[a];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!chalk.enabled || !str) {
|
|
54
|
+
return str;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/*jshint validthis: true*/
|
|
58
|
+
var nestedStyles = this._styles;
|
|
59
|
+
|
|
60
|
+
for (var i = 0; i < nestedStyles.length; i++) {
|
|
61
|
+
var code = ansiStyles[nestedStyles[i]];
|
|
62
|
+
// Replace any instances already present with a re-opening code
|
|
63
|
+
// otherwise only the part of the string until said closing code
|
|
64
|
+
// will be colored, and the rest will simply be 'plain'.
|
|
65
|
+
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return str;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function init() {
|
|
72
|
+
var ret = {};
|
|
73
|
+
|
|
74
|
+
Object.keys(styles).forEach(function (name) {
|
|
75
|
+
ret[name] = {
|
|
76
|
+
get: function () {
|
|
77
|
+
return build([name]);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return ret;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
defineProps(chalk, init());
|
|
86
|
+
|
|
87
|
+
chalk.styles = ansiStyles;
|
|
88
|
+
chalk.hasColor = hasAnsi;
|
|
89
|
+
chalk.stripColor = stripAnsi;
|
|
90
|
+
chalk.supportsColor = supportsColor;
|
|
91
|
+
|
|
92
|
+
// detect mode if not set manually
|
|
93
|
+
if (chalk.enabled === undefined) {
|
|
94
|
+
chalk.enabled = chalk.supportsColor;
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chalk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Terminal string styling done right",
|
|
3
|
+
"version": "0.5.1",
|
|
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
|
-
"
|
|
47
|
-
"
|
|
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
|
-
"
|
|
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
|
-
#
|
|
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
|
-
[
|
|
5
|
+
[](https://travis-ci.org/sindresorhus/chalk)
|
|
6
|
+

|
|
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
|
-

|
|
12
|
+

|
|
10
13
|
|
|
11
14
|
|
|
12
15
|
## Why
|
|
13
16
|
|
|
14
|
-
-
|
|
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
|
-
|
|
29
|
+
```sh
|
|
30
|
+
$ npm install --save chalk
|
|
31
|
+
```
|
|
24
32
|
|
|
25
33
|
|
|
26
|
-
##
|
|
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')
|
|
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'
|
|
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
|
-
|
|
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
|
|
79
|
+
### chalk.`<style>[.<style>...](string, [string...])`
|
|
58
80
|
|
|
59
|
-
|
|
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/
|
|
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
|
-
//=>
|
|
109
|
+
//=> {open: '\u001b[31m', close: '\u001b[39m'}
|
|
83
110
|
|
|
84
|
-
console.log(chalk.styles.red
|
|
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
|
-
-
|
|
100
|
-
-
|
|
101
|
-
-
|
|
102
|
-
-
|
|
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
|
|
175
|
+
MIT © [Sindre Sorhus](http://sindresorhus.com)
|
package/chalk.js
DELETED
|
@@ -1,62 +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(str) {
|
|
31
|
-
if (!chalk.enabled) {
|
|
32
|
-
return str;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return self._styles.reduce(function (str, name) {
|
|
36
|
-
var code = ansi[name];
|
|
37
|
-
return code[0] + (str || '') + code[1];
|
|
38
|
-
}, str);
|
|
39
|
-
}, styles);
|
|
40
|
-
|
|
41
|
-
obj._styles = [];
|
|
42
|
-
|
|
43
|
-
return obj[name];
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
return ret;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
chalk.styles = ansi;
|
|
52
|
-
|
|
53
|
-
chalk.stripColor = function (str) {
|
|
54
|
-
return str.replace(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/g, '');
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
chalk.supportsColor = require('has-color');
|
|
58
|
-
|
|
59
|
-
// detect mode if not set manually
|
|
60
|
-
if (chalk.enabled === undefined) {
|
|
61
|
-
chalk.enabled = chalk.supportsColor;
|
|
62
|
-
}
|