diff --git a/package.json b/package.json index cad8125..4d1d017 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "onCommand:leetcode.manageSessions", "onCommand:leetcode.refreshExplorer", "onCommand:leetcode.pickOne", + "onCommand:leetcode.problemOfToday", "onCommand:leetcode.showProblem", "onCommand:leetcode.previewProblem", "onCommand:leetcode.searchProblem", @@ -81,6 +82,12 @@ "title": "Pick One", "category": "LeetCode" }, + { + "command": "leetcode.problemOfToday", + "title": "Problem Of Today", + "category": "LeetCode", + "icon": "$(calendar)" + }, { "command": "leetcode.showProblem", "title": "Show Problem", @@ -175,6 +182,11 @@ "when": "view == leetCodeExplorer", "group": "navigation@3" }, + { + "command": "leetcode.problemOfToday", + "when": "view == leetCodeExplorer", + "group": "navigation@4" + }, { "command": "leetcode.pickOne", "when": "view == leetCodeExplorer", diff --git a/src/commands/show.ts b/src/commands/show.ts index 3aebce8..d060cf1 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -52,6 +52,33 @@ export async function pickOne(): Promise { await showProblemInternal(randomProblem); } +export async function problemOfToday(): Promise { + if (!leetCodeManager.getUser()) { + promptForSignIn(); + return; + } + try { + const nodes: LeetCodeNode[] = explorerNodeManager.getAllNodes() + const problemOfTodayStr: string = await leetCodeExecutor.problemOfToday(); + const lines: string[] = problemOfTodayStr.split("\n"); + const reg: RegExp = /^\[(.+)\]\s.*/ + const match: RegExpMatchArray | null = lines[0].match(reg); + if (match != null) { + const id = match[1] + const problemOfToday: IProblem[] = nodes.filter(one => one.id === id); + if (problemOfToday.length != 1) { + await promptForOpenOutputChannel("Fail to load problem of today. You may need to refresh the problem list.", DialogType.error); + } + else { + await showProblemInternal(problemOfToday[0]); + } + } + } + catch { + await promptForOpenOutputChannel("Fail to load problem of today. Open the output channel for details.", DialogType.error); + } +} + export async function showProblem(node?: LeetCodeNode): Promise { if (!node) { return; diff --git a/src/extension.ts b/src/extension.ts index 5bd026f..452e65a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -56,6 +56,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)), vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)), vscode.commands.registerCommand("leetcode.pickOne", () => show.pickOne()), + vscode.commands.registerCommand("leetcode.problemOfToday", () => show.problemOfToday()), vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()), vscode.commands.registerCommand("leetcode.showSolution", (input: LeetCodeNode | vscode.Uri) => show.showSolution(input)), vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()), diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index d2332c7..d8b611d 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -100,6 +100,11 @@ class LeetCodeExecutor implements Disposable { return await this.executeCommandEx(this.nodeExecutable, cmd); } + public async problemOfToday(): Promise { + const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", "-d"]; + return await this.executeCommandWithProgressEx("Loading problem of today...", this.nodeExecutable, cmd); + } + public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean): Promise { const templateType: string = showDescriptionInComment ? "-cx" : "-c"; const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNode.id, templateType, "-l", language];