1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var grunt = require('../grunt');
|
11 |
|
12 |
|
13 | var util = require('util');
|
14 |
|
15 |
|
16 | var log = module.exports = {};
|
17 |
|
18 |
|
19 | var suppressOutput;
|
20 |
|
21 |
|
22 | log.muted = false;
|
23 |
|
24 |
|
25 | var hasLogged;
|
26 |
|
27 |
|
28 | function markup(str) {
|
29 | str = str || '';
|
30 |
|
31 | str = str.replace(/(\s|^)_([\s\S]+?)_(?=\s|$)/g, '$1' + '$2'.underline);
|
32 |
|
33 | str = str.replace(/(\s|^)\*([\s\S]+?)\*(?=\s|$)/g, '$1' + '$2'.bold);
|
34 | return str;
|
35 | }
|
36 |
|
37 |
|
38 | log.write = function(msg) {
|
39 |
|
40 | if (!log.muted && !suppressOutput) {
|
41 | hasLogged = true;
|
42 | process.stdout.write(markup(msg));
|
43 | }
|
44 |
|
45 | return this;
|
46 | };
|
47 |
|
48 |
|
49 | log.writeln = function(msg) {
|
50 |
|
51 | this.write((msg || '') + '\n');
|
52 |
|
53 | return this;
|
54 | };
|
55 |
|
56 |
|
57 |
|
58 | log.error = function(msg) {
|
59 | if (msg) {
|
60 | grunt.fail.errorcount++;
|
61 | return this.writeln('>> '.red + grunt.utils._.trim(msg).replace(/\n/g, '\n>> '.red));
|
62 | } else {
|
63 | return this.writeln('ERROR'.red);
|
64 | }
|
65 | };
|
66 | log.ok = function(msg) {
|
67 | if (msg) {
|
68 | return this.writeln('>> '.green + grunt.utils._.trim(msg).replace(/\n/g, '\n>> '.green));
|
69 | } else {
|
70 | return this.writeln('OK'.green);
|
71 | }
|
72 | };
|
73 | log.errorlns = function(msg) { return this.error(log.wraptext(77, msg)); };
|
74 | log.oklns = function(msg) { return this.ok(log.wraptext(77, msg)); };
|
75 | log.success = function(msg) { return this.writeln(msg.green); };
|
76 | log.fail = function(msg) { return this.writeln(msg.red); };
|
77 | log.header = function(msg) {
|
78 |
|
79 | if (hasLogged) { this.writeln(); }
|
80 | return this.writeln(msg.underline);
|
81 | };
|
82 | log.subhead = function(msg) {
|
83 |
|
84 | if (hasLogged) { this.writeln(); }
|
85 | return this.writeln(msg.bold);
|
86 | };
|
87 |
|
88 | log.debug = function() {
|
89 | if (grunt.option('debug')) {
|
90 | this.writeln(('[D] ' + util.format.apply(this, arguments)).magenta);
|
91 | }
|
92 | return this;
|
93 | };
|
94 |
|
95 |
|
96 | log.writetableln = function(widths, texts) {
|
97 | return this.writeln(this.table(widths, texts));
|
98 | };
|
99 |
|
100 |
|
101 | log.writelns = function(msg) {
|
102 | return this.writeln(this.wraptext(80, msg));
|
103 | };
|
104 |
|
105 |
|
106 | log.writeflags = function(obj, prefix) {
|
107 | var wordlist;
|
108 | if (Array.isArray(obj)) {
|
109 | wordlist = log.wordlist(obj);
|
110 | } else if (typeof obj === 'object' && obj) {
|
111 | wordlist = log.wordlist(Object.keys(obj).map(function(key) {
|
112 | var val = obj[key];
|
113 | return key + (val === true ? '' : '=' + JSON.stringify(val));
|
114 | }));
|
115 | }
|
116 | return this.writeln((prefix || 'Flags') + ': ' + (wordlist || '(none)'.cyan));
|
117 | };
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | log.verbose = {};
|
123 | log.notverbose = {};
|
124 |
|
125 |
|
126 | Object.keys(log).filter(function(key) {
|
127 | return typeof log[key] === 'function';
|
128 | }).forEach(function(key) {
|
129 |
|
130 |
|
131 | log.verbose[key] = function() {
|
132 | suppressOutput = !grunt.option('verbose');
|
133 | log[key].apply(this, arguments);
|
134 | suppressOutput = false;
|
135 | return this;
|
136 | };
|
137 |
|
138 |
|
139 | log.notverbose[key] = function() {
|
140 | suppressOutput = grunt.option('verbose');
|
141 | log[key].apply(this, arguments);
|
142 | suppressOutput = false;
|
143 | return this;
|
144 | };
|
145 | });
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | log.verbose.or = log.notverbose;
|
151 | log.notverbose.or = log.verbose;
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | log.wordlist = function(arr, separator) {
|
157 | return arr.map(function(item) {
|
158 | return item.cyan;
|
159 | }).join(separator || ', ');
|
160 | };
|
161 |
|
162 |
|
163 | log.uncolor = function(str) {
|
164 | return str.replace(/\x1B\[\d+m/g, '');
|
165 | };
|
166 |
|
167 |
|
168 | log.wraptext = function(width, text) {
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | var result = [];
|
179 | var matches, color, tmp;
|
180 | var captured = [];
|
181 | var charlen = 0;
|
182 |
|
183 | while (matches = text.match(/(?:(\x1B\[\d+m)|\n|(.))([\s\S]*)/)) {
|
184 |
|
185 | text = matches[3];
|
186 |
|
187 |
|
188 | if (matches[1]) {
|
189 |
|
190 | color = matches[1];
|
191 |
|
192 | captured.push(matches[1]);
|
193 | continue;
|
194 |
|
195 |
|
196 | } else if (matches[2]) {
|
197 |
|
198 |
|
199 | if (charlen === 0 && color) { captured.push(color); }
|
200 |
|
201 | captured.push(matches[2]);
|
202 |
|
203 | charlen++;
|
204 |
|
205 | if (charlen <= width || matches[2] === ' ') { continue; }
|
206 |
|
207 |
|
208 | tmp = captured.lastIndexOf(' ');
|
209 | text = captured.slice(tmp === -1 ? tmp : tmp + 1).join('') + text;
|
210 | captured = captured.slice(0, tmp);
|
211 | }
|
212 |
|
213 |
|
214 | result.push(captured.join(''));
|
215 |
|
216 |
|
217 | captured = [];
|
218 | charlen = 0;
|
219 | }
|
220 |
|
221 | result.push(captured.join(''));
|
222 | return result.join('\n');
|
223 | };
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | log.table = function(widths, texts) {
|
249 | var rows = [];
|
250 | widths.forEach(function(width, i) {
|
251 | var lines = log.wraptext(width, texts[i]).split('\n');
|
252 | lines.forEach(function(line, j) {
|
253 | var row = rows[j];
|
254 | if (!row) { row = rows[j] = []; }
|
255 | row[i] = line;
|
256 | });
|
257 | });
|
258 |
|
259 | var lines = [];
|
260 | rows.forEach(function(row) {
|
261 | var txt = '';
|
262 | var column;
|
263 | for (var i = 0; i < row.length; i++) {
|
264 | column = row[i] || '';
|
265 | txt += column;
|
266 | var diff = widths[i] - log.uncolor(column).length;
|
267 | if (diff > 0) { txt += grunt.utils.repeat(diff, ' '); }
|
268 | }
|
269 | lines.push(txt);
|
270 | });
|
271 |
|
272 | return lines.join('\n');
|
273 | };
|