-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathinit.lua
102 lines (88 loc) · 2.51 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
local log = require("leetcode.logger")
local config = require("leetcode.config")
---@return "fzf" | "telescope" | "snacks"
local function resolve_provider()
---@type string
local provider = config.user.picker.provider
if provider == nil then
local fzf_ok = pcall(require, "fzf-lua")
if fzf_ok then
return "fzf"
end
local telescope_ok = pcall(require, "telescope")
if telescope_ok then
return "telescope"
end
local snacks_ok = pcall(require, "snacks.picker")
if snacks_ok then
return "snacks"
end
error("no supported picker provider found")
else
local mod = provider
if provider == "snacks-picker" then
provider = "snacks"
mod = "snacks.picker"
end
local provider_ok = pcall(require, mod)
assert(provider_ok, ("specified picker provider not found: `%s`"):format(provider))
return provider == "fzf-lua" and "fzf" or provider
end
end
---@class leet.Picker
local P = {}
P.provider = resolve_provider()
function P.hl_to_ansi(hl_group)
local color = vim.api.nvim_get_hl(0, { name = hl_group })
if color and color.fg then
return string.format(
"\x1b[38;2;%d;%d;%dm",
bit.rshift(color.fg, 16),
bit.band(bit.rshift(color.fg, 8), 0xFF),
bit.band(color.fg, 0xFF)
)
end
return ""
end
function P.apply_hl(text, hl_group)
if not hl_group then
return text
end
return P.hl_to_ansi(hl_group) .. text .. "\x1b[0m"
end
function P.normalize(items)
return vim.tbl_map(function(item)
return table.concat(
vim.tbl_map(function(col)
if type(col) == "table" then
return P.apply_hl(col[1], col[2])
else
return col
end
end, item.entry),
" "
)
end, items)
end
function P.pick(path, ...)
local rpath = table.concat({ "leetcode.picker", path, P.provider }, ".")
return require(rpath)(...)
end
function P.language(...)
P.pick("language", ...)
end
function P.question(...)
P.pick("question", ...)
end
function P.tabs()
local utils = require("leetcode.utils")
local tabs = utils.question_tabs()
if vim.tbl_isempty(tabs) then
return log.warn("No questions opened")
end
P.pick("tabs", tabs)
end
function P.hidden_field(text, deli)
return text:match(("([^%s]+)$"):format(deli))
end
return P