Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: skygragon/leetcode-cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: leetcode-tools/leetcode-cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.github/
docs/
test/
.eslintrc.js
.gitignore
.travis.yml
Dockerfile
.dockerignore
.nyc_output/
.vscode/
coverage/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ node_js:
os:
- linux
- osx
- windows

install:
- npm install
30 changes: 30 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/bin/leetcode",
"args": ["show", "1", "--solution"]
},
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/plugins"
],
"internalConsoleOptions": "openOnSessionStart"
},
]
}
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@

# leetcode-cli

> Note: This repository is forked from [leetcode-cli](https://github.com/skygragon/leetcode-cli) for temporary usage.
> Note: Copy cookie from webbrowser and Using **leetcode user -c** can temporary fix can't [login problem](https://github.com/jdneo/vscode-leetcode/issues/478).
<img src="https://github.com/skygragon/leetcode-cli/raw/master/docs/logo.png" align="right">

A productive cli tool to enjoy leetcode!
@@ -35,10 +38,13 @@ Great thanks to leetcode.com, a really awesome website!

## Quick Start

Read help first $ leetcode help
Login with your leetcode account $ leetcode user -l
Browse all questions $ leetcode list
Choose one question $ leetcode show 1 -g -l cpp
Read help first $ leetcode help
Login with your leetcode account $ leetcode user -l
Login with third party account--GitHub $ leetcode user -g
Login with third party account--LinkedIn $ leetcode user -i
Cookie login with cookie $ leetcode user -c
Browse all questions $ leetcode list
Choose one question $ leetcode show 1 -g -l cpp
Coding it!
Run test(s) and pray... $ leetcode test ./two-sum.cpp -t '[3,2,4]\n7'
Submit final solution! $ leetcode submit ./two-sum.cpp
Run test(s) and pray... $ leetcode test ./two-sum.cpp -t '[3,2,4]\n7'
Submit final solution! $ leetcode submit ./two-sum.cpp
30 changes: 16 additions & 14 deletions docs/commands.md
Original file line number Diff line number Diff line change
@@ -3,20 +3,20 @@ layout: default
title: Commands Help
---

* [help](#help)
* [cache](#cache)
* [config](#config)
* [list](#list)
* [plugin](#plugin)
* [show](#show)
* [star](#star)
* [stat](#stat)
* [session](#session)
* [submission](#submission)
* [submit](#submit)
* [test](#test)
* [user](#user)
* [version](#version)
- [help](#help)
- [cache](#cache)
- [config](#config)
- [list](#list)
- [plugin](#plugin)
- [session](#session)
- [show](#show)
- [star](#star)
- [stat](#stat)
- [submission](#submission)
- [submit](#submit)
- [test](#test)
- [user](#user)
- [version](#version)

# help

@@ -141,6 +141,7 @@ Navigate the quations.
* `algorithms`
* `database`
* `shell`
* `concurrency`
* by company (require plugin)
* by topic (require plugin)
* `-s` to show statistic counters of the output list.
@@ -235,6 +236,7 @@ Display question details. With `-g`/`-l`/`-x`, the code template would be auto g
* java
* javascript
* mysql
* php
* python
* python3
* ruby
6 changes: 6 additions & 0 deletions lib/cache.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,12 @@ cache.init = function() {
file.mkdir(file.cacheDir());
};

cache.deleteAll = function () {
cache.list().forEach(value => {
cache.del(value.name);
})
};

cache.get = function(k) {
const fullpath = file.cacheFile(k);
if (!file.exist(fullpath)) return null;
8 changes: 7 additions & 1 deletion lib/commands/list.js
Original file line number Diff line number Diff line change
@@ -28,6 +28,12 @@ const cmd = {
default: false,
describe: 'Show extra details: category, companies, tags.'
})
.option('T', {
alias: 'dontTranslate',
type: 'boolean',
default: false,
describe: 'Set to true to disable endpoint\'s translation',
})
.positional('keyword', {
type: 'string',
default: '',
@@ -73,7 +79,7 @@ cmd.handler = function(argv) {
problem.fid,
problem.name,
h.prettyLevel(problem.level),
problem.percent.toFixed(2));
(problem.percent || 0).toFixed(2));

if (argv.extra) {
let badges = [problem.category];
19 changes: 1 addition & 18 deletions lib/commands/session.js
Original file line number Diff line number Diff line change
@@ -99,24 +99,7 @@ cmd.handler = function(argv) {
}

if (argv.delete) {
log.info([
chalk.red.bold('CAREFUL! This action CANNOT be undone!'),
'\nThis will permanently delete all your submissions',
'and progress associated with this session.',
'\nAre you sure you want to delete this session?\n',
'\nPlease type in the session\'s',
chalk.yellow.bold('number of accepted submissions'),
'to confirm.\n'
].join(' '));

prompt.colors = false;
prompt.message = '';
prompt.start();
prompt.get([{name: 'answer', type: 'integer', required: true}], function(e, x) {
if (x.answer !== session.total_acs) return;
return core.deleteSession(session, printSessions);
});
return;
return core.deleteSession(session, printSessions);
}
}
printSessions(null, sessions);
20 changes: 16 additions & 4 deletions lib/commands/show.js
Original file line number Diff line number Diff line change
@@ -57,6 +57,12 @@ const cmd = {
default: false,
describe: 'Show extra question details in source code'
})
.option('T', {
alias: 'dontTranslate',
type: 'boolean',
default: false,
describe: 'Set to true to disable endpoint\'s translation',
})
.positional('keyword', {
type: 'string',
default: '',
@@ -152,14 +158,20 @@ function showProblem(problem, argv) {
log.printf('* %s', problem.category);
log.printf('* %s (%s%%)', h.prettyLevel(problem.level), problem.percent.toFixed(2));

if (filename)
log.printf('* Source Code: %s', chalk.yellow.underline(filename));
if (problem.likes)
log.printf('* Likes: %s', problem.likes);
if (problem.dislikes)
log.printf('* Dislikes: %s', problem.dislikes);
else
log.printf('* Dislikes: -');
if (problem.totalAC)
log.printf('* Total Accepted: %s', problem.totalAC);
if (problem.totalSubmit)
log.printf('* Total Submissions: %s', problem.totalSubmit);
if (problem.testable && problem.testcase)
log.printf('* Testcase Example: %s', chalk.yellow(util.inspect(problem.testcase)));
if (filename)
log.printf('* Source Code: %s', chalk.yellow.underline(filename));

log.info();
log.info(problem.desc);
@@ -169,7 +181,7 @@ cmd.handler = function(argv) {
session.argv = argv;
if (argv.keyword.length > 0) {
// show specific one
core.getProblem(argv.keyword, function(e, problem) {
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
if (e) return log.fail(e);
showProblem(problem, argv);
});
@@ -188,7 +200,7 @@ cmd.handler = function(argv) {
if (problems.length === 0) return log.fail('Problem not found!');

const problem = _.sample(problems);
core.getProblem(problem, function(e, problem) {
core.getProblem(problem, !argv.dontTranslate, function(e, problem) {
if (e) return log.fail(e);
showProblem(problem, argv);
});
3 changes: 2 additions & 1 deletion lib/commands/star.js
Original file line number Diff line number Diff line change
@@ -29,7 +29,8 @@ const cmd = {

cmd.handler = function(argv) {
session.argv = argv;
core.getProblem(argv.keyword, function(e, problem) {
// translation doesn't affect question lookup
core.getProblem(argv.keyword, true, function(e, problem) {
if (e) return log.fail(e);

core.starProblem(problem, !argv.delete, function(e, starred) {
6 changes: 3 additions & 3 deletions lib/commands/stat.js
Original file line number Diff line number Diff line change
@@ -98,13 +98,13 @@ function showGraph(problems) {
const header = _.range(groups)
.map(x => sprintf('%4s%18s', x * 10 + 1, x * 10 + 10))
.join('');
log.info(' ' + header);
log.info(' ' + header);

const graph = [];
for (let problem of problems)
graph[problem.fid] = ICONS[problem.state] || ICONS.none;

let line = [sprintf(' %03s', 0)];
let line = [sprintf(' %04s', 0)];
for (let i = 1, n = graph.length; i <= n; ++i) {
// padding before group
if (i % 10 === 1) line.push(' ');
@@ -114,7 +114,7 @@ function showGraph(problems) {
// time to start new row
if (i % (10 * groups) === 0 || i === n) {
log.info(line.join(' '));
line = [sprintf(' %03s', i)];
line = [sprintf(' %04s', i)];
}
}

10 changes: 8 additions & 2 deletions lib/commands/submission.js
Original file line number Diff line number Diff line change
@@ -42,6 +42,12 @@ const cmd = {
default: false,
describe: 'Show extra question details in submission code'
})
.option('T', {
alias: 'dontTranslate',
type: 'boolean',
default: false,
describe: 'Set to true to disable endpoint\'s translation',
})
.positional('keyword', {
type: 'string',
default: '',
@@ -69,7 +75,7 @@ function doTask(problem, queue, cb) {

if (argv.extra) {
// have to get problem details, e.g. problem description.
core.getProblem(problem.fid, function(e, problem) {
core.getProblem(problem.fid, !argv.dontTranslate, function(e, problem) {
if (e) return cb(e);
exportSubmission(problem, argv, onTaskDone);
});
@@ -135,7 +141,7 @@ cmd.handler = function(argv) {
if (!argv.keyword)
return log.fail('missing keyword?');

core.getProblem(argv.keyword, function(e, problem) {
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
if (e) return log.fail(e);
q.addTask(problem).run();
});
55 changes: 36 additions & 19 deletions lib/commands/submit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var util = require('util');
var lodash = require('lodash');

var h = require('../helper');
var file = require('../file');
@@ -29,7 +30,7 @@ function printResult(actual, k) {
const v = actual[k] || '';
const lines = Array.isArray(v) ? v : [v];
for (let line of lines) {
if (k !== 'state') line = k + ': ' + line;
if (k !== 'state') line = lodash.startCase(k) + ': ' + line;
log.info(' ' + h.prettyText(' ' + line, actual.ok));
}
}
@@ -48,7 +49,8 @@ cmd.handler = function(argv) {

const meta = file.meta(argv.filename);

core.getProblem(meta.id, function(e, problem) {
// translation doesn't affect problem lookup
core.getProblem(meta.id, true, function(e, problem) {
if (e) return log.fail(e);

problem.file = argv.filename;
@@ -66,24 +68,39 @@ cmd.handler = function(argv) {
if (result.ok) {
session.updateStat('ac', 1);
session.updateStat('ac.set', problem.fid);
core.getSubmission({id: result.id}, function(e, submission) {
if (e || !submission || !submission.distributionChart)
return log.warn('Failed to get submission beat ratio.');

const lang = submission.distributionChart.lang;
const scores = submission.distributionChart.distribution;
const myRuntime = parseFloat(result.runtime);

let ratio = 0.0;
for (let score of scores) {
if (parseFloat(score[0]) > myRuntime)
ratio += parseFloat(score[1]);
}

printLine(result, 'Your runtime beats %d %% of %s submissions',
ratio.toFixed(2), lang);
});

(function () {
if (result.runtime_percentile)
printLine(result, 'Your runtime beats %d %% of %s submissions',
result.runtime_percentile.toFixed(2), result.lang);
else
return log.warn('Failed to get runtime percentile.');
if (result.memory && result.memory_percentile)
printLine(result, 'Your memory usage beats %d %% of %s submissions (%s)',
result.memory_percentile.toFixed(2), result.lang, result.memory);
else
return log.warn('Failed to get memory percentile.');
})();

// core.getSubmission({id: result.id}, function(e, submission) {
// if (e || !submission || !submission.distributionChart)
// return log.warn('Failed to get submission beat ratio.');

// const lang = submission.distributionChart.lang;
// const scores = submission.distributionChart.distribution;
// const myRuntime = parseFloat(result.runtime);

// let ratio = 0.0;
// for (let score of scores) {
// if (parseFloat(score[0]) > myRuntime)
// ratio += parseFloat(score[1]);
// }

// printLine(result, 'Your runtime beats %d %% of %s submissions',
// ratio.toFixed(2), lang);
// });
} else {
result.testcase = result.testcase.slice(1, -1).replace(/\\n/g, '\n');
printResult(result, 'error');
printResult(result, 'testcase');
printResult(result, 'answer');
Loading