1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var util = require('util');
|
10 | var tty = require('tty');
|
11 | var os = require('os');
|
12 |
|
13 | exports.disabled = false;
|
14 | exports.isatty = false;
|
15 |
|
16 | function isColorSupported() {
|
17 | if (exports.disabled) return false;
|
18 |
|
19 |
|
20 | if (!exports.isatty && !tty.isatty()) return false;
|
21 |
|
22 | if ('COLORTERM' in process.env) return true;
|
23 |
|
24 | if (os.type() === 'Windows_NT') return true;
|
25 |
|
26 | var term = process.env.TERM;
|
27 | if (!term) return false;
|
28 |
|
29 | term = term.toLowerCase();
|
30 | if (term.indexOf('color') !== -1) return true;
|
31 | return term === 'xterm' || term === 'linux';
|
32 | }
|
33 |
|
34 |
|
35 | function is256ColorSupported() {
|
36 | if (!isColorSupported()) return false;
|
37 |
|
38 | var term = process.env.TERM;
|
39 | term = term.toLowerCase();
|
40 | return term.indexOf('256') !== -1;
|
41 | }
|
42 | exports.isColorSupported = isColorSupported;
|
43 | exports.is256ColorSupported = is256ColorSupported;
|
44 |
|
45 | var colors = [
|
46 | 'black', 'red', 'green', 'yellow', 'blue',
|
47 | 'magenta', 'cyan', 'white'
|
48 | ];
|
49 |
|
50 | var styles = [
|
51 | 'bold', 'faint', 'italic', 'underline', 'blink', 'overline',
|
52 | 'inverse', 'conceal', 'strike'
|
53 | ];
|
54 |
|
55 | exports.color = {};
|
56 |
|
57 | function Color(obj) {
|
58 | this.string = obj;
|
59 |
|
60 | this.styles = [];
|
61 | this.fgcolor = null;
|
62 | this.bgcolor = null;
|
63 | }
|
64 | util.inherits(Color, String);
|
65 |
|
66 | for (var i = 0; i < colors.length; i++) {
|
67 | (function(i) {
|
68 | var name = colors[i];
|
69 | Object.defineProperty(Color.prototype, name, {
|
70 | get: function() {
|
71 | this.fgcolor = i;
|
72 | return this;
|
73 | }
|
74 | });
|
75 | Object.defineProperty(Color.prototype, name + '_bg', {
|
76 | get: function() {
|
77 | this.bgcolor = i;
|
78 | return this;
|
79 | }
|
80 | });
|
81 | exports.color[name] = exports[name] = function(text) {
|
82 | if (!isColorSupported()) return text;
|
83 | return '\x1b[' + (30 + i) + 'm' + text + '\x1b[0m';
|
84 | };
|
85 | exports.color[name + '_bg'] = exports[name + '_bg'] = function(text) {
|
86 | if (!isColorSupported()) return text;
|
87 | return '\x1b[' + (40 + i) + 'm' + text + '\x1b[0m';
|
88 | };
|
89 | })(i);
|
90 | }
|
91 | for (var i = 0; i < styles.length; i++) {
|
92 | (function(i) {
|
93 | var name = styles[i];
|
94 | Object.defineProperty(Color.prototype, name, {
|
95 | get: function() {
|
96 | if (this.styles.indexOf(i) === -1) {
|
97 | this.styles = this.styles.concat(i + 1);
|
98 | }
|
99 | return this;
|
100 | }
|
101 | });
|
102 | exports.color[name] = exports[name] = function(text) {
|
103 | if (!isColorSupported()) return text;
|
104 | return '\x1b[' + (i + 1) + 'm' + text + '\x1b[0m';
|
105 | };
|
106 | })(i);
|
107 | }
|
108 |
|
109 | exports.color.grey = exports.color.gray = exports.grey = exports.gray = function(text) {
|
110 | if (!isColorSupported()) return text;
|
111 | if (is256ColorSupported()) {
|
112 | return '\x1b[38;5;8m' + text + '\x1b[0m';
|
113 | }
|
114 | return '\x1b[30;1m' + text + '\x1b[0m';
|
115 | };
|
116 | Object.defineProperty(Color.prototype, 'gray', {
|
117 | get: function() {
|
118 | if (isColorSupported) {
|
119 | this.fgcolor = 8;
|
120 | } else {
|
121 | this.styles = this.styles.concat(1);
|
122 | this.fgcolor = 0;
|
123 | }
|
124 | return this;
|
125 | }
|
126 | });
|
127 | Object.defineProperty(Color.prototype, 'grey', {
|
128 | get: function() {
|
129 | if (isColorSupported) {
|
130 | this.fgcolor = 8;
|
131 | } else {
|
132 | this.styles = this.styles.concat(1);
|
133 | this.fgcolor = 0;
|
134 | }
|
135 | return this;
|
136 | }
|
137 | });
|
138 |
|
139 |
|
140 | Color.prototype.valueOf = function() {
|
141 | if (!isColorSupported()) return this.string;
|
142 | var is256 = is256ColorSupported();
|
143 |
|
144 | var text = this.string;
|
145 | var reset = '\x1b[0m';
|
146 |
|
147 | if (is256) {
|
148 | if (this.fgcolor !== null) {
|
149 | text = '\x1b[38;5;' + this.fgcolor + 'm' + text + reset;
|
150 | }
|
151 | if (this.bgcolor !== null) {
|
152 | text = '\x1b[48;5;' + this.bgcolor + 'm' + text + reset;
|
153 | }
|
154 | } else {
|
155 | if (this.fgcolor !== null && this.fgcolor < 8) {
|
156 | text = '\x1b[' + (30 + this.fgcolor) + 'm' + text + reset;
|
157 | }
|
158 | if (this.bgcolor !== null && this.bgcolor < 8) {
|
159 | text = '\x1b[' + (40 + this.bgcolor) + 'm' + text + reset;
|
160 | }
|
161 | }
|
162 | if (this.styles.length) {
|
163 | text = '\x1b[' + this.styles.join(';') + 'm' + text + reset;
|
164 | }
|
165 | return text;
|
166 | };
|
167 | Color.prototype.toString = function() {
|
168 | return this.valueOf();
|
169 | };
|
170 | Object.defineProperty(Color.prototype, 'color', {
|
171 | get: function() {
|
172 | return this.valueOf();
|
173 | }
|
174 | });
|
175 | Object.defineProperty(Color.prototype, 'style', {
|
176 | get: function() {
|
177 | return this.valueOf();
|
178 | }
|
179 | });
|
180 | Object.defineProperty(Color.prototype, 'length', {
|
181 | get: function() {
|
182 | return this.string.length;
|
183 | }
|
184 | });
|
185 |
|
186 | exports.Color = Color;
|