Skip to content

Commit 791a3c6

Browse files
committed
fix: fix calculate lines changes
1 parent 83af56f commit 791a3c6

File tree

5 files changed

+61
-71
lines changed

5 files changed

+61
-71
lines changed

lua/codeme/profile/helpers.lua

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ 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)
276+
function M.top_items(list, max)
277277
if not list or #list == 0 then
278278
return ""
279279
end
@@ -285,19 +285,13 @@ function M.get_top_from_session_list(list, max)
285285
result[#result + 1] = list[i]
286286
end
287287

288+
-- If there are more items, show "• x more" in subtle gray
288289
if #list > max then
289-
result[#result + 1] = "..."
290+
local remaining = #list - max
291+
result[#result + 1] = string.format(" +%d more", remaining)
290292
end
291293

292294
return table.concat(result, ", ")
293295
end
294296

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-
303297
return M

lua/codeme/profile/tabs/activity.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ function M.render()
136136
duration_str = duration_str .. ""
137137
end
138138

139-
local projects = helpers.get_top_projects(session, 2)
140-
local languages = helpers.get_top_languages(session, 5)
139+
local projects = helpers.top_items(session.projects, 2)
140+
local languages = helpers.top_items(session.languages, 5)
141141

142142
tbl[#tbl + 1] = {
143143
time_str,

lua/codeme/profile/tabs/dashboard.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function M.render()
5555
table.insert(lines, {})
5656

5757
-- TODAY AT A GLANCE
58-
local focus_str = focus_score > 0 and tostring(focus_score) .. "/100" or "N/A"
58+
local focus_str = focus_score > 0 and tostring(focus_score) .. "/100" or ""
5959

6060
-- Create metrics table
6161
local metrics_tbl = {

lua/codeme/profile/tabs/weekly.lua

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local state = require("codeme.profile.state")
22
local fmt = require("codeme.profile.formatters")
33
local ui = require("codeme.ui")
4+
local helpers = require("codeme.profile.helpers")
45

56
local M = {}
67

@@ -45,11 +46,11 @@ function M.get_weekday_date(weekday, monday)
4546
end
4647

4748
function M.render()
48-
local s = state.stats
49+
local globalStats = state.stats
4950
local lines = {}
5051

51-
local this_week = s.this_week or {}
52-
local last_week = s.last_week or {}
52+
local this_week = globalStats.this_week or {}
53+
local last_week = globalStats.last_week or {}
5354

5455
local week_time = this_week.total_time or 0
5556
local last_week_time = last_week.total_time or 0
@@ -72,7 +73,7 @@ function M.render()
7273
-- Calculate week boundaries
7374
local week_monday, week_sunday, today = M.get_week_boundaries()
7475

75-
local daily_activity = s.daily_activity or {}
76+
local daily_activity = globalStats.daily_activity or {}
7677

7778
table.insert(lines, { { " 📊 Daily Breakdown (Mon-Sun)", "exgreen" } })
7879
table.insert(lines, {})
@@ -93,32 +94,35 @@ function M.render()
9394
day_stat = { time = 0, lines = 0, files = 0 }
9495
end
9596

96-
local session_count = this_week.session_count
97+
local session_count = day_stat.session_count or 0
98+
local time_value = day_stat.time or 0
99+
local lines_value = day_stat.lines or 0
100+
local has_activity = time_value > 0 or lines_value > 0 or session_count > 0
97101

98-
if day_stat.time > max_day_time then
99-
max_day_time = day_stat.time
102+
if time_value > max_day_time then
103+
max_day_time = time_value
100104
max_day_data = {
101105
day_name = day_name,
102106
date = expected_date,
103-
time = day_stat.time,
104-
lines = day_stat.lines or 0,
107+
time = time_value,
108+
lines = lines_value,
105109
sessions = session_count,
106110
}
107111
end
108112

109113
local day_label = day_name:sub(1, 3)
110-
local date_label = expected_date:sub(6, 10) -- MM-DD format
111-
local time_str = fmt.fmt_time(day_stat.time)
112-
local lines_str = fmt.fmt_num(day_stat.lines or 0)
113-
local sessions_str = tostring(session_count)
114-
local trend_str = day_stat.time > 0 and "" or "-"
114+
local date_str = tostring(expected_date)
115+
local date_label = date_str:sub(6, 10) -- MM-DD
116+
117+
local time_str = has_activity and fmt.fmt_time(time_value) or "-"
118+
local lines_str = has_activity and fmt.fmt_num(lines_value) or "-"
119+
local sessions_str = has_activity and tostring(session_count) or "-"
120+
local trend_str = time_value > 0 and "" or "-"
115121

116-
-- Mark today with a star
117122
if expected_date == today then
118123
day_label = day_label .. ""
119124
end
120125

121-
-- Show "-" for future dates
122126
if expected_date > today then
123127
time_str = "-"
124128
lines_str = "-"
@@ -146,48 +150,45 @@ function M.render()
146150
table.insert(lines, { { " 📌 Week Summary", "exgreen" } })
147151
table.insert(lines, {})
148152

149-
-- ✅ Most Productive Day (from backend records)
150-
local records = s.records or {}
151-
local most_productive_day = records.most_productive_day or {}
153+
local most_productive_day = globalStats.this_week.most_productive_day or {}
152154

153155
if most_productive_day.time and most_productive_day.time > 0 then
154156
table.insert(lines, {
155157
{ " • Most Productive: ", "commentfg" },
156-
{ most_productive_day.weekday or "Unknown", "exgreen" },
158+
{ most_productive_day.weekday or "", "exgreen" },
157159
{ ", " .. fmt.fmt_date_full(most_productive_day.date or ""), "commentfg" },
158160
{ string.format(" (%s)", fmt.fmt_time(most_productive_day.time)), "exyellow" },
159161
})
160162

161163
-- Session details
162-
if most_productive_day.sessions and most_productive_day.sessions > 0 then
164+
if most_productive_day.session_count and most_productive_day.session_count > 0 then
163165
table.insert(lines, {
164166
{ " ↳ Sessions: ", "commentfg" },
165-
{ tostring(most_productive_day.sessions), "exgreen" },
167+
{ tostring(most_productive_day.session_count), "exgreen" },
166168
{ string.format(", %s lines", fmt.fmt_num(most_productive_day.lines or 0)), "commentfg" },
167169
})
168170
end
169171

170-
-- Top languages
171-
if most_productive_day.languages and #most_productive_day.languages > 0 then
172-
local languages_result = table.concat(most_productive_day.languages, ", ")
173-
table.insert(lines, {
174-
{ " ↳ Languages: ", "commentfg" },
175-
{ languages_result, "exgreen" },
176-
})
177-
end
178-
179172
-- Main projects
180173
if most_productive_day.projects and #most_productive_day.projects > 0 then
181-
local projects_result = table.concat(most_productive_day.projects, ", ")
174+
local projects_result = helpers.top_items(most_productive_day.projects, 3)
182175
table.insert(lines, {
183176
{ " ↳ Projects: ", "commentfg" },
184177
{ projects_result, "exyellow" },
185178
})
186179
end
187180

181+
-- Top languages
182+
if most_productive_day.languages and #most_productive_day.languages > 0 then
183+
local languages_result = helpers.top_items(most_productive_day.languages, 5)
184+
table.insert(lines, {
185+
{ " ↳ Languages: ", "commentfg" },
186+
{ languages_result, "exgreen" },
187+
})
188+
end
189+
188190
table.insert(lines, {})
189191
elseif max_day_data and max_day_time > 0 then
190-
-- Fallback to calculated data
191192
table.insert(lines, {
192193
{ " • Most Productive: ", "commentfg" },
193194
{ max_day_data.day_name, "exgreen" },
@@ -287,7 +288,7 @@ function M.render()
287288
end
288289

289290
-- ✅ Productivity Trend (from backend)
290-
local productivity_trend = s.productivity_trend or ""
291+
local productivity_trend = globalStats.productivity_trend or ""
291292
if productivity_trend ~= "" then
292293
local trend_text
293294
local trend_color
@@ -312,7 +313,7 @@ function M.render()
312313
table.insert(lines, {})
313314

314315
-- ✅ Heatmap (from backend weekly_heatmap)
315-
local hm = s.weekly_heatmap
316+
local hm = globalStats.weekly_heatmap
316317
if hm and #hm > 0 then
317318
table.insert(lines, { { " 📅 Activity Heatmap", "exgreen" } })
318319
table.insert(lines, {})

lua/codeme/tracker.lua

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@ end
6161

6262
local function send_to_backend(filepath, lines_changed, opts)
6363
opts = opts or {}
64-
6564
local bufnr = vim.fn.bufnr(filepath)
6665
if bufnr == -1 or not vim.api.nvim_buf_is_valid(bufnr) then
6766
return
6867
end
6968

70-
local language = vim.bo[bufnr].filetype ~= "" and vim.bo[bufnr].filetype or "n/a"
69+
local language = vim.bo[bufnr].filetype ~= "" and vim.bo[bufnr].filetype or ""
7170

7271
local cmd = {
7372
opts.codeme_bin or "codeme",
@@ -86,7 +85,7 @@ local function send_to_backend(filepath, lines_changed, opts)
8685
detach = true,
8786
on_exit = function(_, code)
8887
if opts.verbose then
89-
local heartbeat_type = opts.heartbeat_type or "n/a"
88+
local heartbeat_type = opts.heartbeat_type or ""
9089
if code == 0 then
9190
vim.notify(
9291
string.format(
@@ -114,41 +113,38 @@ local function calculate_lines_changed(filepath)
114113
if current_diff ~= nil then
115114
-- GIT-TRACKED FILE
116115
local last_diff = last_git_diff_lines[filepath] or 0
117-
local delta = current_diff - last_diff
118116

119-
-- Update baseline
120-
last_git_diff_lines[filepath] = current_diff
121-
122-
if delta > 0 then
123-
return delta, "save" -- productivity signal
117+
if current_diff == 0 and last_diff > 0 then
118+
last_git_diff_lines[filepath] = 0
119+
return 0, "reset"
124120
end
125121

126-
-- File was reset (commit, checkout, stash, etc.)
127-
-- Still send heartbeat with 0 lines to preserve time tracking
128-
if current_diff == 0 and last_diff > 0 then
129-
return 0, "save_reset" -- still counts as time spent
122+
local delta = current_diff - last_diff
123+
last_git_diff_lines[filepath] = current_diff
124+
125+
if delta ~= 0 then
126+
-- Lines changed in git diff
127+
-- Count both additions and deletions as work
128+
return math.abs(delta), delta > 0 and "add" or "delete"
130129
end
131130

132-
-- No new changes since last save (delta <= 0)
133-
-- Don't send redundant heartbeat
134-
return nil, nil
131+
-- Git diff unchanged, but still editing/refactoring
132+
-- This catches: refactors, formatting, renames, navigation
133+
-- Send minimal signal to prevent "always 0" problem
134+
return 1, "edit_idle"
135135
else
136136
-- NON-GIT-TRACKED FILE (with NON_GIT_COOLDOWN throttling)
137137
local now = os.time()
138138
local last_save = last_non_git_save_time[filepath]
139139

140140
if not last_save then
141-
-- First save of this non-git file
142141
last_non_git_save_time[filepath] = now
143-
return 0, "save_new" -- new file, track time only
142+
return 0, "new_file"
144143
elseif (now - last_save) < NON_GIT_COOLDOWN then
145-
-- Too soon since last save, skip to prevent spam
146-
-- This implements the NON_GIT_COOLDOWN throttling
147144
return nil, nil
148145
else
149-
-- Enough time passed, send heartbeat and reset timer
150146
last_non_git_save_time[filepath] = now
151-
return 0, "save_untracked" -- untracked file, time only
147+
return 0, "untracked"
152148
end
153149
end
154150
end
@@ -237,7 +233,6 @@ vim.api.nvim_create_autocmd("BufEnter", {
237233
end,
238234
})
239235

240-
--- Wire into BufWritePost: file saved
241236
vim.api.nvim_create_autocmd("BufWritePost", {
242237
group = augroup,
243238
callback = function()

0 commit comments

Comments
 (0)