diff --git a/README.md b/README.md index 61d46d89..5a5dd995 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,8 @@ image_support = false, - `submit` submit currently opened question +- `open` opens the current question in a default browser + - `random` opens a random question - `daily` opens the question of today diff --git a/README.zh.md b/README.zh.md index 90eee016..3d2d301d 100644 --- a/README.zh.md +++ b/README.zh.md @@ -301,6 +301,8 @@ image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行 - `submit` 提交当前打开的问题 +- `open` 在默认浏览器中打开当前问题。 + - `random` 打开一个随机问题 - `daily` 打开今天的问题 diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index c0d44605..209e9832 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -246,6 +246,28 @@ function cmd.ui_languages() languages:show() end +function cmd.open() + local utils = require("leetcode.utils") + utils.auth_guard() + local q = utils.curr_question() + + if q then + local command + local os_name = vim.loop.os_uname().sysname + + if os_name == "Linux" then + command = string.format("xdg-open '%s'", q.cache.link) + elseif os_name == "Darwin" then + command = string.format("open '%s'", q.cache.link) + else + -- Fallback to Windows if uname is not available or does not match Linux/Darwin. + command = string.format("start \"\" \"%s\"", q.cache.link) + end + + os.execute(command) + end +end + function cmd.fix() require("leetcode.cache.cookie").delete() require("leetcode.cache.problemlist").delete() @@ -373,6 +395,7 @@ cmd.commands = { daily = { cmd.qot }, fix = { cmd.fix }, yank = { cmd.yank }, + open = { cmd.open }, list = { cmd.problems,