forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathsession.js
109 lines (95 loc) · 3.09 KB
/
session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var prompt = require('prompt');
var h = require('../helper');
var chalk = require('../chalk');
var log = require('../log');
var core = require('../core');
var session = require('../session');
var sprintf = require('../sprintf');
const cmd = {
command: 'session [keyword]',
aliases: ['branch'],
desc: 'Manage sessions',
builder: function(yargs) {
return yargs
.option('c', {
alias: 'create',
type: 'boolean',
describe: 'Create session',
default: false
})
.option('d', {
alias: 'delete',
type: 'boolean',
describe: 'Delete session',
default: false
})
.option('e', {
alias: 'enable',
type: 'boolean',
describe: 'Enable/activate session',
default: false
})
.positional('keyword', {
type: 'string',
describe: 'Session name or id',
default: ''
})
.example(chalk.yellow('leetcode session'), 'Show all cache')
.example(chalk.yellow('leetcode session xxx'), 'Show session by keyword')
.example('', '')
.example(chalk.yellow('leetcode session -c xxx'), 'Create session with name')
.example(chalk.yellow('leetcode session -e xxx'), 'Enable session by keyword')
.example(chalk.yellow('leetcode session -d xxx'), 'Delete session by keyword');
}
};
function printSessions(e, sessions) {
if (e) return log.fail(e);
log.info(chalk.gray(sprintf(' %6s %5s %18s %28s %16s',
'Active', 'Id', 'Name', 'AC Questions', 'AC Submits')));
log.info(chalk.gray('-'.repeat(80)));
for (let s of sessions) {
let questionRate = 0;
let submissionRate = 0;
if (s.submitted_questions > 0)
questionRate = s.ac_questions * 100 / s.submitted_questions;
if (s.total_submitted > 0)
submissionRate = s.total_acs * 100 / s.total_submitted;
log.printf(' %s %8s %-26s %6s (%6s %%) %6s (%6s %%)',
s.is_active ? h.prettyState('ac') : ' ',
s.id,
s.name || 'Anonymous Session',
chalk.green(s.ac_questions),
questionRate.toFixed(2),
chalk.green(s.total_acs),
submissionRate.toFixed(2));
}
}
cmd.handler = function(argv) {
session.argv = argv;
if (argv.create)
return core.createSession(argv.keyword, printSessions);
core.getSessions(function(e, sessions) {
if (e) return log.fail(e);
if (argv.keyword) {
const id = Number(argv.keyword);
sessions = sessions.filter(x => x.name === argv.keyword || x.id === id);
if (sessions.length > 1) return log.fail('Ambiguous sessions?');
const session = sessions[0];
if (!session) return log.fail('Session not found!');
if (argv.enable && !session.is_active) {
core.activateSession(session, function(e, sessions) {
if (e) return log.fail(e);
require('../session').deleteCodingSession();
printSessions(e, sessions);
});
return;
}
if (argv.delete) {
return core.deleteSession(session, printSessions);
}
}
printSessions(null, sessions);
});
};
module.exports = cmd;