From 4f59f4cdf969f6c8dab0f96282345953f080f18e Mon Sep 17 00:00:00 2001 From: Rustem Galiullin Date: Wed, 7 Feb 2024 11:47:40 +0400 Subject: [PATCH 1/4] add Leet open command to open current question in browser --- lua/leetcode-ui/layout/console.lua | 22 ++++++++++++++++++++++ lua/leetcode/command/init.lua | 8 ++++++++ 2 files changed, 30 insertions(+) diff --git a/lua/leetcode-ui/layout/console.lua b/lua/leetcode-ui/layout/console.lua index f7801e44..bda719ae 100644 --- a/lua/leetcode-ui/layout/console.lua +++ b/lua/leetcode-ui/layout/console.lua @@ -73,6 +73,28 @@ function ConsoleLayout:set_keymaps(keymaps) end end +function ConsoleLayout:open_url() + local command + local p = io.popen("uname") + local os_name + + if p then + os_name = p:read("*l") + p:close() + end + + if os_name == "Linux" then + command = string.format("xdg-open '%s'", self.question.cache.link) + elseif os_name == "Darwin" then + command = string.format("open '%s'", self.question.cache.link) + else + -- Fallback to Windows if uname is not available or does not match Linux/Darwin. + command = string.format("start \"\" \"%s\"", self.question.cache.link) + end + + os.execute(command) +end + ---@param parent lc.ui.Question function ConsoleLayout:init(parent) self.question = parent diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index c0d44605..7e436e25 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -246,6 +246,13 @@ 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 q.console:open_url() end +end + function cmd.fix() require("leetcode.cache.cookie").delete() require("leetcode.cache.problemlist").delete() @@ -373,6 +380,7 @@ cmd.commands = { daily = { cmd.qot }, fix = { cmd.fix }, yank = { cmd.yank }, + open = { cmd.open }, list = { cmd.problems, From 98b6345d531f450bca80344661212032754e419b Mon Sep 17 00:00:00 2001 From: Rustem Galiullin Date: Wed, 7 Feb 2024 11:55:38 +0400 Subject: [PATCH 2/4] add cmd to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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 From a5111a5bda19049f2347afd0e824db12ee206e6e Mon Sep 17 00:00:00 2001 From: Rustem Galiullin Date: Fri, 9 Feb 2024 11:37:05 +0400 Subject: [PATCH 3/4] refactor code as requested --- lua/leetcode-ui/layout/console.lua | 22 ---------------------- lua/leetcode/command/init.lua | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lua/leetcode-ui/layout/console.lua b/lua/leetcode-ui/layout/console.lua index bda719ae..f7801e44 100644 --- a/lua/leetcode-ui/layout/console.lua +++ b/lua/leetcode-ui/layout/console.lua @@ -73,28 +73,6 @@ function ConsoleLayout:set_keymaps(keymaps) end end -function ConsoleLayout:open_url() - local command - local p = io.popen("uname") - local os_name - - if p then - os_name = p:read("*l") - p:close() - end - - if os_name == "Linux" then - command = string.format("xdg-open '%s'", self.question.cache.link) - elseif os_name == "Darwin" then - command = string.format("open '%s'", self.question.cache.link) - else - -- Fallback to Windows if uname is not available or does not match Linux/Darwin. - command = string.format("start \"\" \"%s\"", self.question.cache.link) - end - - os.execute(command) -end - ---@param parent lc.ui.Question function ConsoleLayout:init(parent) self.question = parent diff --git a/lua/leetcode/command/init.lua b/lua/leetcode/command/init.lua index 7e436e25..53018611 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -250,7 +250,22 @@ function cmd.open() local utils = require("leetcode.utils") utils.auth_guard() local q = utils.curr_question() - if q then q.console:open_url() end + + 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 + + vim.cmd("!" .. command) + end end function cmd.fix() From 3c03e671894bd4908c2d578c1c85e6e78b87fc6c Mon Sep 17 00:00:00 2001 From: kawre Date: Tue, 13 Feb 2024 14:03:22 +0100 Subject: [PATCH 4/4] docs(readme): cn `Leet open` --- README.zh.md | 2 ++ lua/leetcode/command/init.lua | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) 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 53018611..209e9832 100644 --- a/lua/leetcode/command/init.lua +++ b/lua/leetcode/command/init.lua @@ -264,7 +264,7 @@ function cmd.open() command = string.format("start \"\" \"%s\"", q.cache.link) end - vim.cmd("!" .. command) + os.execute(command) end end