Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lua/leetcode/command/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,82 @@ function cmd.qot()
end)
end

local function split_num(s)
local parts = {}
for part in s:gmatch("[%d]+") do
table.insert(parts, tonumber(part))
end
return parts
end

local function compare_num_strs(a, b)
local ta = split_num(a)
local tb = split_num(b)
for i = 1, math.max(#ta, #tb) do
local va = ta[i] or 0
local vb = tb[i] or 0
if va ~= vb then
return va < vb
end
end
return false
end

local function get_sibling_question(offset)
local utils = require("leetcode.utils")
utils.auth_guard()
local q = utils.curr_question()
if not q then
return log.error("No question is currently open")
end

local current_id = q.q.frontend_id
local prefix, num_str = string.match(current_id, "^(.-)([%d%.]+)$")

if not prefix or not num_str then
return log.error("Cannot parse frontend_id: " .. tostring(current_id))
end

local problems = require("leetcode.cache.problemlist").get()

local filtered = {}
for _, p in ipairs(problems) do
local p_prefix, p_num_str = string.match(p.frontend_id, "^(.-)([%d%.]+)$")
if p_prefix == prefix and p_num_str then
table.insert(filtered, { p = p, num_str = p_num_str })
end
end

table.sort(filtered, function(a, b)
return compare_num_strs(a.num_str, b.num_str)
end)

local sibling_item
for i, item in ipairs(filtered) do
if item.p.frontend_id == current_id then
if filtered[i + offset] then
sibling_item = filtered[i + offset].p
end
break
end
end

if sibling_item then
local Question = require("leetcode-ui.question")
Question(sibling_item):mount()
else
log.warn("No " .. (offset > 0 and "next" or "previous") .. " question found")
end
end

function cmd.next_question()
get_sibling_question(1)
end

function cmd.prev_question()
get_sibling_question(-1)
end

function cmd.random_question(opts)
require("leetcode.utils").auth_guard()

Expand Down Expand Up @@ -634,6 +710,8 @@ cmd.commands = {
cmd.problems,
_args = arguments.list,
},
next = { cmd.next_question },
prev = { cmd.prev_question },
random = {
cmd.random_question,
_args = arguments.random,
Expand Down