feat(config): open url option

This commit is contained in:
Barrett Ruth 2025-10-12 16:19:02 -04:00
parent 2bc56195fd
commit c0e175d84b
9 changed files with 45 additions and 18 deletions

View file

@ -9,6 +9,7 @@
---@field index_map table<string, number>
---@field name string
---@field display_name string
---@field url string
---@class ContestSummary
---@field display_name string
@ -94,11 +95,13 @@ end
---@param platform string
---@param contest_id string
---@param problems Problem[]
function M.set_contest_data(platform, contest_id, problems)
---@param url string
function M.set_contest_data(platform, contest_id, problems, url)
vim.validate({
platform = { platform, 'string' },
contest_id = { contest_id, 'string' },
problems = { problems, 'table' },
url = { url, 'string' },
})
cache_data[platform] = cache_data[platform] or {}
@ -109,6 +112,7 @@ function M.set_contest_data(platform, contest_id, problems)
display_name = prev.display_name,
problems = problems,
index_map = {},
url = url,
}
for i, p in ipairs(out.problems) do
out.index_map[p.id] = i

View file

@ -43,6 +43,7 @@
---@field platforms table<string, CpPlatform>
---@field hooks Hooks
---@field debug boolean
---@field open_url boolean
---@field scrapers string[]
---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string
---@field ui CpUI
@ -58,6 +59,7 @@ local utils = require('cp.utils')
-- defaults per the new single schema
---@type cp.Config
M.defaults = {
open_url = false,
languages = {
cpp = {
extension = 'cc',
@ -223,9 +225,7 @@ function M.setup(user_config)
vim.validate({
hooks = { cfg.hooks, { 'table' } },
ui = { cfg.ui, { 'table' } },
})
vim.validate({
open_url = { cfg.open_url, { 'boolean', 'nil' }, true },
before_run = { cfg.hooks.before_run, { 'function', 'nil' }, true },
before_debug = { cfg.hooks.before_debug, { 'function', 'nil' }, true },
setup_code = { cfg.hooks.setup_code, { 'function', 'nil' }, true },

View file

@ -77,6 +77,11 @@ function M.setup_contest(platform, contest_id, problem_id, language)
local pid = problem_id and problem_id or problems[1].id
M.setup_problem(pid, language)
start_tests(platform, contest_id, problems)
if contest_data.url and config_module.get_config().open_url then
vim.print('opening')
vim.ui.open(contest_data.url)
end
end
local contest_data = cache.get_contest_data(platform, contest_id)
@ -134,7 +139,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
contest_id,
vim.schedule_wrap(function(result)
local problems = result.problems or {}
cache.set_contest_data(platform, contest_id, problems)
cache.set_contest_data(platform, contest_id, problems, result.url)
local prov = state.get_provisional()
if not prov or prov.platform ~= platform or prov.contest_id ~= contest_id then
return
@ -150,8 +155,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
if not pid then
return
end
M.setup_problem(pid, prov.language)
start_tests(platform, contest_id, cd.problems)
proceed(cd)
end)
)
return

View file

@ -12,7 +12,7 @@ local _timeout_path = nil
local _timeout_reason = nil
local function is_windows()
return uname and uname.sysname == 'Windows_NT'
return uname.sysname == 'Windows_NT'
end
local function check_time_is_gnu_time(bin)