generated from nvimdev/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimetrack-cli.lua
More file actions
314 lines (276 loc) · 9.68 KB
/
timetrack-cli.lua
File metadata and controls
314 lines (276 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env lua
-- timeTrack CLI - Standalone command-line interface for timeTrack.nvim
-- Usage: lua timetrack-cli.lua <command> [options]
-- Ensure we can find the modules
local script_dir = debug.getinfo(1, 'S').source:match('@(.*/)')
if script_dir then
package.path = script_dir .. '?.lua;' .. script_dir .. '?/init.lua;' .. package.path
else
-- Fallback: assume we're in the timeTrack.nvim root directory
package.path = './?.lua;./?/init.lua;' .. package.path
end
local cli_core = require('cli.core')
-- Helper function to print usage
local function print_usage()
print([[
timeTrack CLI - Time tracking without Neovim
Usage: lua timetrack-cli.lua <command> [options]
Commands:
status Show current time tracking status
summary [week] [year] Show weekly summary (defaults to current week)
add <project> <file> <hours> [weekday] Add manual time entry
list [week] [year] List all time entries for a week
validate Validate time data for issues
export <format> [week] [year] Export data (csv, markdown)
help Show this help message
Examples:
lua timetrack-cli.lua status
lua timetrack-cli.lua summary
lua timetrack-cli.lua summary 25 2024
lua timetrack-cli.lua add "MyProject" "main.lua" 2.5
lua timetrack-cli.lua add "MyProject" "main.lua" 2.5 "Monday"
lua timetrack-cli.lua list
lua timetrack-cli.lua export csv
lua timetrack-cli.lua validate
]])
end
-- Helper function to format hours
local function format_hours(hours)
return string.format('%.1fh', hours)
end
-- Helper function to print weekly summary
local function print_summary(summary, year, week)
print(
string.format(
'\n📊 Weekly Summary - Year %s, Week %s',
year or os.date('%Y'),
week or os.date('%W')
)
)
print(string.rep('=', 50))
local total_week_hours = 0
local weekdays =
{ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' }
for _, weekday in ipairs(weekdays) do
if summary[weekday] then
print(
string.format('\n📅 %s - %s', weekday, format_hours(summary[weekday].total_hours))
)
total_week_hours = total_week_hours + summary[weekday].total_hours
for project, files in pairs(summary[weekday].projects) do
local project_hours = 0
for file, hours in pairs(files) do
project_hours = project_hours + hours
end
print(string.format(' 📁 %s: %s', project, format_hours(project_hours)))
for file, hours in pairs(files) do
if hours > 0 then
print(string.format(' 📄 %s: %s', file, format_hours(hours)))
end
end
end
end
end
print(string.format('\n🏁 Total Week: %s', format_hours(total_week_hours)))
end
-- Helper function to print validation results
local function print_validation_results(results)
print('\n🔍 Data Validation Results')
print(string.rep('=', 30))
if results.summary.total_issues == 0 then
print('✅ No issues found!')
return
end
print(string.format('Found %d issues:', results.summary.total_issues))
if results.summary.total_overlaps > 0 then
print(string.format(' ⚠️ %d overlapping entries', results.summary.total_overlaps))
end
if results.summary.total_duplicates > 0 then
print(string.format(' ⚠️ %d duplicate entries', results.summary.total_duplicates))
end
if results.summary.total_errors > 0 then
print(string.format(' ❌ %d erroneous entries', results.summary.total_errors))
end
-- Show detailed issues
for weekday, issues in pairs(results.issues) do
if #issues > 0 then
print(string.format('\n📅 %s:', weekday))
for _, issue in ipairs(issues) do
print(
string.format(
' • %s/%s: %s',
issue.project,
issue.file,
table.concat(issue.issues, ', ')
)
)
end
end
end
end
-- Parse command line arguments
local function parse_args(args)
local command = args[1]
local parsed = { command = command }
if command == 'summary' or command == 'list' then
parsed.week = args[2]
parsed.year = args[3]
elseif command == 'add' then
parsed.project = args[2]
parsed.file = args[3]
parsed.hours = tonumber(args[4])
parsed.weekday = args[5]
elseif command == 'export' then
parsed.format = args[2]
parsed.week = args[3]
parsed.year = args[4]
end
return parsed
end
-- Main CLI function
local function main(args)
local parsed = parse_args(args)
local command = parsed.command
if not command or command == 'help' then
print_usage()
return
end
-- Handle different commands
if command == 'status' then
local status = cli_core.get_status()
print('\n📊 timeTrack Status')
print(string.rep('=', 20))
print(string.format('Data file: %s', status.data_file))
print(
string.format(
'Current: %s, Week %s (%s)',
status.current_year,
status.current_week,
status.current_weekday
)
)
print(string.format('Paused: %s', status.paused and 'Yes' or 'No'))
print('\n⏰ Hours per weekday:')
for weekday, hours in pairs(status.hours_per_weekday) do
print(string.format(' %s: %s', weekday, format_hours(hours)))
end
-- Show current week summary if there's data
local has_data = false
for _, data in pairs(status.current_week_summary) do
if data.total_hours > 0 then
has_data = true
break
end
end
if has_data then
print_summary(status.current_week_summary, status.current_year, status.current_week)
else
print(
string.format(
'\n📈 No time entries found for current week (%s/%s)',
status.current_week,
status.current_year
)
)
end
elseif command == 'summary' then
local summary = cli_core.get_weekly_summary({
week = parsed.week,
year = parsed.year,
})
print_summary(summary, parsed.year, parsed.week)
elseif command == 'add' then
if not parsed.project or not parsed.file or not parsed.hours then
print('❌ Error: project, file, and hours are required')
print('Usage: lua timetrack-cli.lua add <project> <file> <hours> [weekday]')
return 1
end
local success, err = pcall(cli_core.add_time_entry, {
project = parsed.project,
file = parsed.file,
hours = parsed.hours,
weekday = parsed.weekday,
})
if success then
local weekday = parsed.weekday or os.date('%A')
print(
string.format(
'✅ Added %s to %s/%s for %s',
format_hours(parsed.hours),
parsed.project,
parsed.file,
weekday
)
)
else
print(string.format('❌ Error adding time entry: %s', err))
return 1
end
elseif command == 'list' then
local entries = cli_core.list_entries({
week = parsed.week,
year = parsed.year,
})
print(
string.format(
'\n📋 Time Entries - Year %s, Week %s',
parsed.year or os.date('%Y'),
parsed.week or os.date('%W')
)
)
print(string.rep('=', 50))
if #entries == 0 then
print('No time entries found.')
else
for _, entry in ipairs(entries) do
print(
string.format(
'%s | %s/%s | %s | %s-%s (%s)',
entry.weekday,
entry.project,
entry.file,
format_hours(entry.diffInHours or 0),
entry.startReadable,
entry.endReadable,
format_hours((entry.endTime - entry.startTime) / 3600)
)
)
end
end
elseif command == 'validate' then
local results = cli_core.validate_data()
print_validation_results(results)
elseif command == 'export' then
if not parsed.format then
print('❌ Error: format is required')
print('Usage: lua timetrack-cli.lua export <format> [week] [year]')
print('Formats: csv, markdown')
return 1
end
local export_data = cli_core.export_data({
format = parsed.format,
week = parsed.week,
year = parsed.year,
})
if export_data then
print(export_data)
else
print('❌ Error: Failed to export data')
return 1
end
else
print(string.format('❌ Unknown command: %s', command))
print_usage()
return 1
end
return 0
end
-- Run the CLI if this script is executed directly
if arg then
local exit_code = main(arg)
if exit_code and exit_code ~= 0 then
os.exit(exit_code)
end
end
-- Return the main function for testing
return main