Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0c0145d

Browse files
committed
feat(result): improved handlers
1 parent 0a801bb commit 0c0145d

File tree

2 files changed

+92
-76
lines changed

2 files changed

+92
-76
lines changed

lua/leetcode/ui/console/components/stdout.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ local stdout = {}
99
---
1010
---@return lc-ui.Text | nil
1111
function stdout:init(output)
12+
output = output or ""
1213
local output_list = vim.split(output, "\n", { trimempty = true })
1314

15+
if vim.tbl_isempty(output_list) then return end
16+
1417
local t = {}
1518
for i = 1, #output_list, 1 do
1619
table.insert(t, NuiLine():append(output_list[i]))

lua/leetcode/ui/console/result.lua

Lines changed: 89 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,79 +22,85 @@ setmetatable(result, console_popup)
2222
---@param hi string
2323
function result:set_popup_border_hi(hi) self.popup.border:set_highlight(hi) end
2424

25+
function result:handle_accepted(item)
26+
local function perc_hi(perc) return perc >= 50 and "LeetCodeOk" or "LeetCodeError" end
27+
local group = Group:init({ opts = { spacing = 2 } })
28+
29+
local header = Text:init()
30+
header:append(item._.title, item._.hl)
31+
group:append(header)
32+
33+
-- runtime
34+
local status_runtime = NuiLine()
35+
local runtime_ms = item.display_runtime or vim.split(item.status_runtime, " ")[1] or "NIL"
36+
status_runtime:append(runtime_ms)
37+
status_runtime:append(" ms", "Comment")
38+
39+
local perc_runtime = NuiLine()
40+
perc_runtime:append(
41+
"Beats " .. string.format("%.2f", item.runtime_percentile) .. "% ",
42+
perc_hi(item.runtime_percentile)
43+
)
44+
perc_runtime:append("of users with " .. item.pretty_lang)
45+
46+
local runtime = Pre:init(NuiText("󰓅 Runtime", "LeetCodeNormal"), {
47+
status_runtime,
48+
perc_runtime,
49+
})
50+
group:append(runtime)
51+
52+
-- memory
53+
local status_memory = NuiLine()
54+
local s_mem = vim.split(item.status_memory, " ")
55+
status_memory:append(s_mem[1] .. " ")
56+
status_memory:append(s_mem[2], "Comment")
57+
58+
local perc_mem = NuiLine()
59+
perc_mem:append(
60+
"Beats " .. string.format("%.2f", item.memory_percentile) .. "% ",
61+
perc_hi(item.memory_percentile)
62+
)
63+
perc_mem:append("of users with " .. item.pretty_lang)
64+
65+
local memory = Pre:init(NuiText("󰍛 Memory", "LeetCodeNormal"), {
66+
status_memory,
67+
perc_mem,
68+
})
69+
group:append(memory)
70+
71+
self.layout:append(group)
72+
end
73+
2574
---@private
2675
---
2776
---@param item lc.runtime
2877
function result:handle_runtime(item) -- status code = 10
78+
if item._.submission then return self:handle_accepted(item) end
79+
2980
local group = Group:init({ opts = { spacing = 1 } })
3081
local header = Text:init()
31-
local is_submission = item.runtime_percentile ~= vim.NIL and item.memory_percentile ~= vim.NIL
32-
33-
if not is_submission then
34-
local h = NuiLine()
35-
h:append(item.lcnvim_title, item.lcnvim_hl)
36-
h:append(" | ")
37-
h:append("Runtime: " .. item.status_runtime, "Comment")
38-
header:append(h)
39-
group:append(header)
40-
41-
for i, answer in ipairs(item.code_answer) do
42-
local passed = item.compare_result:sub(i, i) == "1"
43-
44-
local text = Case:init(
45-
i,
46-
self.parent.testcase.testcases[i],
47-
answer,
48-
item.expected_code_answer[i],
49-
passed
50-
)
51-
group:append(text)
52-
53-
local stdout = Stdout:init(item.std_output_list[i])
54-
if stdout then group:append(stdout) end
55-
end
56-
else
57-
local function perc_hi(perc) return perc >= 50 and "LeetCodeOk" or "LeetCodeError" end
58-
59-
header:append(item.lcnvim_title, item.lcnvim_hl)
60-
group:append(header)
61-
62-
local status_runtime = NuiLine()
63-
local runtime_ms = item.display_runtime or vim.split(item.status_runtime, " ")[1] or "NIL"
64-
status_runtime:append(runtime_ms)
65-
status_runtime:append(" ms", "Comment")
6682

67-
local perc_runtime = NuiLine()
68-
perc_runtime:append(
69-
"Beats " .. string.format("%.2f", item.runtime_percentile) .. "% ",
70-
perc_hi(item.runtime_percentile)
83+
local h = NuiLine()
84+
h:append(item._.title, item._.hl)
85+
h:append(" | ")
86+
h:append("Runtime: " .. item.status_runtime, "Comment")
87+
header:append(h)
88+
group:append(header)
89+
90+
for i, answer in ipairs(item.code_answer) do
91+
local passed = item.compare_result:sub(i, i) == "1"
92+
93+
local text = Case:init(
94+
i,
95+
self.parent.testcase.testcases[i],
96+
answer,
97+
item.expected_code_answer[i],
98+
passed
7199
)
72-
perc_runtime:append("of users with " .. item.pretty_lang)
73-
74-
local runtime = Pre:init(NuiText("󰓅 Runtime"), {
75-
status_runtime,
76-
perc_runtime,
77-
})
78-
79-
local status_memory = NuiLine()
80-
local s_mem = vim.split(item.status_memory, " ")
81-
status_memory:append(s_mem[1] .. " ")
82-
status_memory:append(s_mem[2], "Comment")
83-
84-
local perc_mem = NuiLine()
85-
perc_mem:append(
86-
"Beats " .. string.format("%.2f", item.memory_percentile) .. "% ",
87-
perc_hi(item.memory_percentile)
88-
)
89-
perc_mem:append("of users with " .. item.pretty_lang)
90-
91-
local memory = Pre:init(NuiText("󰍛 Memory"), {
92-
status_memory,
93-
perc_mem,
94-
})
100+
group:append(text)
95101

96-
group:append(runtime)
97-
group:append(memory)
102+
local stdout = Stdout:init(item.std_output_list[i])
103+
if stdout then group:append(stdout) end
98104
end
99105

100106
self.layout:append(group)
@@ -104,24 +110,24 @@ end
104110
---
105111
---@param item lc.submission
106112
function result:handle_submission(item) -- status code = 11
113+
local group = Group:init({ opts = { spacing = 1 } })
114+
107115
local header = NuiLine()
108-
header:append(item.lcnvim_title, item.lcnvim_hl)
116+
header:append(item._.title, item._.hl)
109117
header:append(" | ")
110118
local testcases =
111119
string.format("%d/%d testcases passed", item.total_correct, item.total_testcases)
112120
header:append(testcases, "Comment")
121+
group:append(Text:init({ lines = { header } }))
113122

114-
self.layout:append(Text:init({ lines = { header, NuiLine() } }))
115-
116-
local group = Group:init({ opts = { spacing = 1 } })
117123
local text = Case:init(
118124
item.total_correct + 1,
119125
item.input_formatted,
120126
item.code_output,
121127
item.expected_output
122128
)
123-
124129
group:append(text)
130+
125131
if item.std_output then
126132
local stdout = Stdout:init(item.std_output)
127133
if stdout then group:append(stdout) end
@@ -145,7 +151,7 @@ function result:handle_limit_exceeded(item) -- status code = 14
145151
last_testcase:append(item.last_testcase:gsub("\n", " "), "LeetCodeIndent")
146152

147153
local pre_header = NuiLine()
148-
pre_header:append(" Last Executed Input", "")
154+
pre_header:append(" Last Executed Input", "LeetCodeNormal")
149155

150156
local last_exec = Pre:init(pre_header, { last_testcase })
151157
group:append(last_exec)
@@ -179,17 +185,22 @@ function result:handle_runtime_error(item) -- status code = 15
179185
end
180186

181187
function result:handle_internal_error(item) -- status code = 16
182-
local header = NuiLine()
183-
header:append(item.lcnvim_title, item.lcnvim_hl)
188+
local group = Group:init({ opts = { spacing = 1 } })
184189

190+
local header = NuiLine()
191+
header:append(item._.title, item._.hl)
185192
local text = Text:init({ lines = { header } })
186-
self.layout:append(text)
193+
group:append(text)
194+
195+
self.layout:append(group)
187196
end
188197

189198
---@private
190199
---
191200
---@param item lc.compile_error
192201
function result:handle_compile_error(item) -- status code = 20
202+
local group = Group:init({ opts = { spacing = 1 } })
203+
193204
local header = NuiLine()
194205
header:append(item._.title, item._.hl)
195206

@@ -198,7 +209,8 @@ function result:handle_compile_error(item) -- status code = 20
198209
table.insert(t, NuiLine():append(line, "LeetCodeError"))
199210
end
200211

201-
self.layout:append(Pre:init(header, t))
212+
group:append(Pre:init(header, t))
213+
self.layout:append(group)
202214
end
203215

204216
---@param item lc.interpreter_response
@@ -208,17 +220,18 @@ function result:handle_item(item)
208220
local success = false
209221
if item.status_code == 10 then
210222
success = item.compare_result:match("^[1]+$") and true or false
223+
item.status_msg = success and "Accepted" or "Wrong Answer"
211224
end
225+
212226
local submission = not item.submission_id:find("runcode") and true or false
213227
local hl = success and "LeetCodeOk" or "LeetCodeError"
214228

215229
item._ = {
216-
title = item.status_msg,
230+
title = "" .. item.status_msg,
217231
hl = hl,
218232
success = success,
219233
submission = submission,
220234
}
221-
log.info(item)
222235

223236
return item
224237
end

0 commit comments

Comments
 (0)