Skip to content

Commit 1b7eb93

Browse files
committed
feat: update new API syntax from backend
fix: use option compress for get stats
1 parent 20f6bcf commit 1b7eb93

File tree

10 files changed

+661
-706
lines changed

10 files changed

+661
-706
lines changed

lua/codeme/init.lua

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,49 @@ function M.track(is_save)
9191
})
9292
end
9393

94-
--- Get stats from backend
95-
function M.get_stats(callback, today_only)
96-
local cmd = M.config.codeme_bin .. " stats --json"
97-
if today_only then
98-
cmd = cmd .. " --today"
99-
end
94+
function M.get_stats(callback)
95+
local cmd = M.config.codeme_bin .. " api --compact"
96+
local output = {}
10097

10198
vim.fn.jobstart(cmd, {
102-
stdout_buffered = true,
99+
stdout_buffered = true, -- still fine
103100
on_stdout = function(_, data)
101+
if data then
102+
vim.list_extend(output, data)
103+
end
104+
end,
105+
on_stderr = function(_, data)
104106
if data and #data > 0 then
105-
local filtered = vim.tbl_filter(function(line)
106-
return line ~= "" and (line:match("^%s*{") or line:match("[,}%]]%s*$"))
107-
end, data)
108-
109-
if #filtered > 0 then
110-
local json_str = table.concat(filtered, "")
111-
local ok, stats = pcall(vim.json.decode, json_str)
112-
if ok and stats then
113-
callback(stats)
114-
end
107+
local msg = table.concat(data, "\n"):gsub("^%s+", ""):gsub("%s+$", "")
108+
if msg ~= "" then
109+
vim.notify("CodeMe API error: " .. msg, vim.log.levels.WARN)
115110
end
116111
end
117112
end,
113+
on_exit = function(_, exit_code)
114+
if exit_code ~= 0 then
115+
vim.notify(string.format("CodeMe API exited with code %d", exit_code), vim.log.levels.ERROR)
116+
return
117+
end
118+
119+
local json_str = table.concat(output, "\n")
120+
-- Strip anything before first `{`
121+
json_str = json_str:match("({.*})")
122+
if not json_str then
123+
vim.notify("CodeMe: No JSON found in output", vim.log.levels.ERROR)
124+
return
125+
end
126+
127+
local ok, stats = pcall(vim.json.decode, json_str)
128+
if ok then
129+
callback(stats)
130+
else
131+
vim.notify(
132+
string.format("CodeMe: Failed to parse stats JSON\nFirst 200 chars: %s", json_str:sub(1, 200)),
133+
vim.log.levels.ERROR
134+
)
135+
end
136+
end,
118137
})
119138
end
120139

lua/codeme/profile/helpers.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,31 @@ function M.format_items_list(items, key, max_items)
273273
return M.format_list(extracted, max_items)
274274
end
275275

276+
function M.get_top_from_session_list(list, max)
277+
if not list or #list == 0 then
278+
return ""
279+
end
280+
281+
max = max or #list
282+
local result = {}
283+
284+
for i = 1, math.min(max, #list) do
285+
result[#result + 1] = list[i]
286+
end
287+
288+
if #list > max then
289+
result[#result + 1] = "..."
290+
end
291+
292+
return table.concat(result, ", ")
293+
end
294+
295+
function M.get_top_projects(session, max)
296+
return M.get_top_from_session_list(session.projects, max or 3)
297+
end
298+
299+
function M.get_top_languages(session, max)
300+
return M.get_top_from_session_list(session.languages, max or 5)
301+
end
302+
276303
return M

lua/codeme/profile/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local api = vim.api
66
function M.open(stats)
77
state.set_stats(stats)
88
state.tab = 1
9-
state.width = math.min(110, math.floor(vim.o.columns * 0.85))
9+
state.width = math.min(130, math.floor(vim.o.columns * 0.9))
1010
state.ns = api.nvim_create_namespace("codeme")
1111

1212
-- Calculate height by rendering all tabs once

0 commit comments

Comments
 (0)