-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrecent.lua
124 lines (106 loc) · 3.44 KB
/
recent.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
local log = require("leetcode.logger")
local t = require("leetcode.translator")
local utils = require("leetcode.utils")
local ui_utils = require("leetcode-ui.utils")
local Question = require("leetcode-ui.question")
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local config = require("leetcode.config")
local entry_display = require("telescope.pickers.entry_display")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
---@param question lc.cache.Question
---
---@return string
local function question_formatter(question)
return ("%s. %s %s %s"):format(
tostring(question.frontend_id),
question.title,
question.title_cn,
question.title_slug
)
end
---@param question lc.cache.Question
local function display_difficulty(question)
local hl = ui_utils.diff_to_hl(question.difficulty)
return { config.icons.square, hl }
end
---@param question lc.cache.Question
local function display_user_status(question)
if question.paid_only then
return config.auth.is_premium and config.icons.hl.unlock or config.icons.hl.lock
end
if question.status == vim.NIL then
return { "" }
end
return config.icons.hl.status[question.status] or { "" }
end
---@param question lc.cache.Question
local function display_question(question)
local index = { question.frontend_id .. ".", "leetcode_normal" }
local title = { utils.translate(question.title, question.title_cn) }
return unpack({ index, title })
end
local displayer = entry_display.create({
separator = " ",
items = {
{ width = 1 },
{ width = 1 },
{ width = 5 },
{ remaining = true },
},
})
local function make_display(entry)
---@type lc.cache.Question
local q = entry.value
return displayer({
display_user_status(q),
display_difficulty(q),
display_question(q),
})
end
local function entry_maker(entry)
return {
value = entry,
display = make_display,
ordinal = question_formatter(entry),
}
end
local theme = require("telescope.themes").get_dropdown({
layout_config = {
width = 100,
height = 20,
},
})
return {
pick = function()
local Recent = require("leetcode.cache.recent")
local questions = Recent.convert()
pickers
.new(theme, {
prompt_title = t("Recent Questions"),
finder = finders.new_table({
results = questions,
entry_maker = entry_maker,
}),
sorter = conf.generic_sorter(theme),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if not selection then
return
end
local q = selection.value
if q.paid_only and not config.auth.is_premium then
return log.warn("Question is for premium users only")
end
actions.close(prompt_bufnr)
Question(q):mount()
end)
return true
end,
})
:find()
end,
}