Skip to content

Commit 4f59f4c

Browse files
author
Rustem Galiullin
committed
add Leet open command to open current question in browser
1 parent f114abc commit 4f59f4c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lua/leetcode-ui/layout/console.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ function ConsoleLayout:set_keymaps(keymaps)
7373
end
7474
end
7575

76+
function ConsoleLayout:open_url()
77+
local command
78+
local p = io.popen("uname")
79+
local os_name
80+
81+
if p then
82+
os_name = p:read("*l")
83+
p:close()
84+
end
85+
86+
if os_name == "Linux" then
87+
command = string.format("xdg-open '%s'", self.question.cache.link)
88+
elseif os_name == "Darwin" then
89+
command = string.format("open '%s'", self.question.cache.link)
90+
else
91+
-- Fallback to Windows if uname is not available or does not match Linux/Darwin.
92+
command = string.format("start \"\" \"%s\"", self.question.cache.link)
93+
end
94+
95+
os.execute(command)
96+
end
97+
7698
---@param parent lc.ui.Question
7799
function ConsoleLayout:init(parent)
78100
self.question = parent

lua/leetcode/command/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ function cmd.ui_languages()
246246
languages:show()
247247
end
248248

249+
function cmd.open()
250+
local utils = require("leetcode.utils")
251+
utils.auth_guard()
252+
local q = utils.curr_question()
253+
if q then q.console:open_url() end
254+
end
255+
249256
function cmd.fix()
250257
require("leetcode.cache.cookie").delete()
251258
require("leetcode.cache.problemlist").delete()
@@ -373,6 +380,7 @@ cmd.commands = {
373380
daily = { cmd.qot },
374381
fix = { cmd.fix },
375382
yank = { cmd.yank },
383+
open = { cmd.open },
376384

377385
list = {
378386
cmd.problems,

0 commit comments

Comments
 (0)