forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.ts
62 lines (59 loc) · 2.41 KB
/
list.ts
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
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { leetCodeManager } from "../leetCodeManager";
import { IProblem, ProblemState, UserStatus } from "../shared";
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
export async function listProblems(): Promise<IProblem[]> {
try {
if (leetCodeManager.getStatus() === UserStatus.SignedOut) {
return [];
}
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
const showLocked: boolean = !!leetCodeConfig.get<boolean>("showLocked");
const result: string = await leetCodeExecutor.listProblems(showLocked);
const problems: IProblem[] = [];
const lines: string[] = result.split("\n");
const reg: RegExp = /^(.)\s(.{1,2})\s(.)\s\[\s*(\d*)\s*\]\s*(.*)\s*(Easy|Medium|Hard)\s*\((\s*\d+\.\d+ %)\)/;
const { companies, tags } = await leetCodeExecutor.getCompaniesAndTags();
for (const line of lines) {
const match: RegExpMatchArray | null = line.match(reg);
if (match && match.length === 8) {
const id: string = match[4].trim();
problems.push({
id,
isFavorite: match[1].trim().length > 0,
locked: match[2].trim().length > 0,
state: parseProblemState(match[3]),
name: match[5].trim(),
difficulty: match[6].trim(),
passRate: match[7].trim(),
companies: companies[id] || ["Unknown"],
tags: tags[id] || ["Unknown"],
});
}
}
return problems.reverse();
} catch (error) {
await promptForOpenOutputChannel("Failed to list problems. Please open the output channel for details.", DialogType.error);
return [];
}
}
function parseProblemState(stateOutput: string): ProblemState {
if (!stateOutput) {
return ProblemState.Unknown;
}
switch (stateOutput.trim()) {
case "v":
case "✔":
case "√":
return ProblemState.AC;
case "X":
case "✘":
case "×":
return ProblemState.NotAC;
default:
return ProblemState.Unknown;
}
}