1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var util = require('util');
|
10 | var terminal = require('./terminal');
|
11 |
|
12 | var colors = [
|
13 | 'black', 'red', 'green', 'yellow', 'blue',
|
14 | 'magenta', 'cyan', 'white'
|
15 | ];
|
16 |
|
17 | var styles = [
|
18 | 'bold', 'faint', 'italic', 'underline', 'blink', 'overline',
|
19 | 'inverse', 'conceal', 'strike'
|
20 | ];
|
21 |
|
22 | exports.color = {};
|
23 |
|
24 | function Color(obj) {
|
25 | this.string = obj;
|
26 |
|
27 | this.styles = [];
|
28 | this.fgcolor = null;
|
29 | this.bgcolor = null;
|
30 | }
|
31 | util.inherits(Color, String);
|
32 |
|
33 | for (var i = 0; i < colors.length; i++) {
|
34 | (function(i) {
|
35 | var name = colors[i];
|
36 | Object.defineProperty(Color.prototype, name, {
|
37 | get: function() {
|
38 | this.fgcolor = i;
|
39 | return this;
|
40 | }
|
41 | });
|
42 | Object.defineProperty(Color.prototype, name + '_bg', {
|
43 | get: function() {
|
44 | this.bgcolor = i;
|
45 | return this;
|
46 | }
|
47 | });
|
48 | exports.color[name] = exports[name] = function(text) {
|
49 | if (!terminal.isColorSupported()) return text;
|
50 | return '\x1b[' + (30 + i) + 'm' + text + '\x1b[0m';
|
51 | };
|
52 | exports.color[name + '_bg'] = exports[name + '_bg'] = function(text) {
|
53 | if (!terminal.isColorSupported()) return text;
|
54 | return '\x1b[' + (40 + i) + 'm' + text + '\x1b[0m';
|
55 | };
|
56 | })(i);
|
57 | }
|
58 | for (var i = 0; i < styles.length; i++) {
|
59 | (function(i) {
|
60 | var name = styles[i];
|
61 | Object.defineProperty(Color.prototype, name, {
|
62 | get: function() {
|
63 | if (this.styles.indexOf(i) === -1) {
|
64 | this.styles = this.styles.concat(i + 1);
|
65 | }
|
66 | return this;
|
67 | }
|
68 | });
|
69 | exports.color[name] = exports[name] = function(text) {
|
70 | if (!terminal.isColorSupported()) return text;
|
71 | return '\x1b[' + (i + 1) + 'm' + text + '\x1b[0m';
|
72 | };
|
73 | })(i);
|
74 | }
|
75 |
|
76 | Color.prototype.valueOf = function() {
|
77 | if (!terminal.isColorSupported()) return this.string;
|
78 | var is256 = terminal.is256ColorSupported();
|
79 |
|
80 | var text = this.string;
|
81 | var reset = '\x1b[0m';
|
82 |
|
83 | if (is256) {
|
84 | if (this.fgcolor !== null) {
|
85 | text = '\x1b[38;5;' + this.fgcolor + 'm' + text + reset;
|
86 | }
|
87 | if (this.bgcolor !== null) {
|
88 | text = '\x1b[48;5;' + this.bgcolor + 'm' + text + reset;
|
89 | }
|
90 | } else {
|
91 | if (this.fgcolor !== null && this.fgcolor < 8) {
|
92 | text = '\x1b[' + (30 + this.fgcolor) + 'm' + text + reset;
|
93 | }
|
94 | if (this.bgcolor !== null && this.bgcolor < 8) {
|
95 | text = '\x1b[' + (40 + this.bgcolor) + 'm' + text + reset;
|
96 | }
|
97 | }
|
98 | if (this.styles.length) {
|
99 | text = '\x1b[' + this.styles.join(';') + 'm' + text + reset;
|
100 | }
|
101 | return text;
|
102 | };
|
103 | Color.prototype.toString = function() {
|
104 | return this.valueOf();
|
105 | };
|
106 | Object.defineProperty(Color.prototype, 'color', {
|
107 | get: function() {
|
108 | return this.valueOf();
|
109 | }
|
110 | });
|
111 | Object.defineProperty(Color.prototype, 'style', {
|
112 | get: function() {
|
113 | return this.valueOf();
|
114 | }
|
115 | });
|
116 | Object.defineProperty(Color.prototype, 'length', {
|
117 | get: function() {
|
118 | return this.string.length;
|
119 | }
|
120 | });
|
121 |
|
122 | exports.Color = Color;
|