Skip to content

Commit 2db2814

Browse files
committedAug 21, 2016
Implement colorful output.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 7ab29b3 commit 2db2814

File tree

6 files changed

+19
-9
lines changed

6 files changed

+19
-9
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Navigate all the problems. The heading `✔` means you have AC-ed the problem.
5858
* `-l` to filter by level.
5959
* `-s` to show statistic counters.
6060
* `lc list <keyword>` to search by keyword.
61+
* `--no-color` to disable colorful output.
6162

6263
### 3. Prepare
6364

‎lib/commands/show.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('underscore');
2+
var chalk = require('chalk');
23
var fs = require('fs');
34

45
var sprintf = require('sprintf-js').sprintf;
@@ -43,11 +44,11 @@ cmd.handler = function(argv) {
4344

4445
var f = problem.key + h.langToExt(argv.lang);
4546
fs.writeFileSync(f, template.defaultCode);
46-
msg = sprintf('(File: %s)', f);
47+
msg = sprintf('(File: %s)', chalk.yellow.underline(f));
4748
}
4849

4950
console.log(sprintf('[%d] %s\t%s\n', problem.id, problem.name, msg));
50-
console.log(sprintf('%s\n', problem.link));
51+
console.log(sprintf('%s\n', chalk.underline(problem.link)));
5152
console.log(sprintf('* %s (%s)', problem.level, problem.percent));
5253
console.log(sprintf('* Total Accepted: %d', problem.totalAC));
5354
console.log(sprintf('* Total Submissions: %d\n', problem.totalSubmit));

‎lib/commands/test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('underscore');
2+
var chalk = require('chalk');
23
var util = require('util');
34

45
var core = require('../core');
@@ -31,7 +32,8 @@ function prettyLine(actual, expected, key) {
3132
!_.isEqual(actual[key], expected[key])) {
3233
sym = '✘';
3334
}
34-
return util.format(' %s %s: %s', sym, key.split('_').pop(), actual[key]);
35+
var line = util.format(' %s %s: %s', sym, key.split('_').pop(), actual[key]);
36+
return (sym === '✔') ? chalk.green(line) : chalk.red(line);
3537
}
3638

3739
cmd.handler = function(argv) {
@@ -40,6 +42,9 @@ cmd.handler = function(argv) {
4042
testcase = h.readStdin();
4143
}
4244

45+
if (!testcase || testcase === '')
46+
return console.log('ERROR: missing testcase?');
47+
4348
var keyword = h.getFilename(argv.filename);
4449
core.getProblem(keyword, function(e, problem) {
4550
if (e) return console.log('ERROR:', e);
@@ -55,7 +60,7 @@ cmd.handler = function(argv) {
5560

5661
for (var i = 0; i < results.length; ++i) {
5762
console.log();
58-
console.log(results[i].name);
63+
console.log(chalk.yellow(results[i].name));
5964
console.log(prettyLine(results[i], results[i + 1], 'status_runtime'));
6065
console.log(prettyLine(results[i], results[i + 1], 'code_answer'));
6166
console.log(prettyLine(results[i], results[i + 1], 'code_output'));

‎lib/commands/user.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var chalk = require('chalk');
12
var prompt = require('prompt');
23

34
var core = require('../core');
@@ -33,21 +34,21 @@ cmd.handler = function(argv) {
3334
core.login(user, function(e, user) {
3435
if (e) return console.log('Login failed:', e);
3536

36-
console.log('Successfully login as', user.name);
37+
console.log('Successfully login as', chalk.yellow(user.name));
3738
});
3839
});
3940
} else if (argv.logout) {
4041
// logout
4142
user = core.logout(null);
4243
if (user)
43-
console.log('Successfully logout as', user.name);
44+
console.log('Successfully logout as', chalk.yellow(user.name));
4445
else
4546
console.log('You are not login yet?');
4647
} else {
4748
// show current user
4849
user = core.getUser();
4950
if (user)
50-
console.log('You are now login as', user.name);
51+
console.log('You are now login as', chalk.yellow(user.name));
5152
else
5253
console.log('You are not login yet?');
5354
}

‎lib/helper.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
var chalk = require('chalk');
12
var fs = require('fs');
23
var path = require('path');
34

45
var h = {};
56

67
h.prettyState = function(state) {
78
switch (state) {
8-
case 'ac': return '✔';
9-
case 'notac': return '✘';
9+
case 'ac': return chalk.green('✔');
10+
case 'notac': return chalk.red('✘');
1011
default: return ' ';
1112
}
1213
};

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"homepage": "https://github.com/skygragon/leetcode-cli#readme",
3232
"dependencies": {
33+
"chalk": "^1.1.3",
3334
"cheerio": "^0.20.0",
3435
"prompt": "^1.0.0",
3536
"request": "^2.74.0",

0 commit comments

Comments
 (0)