cp.nvim/lua/cp/pickers/fzf_lua.lua
Barrett Ruth 3c11d609f5
feat(codechef): implement full CodeChef support (#354)
## Problem

CodeChef had no working login, submit, or contest list. The browser
selectors were wrong, the contest list was missing present/past
contests,
and problem/contest URLs were unset.

## Solution

Fix login and submit selectors for the Drupal-based site. Paginate
`/api/list/contests/past` to collect all 228 Starters, then expand each
parent contest into individual division entries (e.g. `START228 (Div.
4)`).
Add language IDs, correct `url`/`contest_url`/`standings_url` in
metadata,
and make `:CP <platform>` open the contest picker directly.
2026-03-06 23:10:44 -05:00

100 lines
2.4 KiB
Lua

local logger = require('cp.log')
local picker_utils = require('cp.pickers')
local M = {}
local function contest_picker(platform, refresh, language)
local constants = require('cp.constants')
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform]
local fzf = require('fzf-lua')
local contests = picker_utils.get_platform_contests(platform, refresh)
if vim.tbl_isempty(contests) then
logger.log(
("No contests found for platform '%s'"):format(platform_display_name),
{ level = vim.log.levels.WARN }
)
return
end
local entries = vim.tbl_map(function(contest)
return contest.display_name
end, contests)
return fzf.fzf_exec(entries, {
prompt = ('Select Contest (%s)> '):format(platform_display_name),
fzf_opts = {
['--header'] = 'ctrl-r: refresh',
},
actions = {
['default'] = function(selected)
if vim.tbl_isempty(selected) then
return
end
local selected_name = selected[1]
local contest = nil
for _, c in ipairs(contests) do
if c.display_name == selected_name then
contest = c
break
end
end
if contest then
local cp = require('cp')
local fargs = { platform, contest.id }
if language then
table.insert(fargs, '--lang')
table.insert(fargs, language)
end
cp.handle_command({ fargs = fargs })
end
end,
['ctrl-r'] = function()
contest_picker(platform, true, language)
end,
},
})
end
---@param language? string
---@param platform? string
function M.pick(language, platform)
if platform then
contest_picker(platform, false, language)
return
end
local fzf = require('fzf-lua')
local platforms = picker_utils.get_platforms()
local entries = vim.tbl_map(function(p)
return p.display_name
end, platforms)
return fzf.fzf_exec(entries, {
prompt = 'Select Platform> ',
actions = {
['default'] = function(selected)
if vim.tbl_isempty(selected) then
return
end
local selected_name = selected[1]
local found = nil
for _, p in ipairs(platforms) do
if p.display_name == selected_name then
found = p
break
end
end
if found then
contest_picker(found.id, false, language)
end
end,
},
})
end
return M