Skip to content

Commit 9a32c09

Browse files
committed
Add spinner for long jobs.
Signed-off-by: Eric Wang <[email protected]>
1 parent 332d68e commit 9a32c09

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

lib/commands/plugin.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ function install(src) {
7070
fs.unlinkSync(dst);
7171
});
7272

73+
var spin = h.spin('Downloading ' + src);
7374
srcstream.pipe(dststream);
7475
dststream.on('close', function() {
76+
spin.stop();
7577
log.debug('copied to ' + dst);
7678

7779
// install dependencies for current platform
@@ -83,15 +85,17 @@ function install(src) {
8385
else
8486
return parts[0];
8587
}).join(' ').trim();
86-
87-
if (deps.length > 0) {
88-
var cmd = 'npm install --save ' + deps;
89-
log.debug(cmd);
90-
require('child_process').execSync(cmd, {
91-
cwd: path.resolve(__dirname, '../..')
92-
});
93-
}
94-
plugin.help();
88+
if (deps.length === 0) return plugin.help();
89+
90+
var cmd = 'npm install --save ' + deps;
91+
spin = h.spin(cmd);
92+
log.debug(cmd);
93+
require('child_process').exec(cmd, {
94+
cwd: path.resolve(__dirname, '../..')
95+
}, function() {
96+
spin.stop();
97+
plugin.help();
98+
});
9599
});
96100
}
97101

lib/commands/user.js

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ cmd.handler = function(argv) {
4040

4141
core.login(user, function(e, user) {
4242
if (e) return log.fail(e);
43-
4443
log.info('Successfully login as', chalk.yellow(user.name));
4544
});
4645
});

lib/helper.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var path = require('path');
33

44
var _ = require('underscore');
55
var mkdirp = require('mkdirp');
6+
var ora = require('ora');
67

78
var UNITS_SIZE = [
89
{unit: 'B', name: 'Bytes', count: 1024},
@@ -243,4 +244,8 @@ h.printSafeHTTP = function(msg) {
243244
.replace(/('set-cookie'\s*:\s*)\[.*?\]/, '$1<hidden>');
244245
};
245246

247+
h.spin = function(s) {
248+
return ora(require('./chalk').gray(s)).start();
249+
};
250+
246251
module.exports = h;

lib/plugins/leetcode.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var session = require('../session');
1515
var plugin = new Plugin(10, 'leetcode', '',
1616
'Plugin to talk with leetcode APIs.');
1717

18+
var spin;
19+
1820
// update options with user credentials
1921
function signOpts(opts, user) {
2022
opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId +
@@ -49,7 +51,6 @@ function checkError(e, resp, expectedStatus) {
4951

5052
plugin.getProblems = function(cb) {
5153
log.debug('running leetcode.getProblems');
52-
5354
var problems = [];
5455
var doTask = function(category, taskDone) {
5556
plugin.getCategoryProblems(category, function(e, _problems) {
@@ -63,7 +64,9 @@ plugin.getProblems = function(cb) {
6364
});
6465
};
6566

67+
spin = h.spin('Downloading problems');
6668
queue.run(config.sys.categories, doTask, function(e) {
69+
spin.stop();
6770
return cb(e, problems);
6871
});
6972
};
@@ -72,6 +75,7 @@ plugin.getCategoryProblems = function(category, cb) {
7275
log.debug('running leetcode.getCategoryProblems: ' + category);
7376
var opts = makeOpts(config.sys.urls.problems.replace('$category', category));
7477

78+
spin.text = 'Downloading category ' + category;
7579
request(opts, function(e, resp, body) {
7680
e = checkError(e, resp, 200);
7781
if (e) return cb(e);
@@ -140,7 +144,9 @@ plugin.getProblem = function(problem, cb) {
140144
operationName: 'getQuestionDetail'
141145
};
142146

147+
spin = h.spin('Downloading ' + problem.slug);
143148
request.post(opts, function(e, resp, body) {
149+
spin.stop();
144150
e = checkError(e, resp, 200);
145151
if (e) return cb(e);
146152

@@ -175,7 +181,9 @@ function runCode(opts, problem, cb) {
175181
typed_code: h.getFileData(problem.file)
176182
});
177183

184+
spin = h.spin('Sending code to judge');
178185
request(opts, function(e, resp, body) {
186+
spin.stop();
179187
e = checkError(e, resp, 200);
180188
if (e) return cb(e);
181189

@@ -207,7 +215,9 @@ function verifyResult(opts, jobs, results, cb) {
207215
opts.method = 'GET';
208216
opts.url = config.sys.urls.verify.replace('$id', jobs[0].id);
209217

218+
spin = h.spin('Waiting for judge result');
210219
request(opts, function(e, resp, body) {
220+
spin.stop();
211221
e = checkError(e, resp, 200);
212222
if (e) return cb(e);
213223

@@ -366,7 +376,9 @@ plugin.getFavorites = function(cb) {
366376

367377
plugin.signin = function(user, cb) {
368378
log.debug('running leetcode.signin');
379+
spin = h.spin('Signing in leetcode.com');
369380
request(config.sys.urls.login, function(e, resp, body) {
381+
spin.stop();
370382
e = checkError(e, resp, 200);
371383
if (e) return cb(e);
372384

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"he": "1.1.1",
3636
"mkdirp": "0.5.1",
3737
"nconf": "0.10.0",
38+
"ora": "^1.3.0",
3839
"prompt": "1.0.0",
3940
"request": "2.83.0",
4041
"sprintf-js": "1.1.1",

0 commit comments

Comments
 (0)