|
93 | 93 |
|
94 | 94 | --- Get stats from backend |
95 | 95 | function M.get_stats(callback) |
96 | | - local cmd = M.config.codeme_bin .. " api --compress" |
| 96 | + local cmd = M.config.codeme_bin .. " api --compact" |
97 | 97 |
|
98 | 98 | vim.fn.jobstart(cmd, { |
99 | 99 | stdout_buffered = true, |
100 | 100 | on_stdout = function(_, data) |
| 101 | + if not data or #data == 0 then |
| 102 | + return |
| 103 | + end |
| 104 | + |
| 105 | + local json_str = table.concat(data, "\n") |
| 106 | + |
| 107 | + -- Remove empty lines |
| 108 | + json_str = json_str:gsub("\n\n+", "\n") |
| 109 | + |
| 110 | + -- Try to parse |
| 111 | + local ok, stats = pcall(vim.json.decode, json_str) |
| 112 | + |
| 113 | + if ok and stats then |
| 114 | + callback(stats) |
| 115 | + else |
| 116 | + -- Debug: show what we got |
| 117 | + vim.notify( |
| 118 | + string.format("CodeMe: Failed to parse stats JSON\nFirst 200 chars: %s", json_str:sub(1, 200)), |
| 119 | + vim.log.levels.ERROR |
| 120 | + ) |
| 121 | + end |
| 122 | + end, |
| 123 | + on_stderr = function(_, data) |
101 | 124 | if data and #data > 0 then |
102 | | - local filtered = vim.tbl_filter(function(line) |
103 | | - return line ~= "" and (line:match("^%s*{") or line:match("[,}%]]%s*$")) |
104 | | - end, data) |
105 | | - |
106 | | - if #filtered > 0 then |
107 | | - local json_str = table.concat(filtered, "") |
108 | | - local ok, stats = pcall(vim.json.decode, json_str) |
109 | | - if ok and stats then |
110 | | - callback(stats) |
111 | | - end |
| 125 | + local msg = table.concat(data, "\n"):gsub("^%s+", ""):gsub("%s+$", "") |
| 126 | + if msg ~= "" then |
| 127 | + vim.notify("CodeMe API error: " .. msg, vim.log.levels.WARN) |
112 | 128 | end |
113 | 129 | end |
114 | 130 | end, |
| 131 | + on_exit = function(_, exit_code) |
| 132 | + if exit_code ~= 0 then |
| 133 | + vim.notify(string.format("CodeMe API exited with code %d", exit_code), vim.log.levels.ERROR) |
| 134 | + end |
| 135 | + end, |
115 | 136 | }) |
116 | 137 | end |
117 | 138 |
|
|
0 commit comments