UNPKG

4.5 kBJavaScriptView Raw
1
2/* ANSI color support in terminal
3 * @author: Hsiaoming Yang <lepture@me.com>
4 *
5 * http://en.wikipedia.org/wiki/ANSI_escape_code
6 */
7
8
9var util = require('util');
10var tty = require('tty');
11var os = require('os');
12
13exports.disabled = false;
14exports.isatty = false;
15
16function isColorSupported() {
17 if (exports.disabled) return false;
18
19 // you can force to tty
20 if (!exports.isatty && !tty.isatty()) return false;
21
22 if ('COLORTERM' in process.env) return true;
23 // windows will support color
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
35function is256ColorSupported() {
36 if (!isColorSupported()) return false;
37
38 var term = process.env.TERM;
39 term = term.toLowerCase();
40 return term.indexOf('256') !== -1;
41}
42exports.isColorSupported = isColorSupported;
43exports.is256ColorSupported = is256ColorSupported;
44
45var colors = [
46 'black', 'red', 'green', 'yellow', 'blue',
47 'magenta', 'cyan', 'white'
48];
49
50var styles = [
51 'bold', 'faint', 'italic', 'underline', 'blink', 'overline',
52 'inverse', 'conceal', 'strike'
53];
54
55exports.color = {};
56
57function Color(obj) {
58 this.string = obj;
59
60 this.styles = [];
61 this.fgcolor = null;
62 this.bgcolor = null;
63}
64util.inherits(Color, String);
65
66for (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}
91for (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
109exports.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};
116Object.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});
127Object.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
140Color.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};
167Color.prototype.toString = function() {
168 return this.valueOf();
169};
170Object.defineProperty(Color.prototype, 'color', {
171 get: function() {
172 return this.valueOf();
173 }
174});
175Object.defineProperty(Color.prototype, 'style', {
176 get: function() {
177 return this.valueOf();
178 }
179});
180Object.defineProperty(Color.prototype, 'length', {
181 get: function() {
182 return this.string.length;
183 }
184});
185
186exports.Color = Color;