Skip to content

Commit 3fd19b0

Browse files
committed
feat: Question:filename
1 parent 1a7b596 commit 3fd19b0

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ hooks = {
303303
},
304304
```
305305

306-
### filename
306+
### custom filename
307307

308308
This function is used for both creating and finding existing questions
309309

@@ -312,7 +312,7 @@ This function is used for both creating and finding existing questions
312312

313313
```lua
314314
---@type lc.filename
315-
filename = function(id, title, alias, extension)
315+
custom_filename = function(id, title, alias, extension)
316316
local parts = alias and { id, title, alias, extension } or { id, title, extension }
317317
return table.concat(parts, ".")
318318
end,

lua/leetcode-ui/question.lua

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,36 @@ function Question:reset_lines()
5757
self:set_lines(new_lines)
5858
end
5959

60-
---@return string path, boolean existed
61-
function Question:path()
60+
function Question:filename()
6261
local lang = utils.get_lang(self.lang)
6362

64-
---@type lc.filename
65-
local generate_fn = config.user.filename
66-
or function(id, title, alias, extension)
67-
local parts = alias and { id, title, alias, extension } or { id, title, extension }
68-
return table.concat(parts, ".")
63+
local id, title, alias, extension = self.q.frontend_id, self.q.title_slug, lang.alias, lang.ft
64+
65+
local parts = alias and { id, title, alias, extension } or { id, title, extension }
66+
local filename = table.concat(parts, ".")
67+
68+
if config.user.custom_filename then
69+
local ok, cfn = pcall(config.user.custom_filename, id, title, alias, extension)
70+
71+
if ok then
72+
return cfn
73+
else
74+
local err_msg = ("[opts.custom_filename]\n%s\nfalling back to `%s`"):format(
75+
cfn,
76+
filename
77+
)
78+
log.error(err_msg, true)
6979
end
80+
end
81+
82+
return filename
83+
end
7084

71-
local filename = generate_fn(self.q.frontend_id, self.q.title_slug, lang.alias, lang.ft)
85+
---@return string path, boolean existed
86+
function Question:path()
87+
local fn = self:filename()
7288

73-
self.file = config.storage.home:joinpath(filename)
89+
self.file = config.storage.home:joinpath(fn)
7490
local existed = self.file:exists()
7591

7692
if not existed then

lua/leetcode/config/template.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ local M = {
115115
},
116116

117117
---@type lc.filename|nil,
118-
filename = nil,
118+
custom_filename = nil,
119119

120120
keys = {
121121
toggle = { "q" }, ---@type string|string[]

lua/leetcode/logger/init.lua

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ local function normalize(msg)
1414
return type(msg) == "string" and t(msg) or vim.inspect(msg)
1515
end
1616

17-
-- ---@private
18-
-- ---@param msg any
19-
-- ---@param lvl? integer
20-
-- ---@return any
21-
-- logger.log = vim.schedule_wrap(function(msg, lvl)
22-
-- end)
23-
24-
function logger.log(msg, lvl)
17+
---@param msg any
18+
---@param lvl? integer
19+
---@param schedule? boolean
20+
function logger.log(msg, lvl, schedule)
2521
if not config.user.logging then
2622
return
2723
end
@@ -34,35 +30,47 @@ function logger.log(msg, lvl)
3430
msg = debug.traceback(msg .. "\n")
3531
end
3632

37-
vim.notify(msg, lvl, { title = title })
33+
local function notify_send()
34+
vim.notify(msg, lvl, { title = title })
35+
end
36+
37+
if schedule then
38+
vim.schedule(notify_send)
39+
else
40+
notify_send()
41+
end
3842
end
3943

4044
---@param msg any
41-
logger.info = function(msg)
42-
logger.log(msg)
45+
---@param schedule? boolean
46+
logger.info = function(msg, schedule)
47+
logger.log(msg, nil, schedule)
4348
end
4449

4550
---@param msg any
46-
logger.warn = function(msg)
47-
logger.log(msg, lvls.WARN)
51+
---@param schedule? boolean
52+
logger.warn = function(msg, schedule)
53+
logger.log(msg, lvls.WARN, schedule)
4854
end
4955

5056
---@param msg any
51-
logger.error = function(msg)
52-
logger.log(msg, lvls.ERROR)
57+
---@param schedule? boolean
58+
logger.error = function(msg, schedule)
59+
logger.log(msg, lvls.ERROR, schedule)
5360
logger.debug(msg)
5461
end
5562

5663
---@param err lc.err
57-
logger.err = function(err)
64+
---@param schedule? boolean
65+
logger.err = function(err, schedule)
5866
if not err then
5967
return logger.error("error")
6068
end
6169

6270
local msg = err.msg or ""
6371
local lvl = err.lvl or lvls.ERROR
6472

65-
logger.log(msg, lvl)
73+
logger.log(msg, lvl, schedule)
6674
end
6775

6876
---@param msg any

0 commit comments

Comments
 (0)