-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1128 lines (966 loc) · 26.3 KB
/
Copy pathmain.go
File metadata and controls
1128 lines (966 loc) · 26.3 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// Styles
var (
titleStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#7D56F4")).
Padding(0, 1).
Bold(true)
subtitleStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#7D56F4")).
Bold(true)
infoStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#626262")).
Italic(true)
errorStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FF5F87")).
Bold(true)
successStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#50FA7B")).
Bold(true)
workStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#8BE9FD"))
breakStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFB86C"))
ignoredStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#6272A4"))
currentActivityStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#50FA7B")).
Bold(true)
helpStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#626262"))
docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)
)
// Data structures
type ActivityType int
const (
Work ActivityType = iota
Break
Ignored
)
func (a ActivityType) String() string {
switch a {
case Work:
return "WORK"
case Break:
return "BREAK"
case Ignored:
return "IGNORED"
default:
return "UNKNOWN"
}
}
type Entry struct {
Timestamp time.Time `json:"timestamp"`
Name string `json:"name"`
Comment string `json:"comment,omitempty"`
}
type Activity struct {
Name string
Start time.Time
End time.Time
Duration time.Duration
Type ActivityType
Project string
Task string
Comment string
IsCurrent bool
}
type Config struct {
DataFile string `json:"data_file"`
Editor string `json:"editor"`
}
type TimeTracker struct {
entries []Entry
config Config
}
// Views
type viewType int
const (
mainView viewType = iota
addTaskView
reportView
helpView
)
// Key mappings
type keyMap struct {
Up key.Binding
Down key.Binding
Left key.Binding
Right key.Binding
Help key.Binding
Quit key.Binding
Enter key.Binding
Back key.Binding
AddTask key.Binding
Report key.Binding
Hello key.Binding
Stretch key.Binding
}
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Help, k.Quit}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Up, k.Down, k.Left, k.Right},
{k.AddTask, k.Report, k.Hello, k.Stretch},
{k.Enter, k.Back, k.Help, k.Quit},
}
}
var keys = keyMap{
Up: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "move down"),
),
Left: key.NewBinding(
key.WithKeys("left", "h"),
key.WithHelp("←/h", "move left"),
),
Right: key.NewBinding(
key.WithKeys("right", "l"),
key.WithHelp("→/l", "move right"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select"),
),
Back: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "back"),
),
AddTask: key.NewBinding(
key.WithKeys("a"),
key.WithHelp("a", "complete task"),
),
Report: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "view report"),
),
Hello: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "start day"),
),
Stretch: key.NewBinding(
key.WithKeys("x"),
key.WithHelp("x", "extend last task"),
),
}
// Model
type model struct {
tracker *TimeTracker
currentView viewType
width int
height int
ready bool
// Components
help help.Model
taskInput textinput.Model
viewport viewport.Model
table table.Model
// State
message string
messageType string // "error", "success", "info"
// Add task form
taskName string
taskComment string
inputMode int // 0 = name, 1 = comment
}
func initialModel() model {
tracker := &TimeTracker{}
tracker.loadConfig()
tracker.loadEntries()
// Initialize task input
ti := textinput.New()
ti.Placeholder = "Enter task name (e.g., 'Education: CKA Labs' or 'Lunch **')"
ti.Focus()
ti.CharLimit = 156
ti.Width = 50
// Initialize help
h := help.New()
h.Width = 50
// Initialize viewport
vp := viewport.New(78, 20)
vp.Style = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
PaddingRight(2)
// Initialize table
columns := []table.Column{
{Title: "Time", Width: 10},
{Title: "Duration", Width: 12},
{Title: "Activity", Width: 40},
{Title: "Type", Width: 8},
}
t := table.New(
table.WithColumns(columns),
table.WithFocused(true),
table.WithHeight(15),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)
return model{
tracker: tracker,
currentView: mainView,
help: h,
taskInput: ti,
viewport: vp,
table: t,
inputMode: 0,
}
}
func (m model) Init() tea.Cmd {
return tea.EnterAltScreen
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
m.viewport.Width = msg.Width - 4
m.viewport.Height = msg.Height - 10
m.help.Width = msg.Width
m.ready = true
case tea.KeyMsg:
switch m.currentView {
case mainView:
return m.updateMainView(msg)
case addTaskView:
return m.updateAddTaskView(msg)
case reportView:
return m.updateReportView(msg)
case helpView:
return m.updateHelpView(msg)
}
}
// Only update components that aren't being actively used for input
if m.currentView != addTaskView {
m.taskInput, cmd = m.taskInput.Update(msg)
cmds = append(cmds, cmd)
}
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
m.table, cmd = m.table.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) updateMainView(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, keys.Quit):
return m, tea.Quit
case key.Matches(msg, keys.AddTask):
m.currentView = addTaskView
m.taskInput.SetValue("")
m.taskInput.Focus()
m.inputMode = 0
m.message = ""
m.messageType = ""
case key.Matches(msg, keys.Report):
m.currentView = reportView
m.updateReportData()
case key.Matches(msg, keys.Hello):
m.tracker.addStart()
m.message = "Day started!"
m.messageType = "success"
case key.Matches(msg, keys.Stretch):
err := m.tracker.extend()
if err != nil {
m.message = fmt.Sprintf("Error: %v", err)
m.messageType = "error"
} else {
m.message = "Task extended to current time!"
m.messageType = "success"
}
case key.Matches(msg, keys.Help):
m.currentView = helpView
}
return m, nil
}
func (m model) updateAddTaskView(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch {
case key.Matches(msg, keys.Back):
m.currentView = mainView
m.taskInput.Blur()
m.message = ""
return m, nil
case key.Matches(msg, keys.Enter):
if m.inputMode == 0 {
// Save task name and move to comment
m.taskName = m.taskInput.Value()
if m.taskName == "" {
m.message = "Task name cannot be empty"
m.messageType = "error"
return m, nil
}
m.inputMode = 1
m.taskInput.SetValue("")
m.taskInput.Placeholder = "Optional comment (press Enter to skip)"
m.taskInput.Focus()
} else {
// Save comment and add task
m.taskComment = m.taskInput.Value()
entry := Entry{
Timestamp: time.Now(),
Name: m.taskName,
Comment: m.taskComment,
}
err := m.tracker.addEntry(entry)
if err != nil {
m.message = fmt.Sprintf("Error adding task: %v", err)
m.messageType = "error"
} else {
// Calculate duration from last entry
var durationMsg string
if len(m.tracker.entries) > 1 {
lastEntry := m.tracker.entries[len(m.tracker.entries)-2] // Previous entry before the one we just added
duration := entry.Timestamp.Sub(lastEntry.Timestamp)
durationMsg = fmt.Sprintf(" (%s)", formatDuration(duration))
}
m.message = fmt.Sprintf("Task completed: %s%s", m.taskName, durationMsg)
m.messageType = "success"
m.currentView = mainView
m.taskInput.Blur()
}
// Reset form
m.taskName = ""
m.taskComment = ""
m.inputMode = 0
m.taskInput.Placeholder = "Enter task name (e.g., 'Education: CKA Labs' or 'Lunch **')"
}
return m, nil
default:
// Let the text input handle other keys
m.taskInput, cmd = m.taskInput.Update(msg)
return m, cmd
}
}
func (m model) updateReportView(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, keys.Back):
m.currentView = mainView
case key.Matches(msg, keys.Quit):
return m, tea.Quit
}
return m, nil
}
func (m model) updateHelpView(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, keys.Back), key.Matches(msg, keys.Help):
m.currentView = mainView
case key.Matches(msg, keys.Quit):
return m, tea.Quit
}
return m, nil
}
func (m *model) updateReportData() {
activities := m.tracker.getTodaysActivities()
rows := []table.Row{}
for _, activity := range activities {
timeStr := activity.Start.Format("15:04") + "-" + activity.End.Format("15:04")
durationStr := formatDuration(activity.Duration)
activityName := activity.Name
rows = append(rows, table.Row{
timeStr,
durationStr,
activityName,
activity.Type.String(),
})
}
m.table.SetRows(rows)
// Generate summary for viewport
summary := m.tracker.generateTodaysSummary()
m.viewport.SetContent(summary)
}
func (m model) View() string {
if !m.ready {
return "\n Initializing..."
}
switch m.currentView {
case mainView:
return m.mainViewRender()
case addTaskView:
return m.addTaskViewRender()
case reportView:
return m.reportViewRender()
case helpView:
return m.helpViewRender()
default:
return "Unknown view"
}
}
func (m model) mainViewRender() string {
title := titleStyle.Render("⏱️ Time Tracker")
// Current status
status := m.tracker.getCurrentStatus()
// Recent activities (last 5)
recentActivities := m.tracker.getRecentActivities(5)
var recent strings.Builder
recent.WriteString(subtitleStyle.Render("Recent Activities:") + "\n\n")
if len(recentActivities) == 0 {
recent.WriteString(infoStyle.Render("No activities yet. Press 's' to start your day or 'a' to complete a task."))
} else {
for _, activity := range recentActivities {
timeStr := activity.Start.Format("15:04") + "-" + activity.End.Format("15:04")
durationStr := formatDuration(activity.Duration)
var style lipgloss.Style
switch activity.Type {
case Work:
style = workStyle
case Break:
style = breakStyle
case Ignored:
style = ignoredStyle
}
// Use a simple, consistent format
line := fmt.Sprintf(" %s %s %s", timeStr, durationStr, activity.Name)
recent.WriteString(style.Render(line) + "\n")
}
}
// Quick stats
stats := m.tracker.getTodaysStats()
quickStats := fmt.Sprintf("\n%s\n%s\n%s\n%s",
subtitleStyle.Render("Today's Summary:"),
workStyle.Render(fmt.Sprintf(" Work: %s", formatDuration(stats.WorkTime))),
breakStyle.Render(fmt.Sprintf(" Break: %s", formatDuration(stats.BreakTime))),
subtitleStyle.Render(fmt.Sprintf(" Total: %s", formatDuration(stats.TotalTime))))
// Project breakdown for main view
projects := m.tracker.getTodaysProjects()
// Debug: Always show the projects section to see what's in it
quickStats += "\n\n" + subtitleStyle.Render("Projects:")
if len(projects) == 0 {
quickStats += "\n" + infoStyle.Render(" No projects found")
} else {
for project, duration := range projects {
if project == "" {
project = "General"
}
quickStats += "\n" + workStyle.Render(fmt.Sprintf(" %s: %s", project, formatDuration(duration)))
}
}
// Message
var message string
if m.message != "" {
switch m.messageType {
case "error":
message = "\n" + errorStyle.Render("• "+m.message)
case "success":
message = "\n" + successStyle.Render("• "+m.message)
default:
message = "\n" + infoStyle.Render("• "+m.message)
}
}
// Help
helpView := "\n" + helpStyle.Render("Press ? for help, q to quit")
content := lipgloss.JoinVertical(lipgloss.Left,
title,
"",
status,
"",
recent.String(),
quickStats,
message,
helpView,
)
return docStyle.Render(content)
}
func (m model) addTaskViewRender() string {
title := titleStyle.Render("✅ Task Completed")
var prompt string
if m.inputMode == 0 {
prompt = subtitleStyle.Render("What task did you just finish?")
prompt += "\n" + infoStyle.Render("Examples: 'Meeting: Standup', 'Lunch **', 'Commuting ***'")
// Show duration since last activity
if len(m.tracker.entries) > 0 {
lastEntry := m.tracker.entries[len(m.tracker.entries)-1]
duration := time.Since(lastEntry.Timestamp)
prompt += "\n" + workStyle.Render(fmt.Sprintf("Duration: %s (since %s)",
formatDuration(duration), lastEntry.Timestamp.Format("15:04")))
}
} else {
prompt = subtitleStyle.Render("Comment (optional):")
prompt += "\n" + infoStyle.Render("Task: ") + workStyle.Render(m.taskName)
// Show the duration this task will have
if len(m.tracker.entries) > 0 {
lastEntry := m.tracker.entries[len(m.tracker.entries)-1]
duration := time.Since(lastEntry.Timestamp)
prompt += "\n" + workStyle.Render(fmt.Sprintf("This task took: %s", formatDuration(duration)))
}
}
input := m.taskInput.View()
var message string
if m.message != "" {
switch m.messageType {
case "error":
message = errorStyle.Render("• " + m.message)
default:
message = infoStyle.Render("• " + m.message)
}
}
help := helpStyle.Render("Enter to continue • Esc to cancel")
content := lipgloss.JoinVertical(lipgloss.Left,
title,
"",
prompt,
"",
input,
"",
message,
"",
help,
)
return docStyle.Render(content)
}
func (m model) reportViewRender() string {
title := titleStyle.Render("📊 Today's Report")
// Summary in viewport
summary := m.viewport.View()
// Activities table
table := m.table.View()
help := helpStyle.Render("Esc to go back • q to quit")
content := lipgloss.JoinVertical(lipgloss.Left,
title,
"",
summary,
"",
subtitleStyle.Render("Activities:"),
"",
table,
"",
help,
)
return docStyle.Render(content)
}
func (m model) helpViewRender() string {
title := titleStyle.Render("❓ Help")
helpContent := `
` + subtitleStyle.Render("Navigation:") + `
↑/k, ↓/j Move up/down
←/h, →/l Move left/right
Enter Select/confirm
Esc Go back
q Quit
` + subtitleStyle.Render("Actions:") + `
s Start day
a Complete task (add finished task)
r View today's report
x Extend last task to now
? Toggle this help
` + subtitleStyle.Render("Task Types:") + `
Regular task "Meeting: Standup"
Break task (**) "Lunch **"
Ignored task (***) "Commuting ***"
` + subtitleStyle.Render("Project Format:") + `
Use "Project: Task" to categorize activities
Examples: "Education: CKA Labs", "Sprint-2: Bug fix"
`
back := helpStyle.Render("Press ? or Esc to go back")
content := lipgloss.JoinVertical(lipgloss.Left,
title,
helpContent,
"",
back,
)
return docStyle.Render(content)
}
// TimeTracker methods
func (tt *TimeTracker) loadConfig() {
homeDir, _ := os.UserHomeDir()
configDir := filepath.Join(homeDir, ".config", "timetracker")
configFile := filepath.Join(configDir, "config.json")
// Default config
tt.config = Config{
DataFile: filepath.Join(configDir, "entries.json"),
Editor: "vi",
}
// Try to load existing config
if data, err := os.ReadFile(configFile); err == nil {
json.Unmarshal(data, &tt.config)
} else {
// Create config directory and save default config
os.MkdirAll(configDir, 0755)
data, _ := json.MarshalIndent(tt.config, "", " ")
os.WriteFile(configFile, data, 0644)
}
}
func (tt *TimeTracker) loadEntries() {
if data, err := os.ReadFile(tt.config.DataFile); err == nil {
json.Unmarshal(data, &tt.entries)
}
// Sort entries by timestamp
sort.Slice(tt.entries, func(i, j int) bool {
return tt.entries[i].Timestamp.Before(tt.entries[j].Timestamp)
})
}
func (tt *TimeTracker) saveEntries() error {
// Ensure directory exists
dir := filepath.Dir(tt.config.DataFile)
os.MkdirAll(dir, 0755)
data, err := json.MarshalIndent(tt.entries, "", " ")
if err != nil {
return err
}
return os.WriteFile(tt.config.DataFile, data, 0644)
}
func (tt *TimeTracker) addEntry(entry Entry) error {
tt.entries = append(tt.entries, entry)
return tt.saveEntries()
}
func (tt *TimeTracker) addStart() error {
entry := Entry{
Timestamp: time.Now(),
Name: "Start",
}
return tt.addEntry(entry)
}
func (tt *TimeTracker) extend() error {
if len(tt.entries) == 0 {
return fmt.Errorf("no entries to extend")
}
lastEntry := tt.entries[len(tt.entries)-1]
if lastEntry.Name == "Start" {
return fmt.Errorf("cannot extend start entry")
}
entry := Entry{
Timestamp: time.Now(),
Name: lastEntry.Name,
Comment: lastEntry.Comment,
}
return tt.addEntry(entry)
}
func (tt *TimeTracker) getCurrentStatus() string {
if len(tt.entries) == 0 {
return infoStyle.Render("No activities yet. Start your day!")
}
lastEntry := tt.entries[len(tt.entries)-1]
duration := time.Since(lastEntry.Timestamp)
if lastEntry.Name == "Start" {
return currentActivityStyle.Render(fmt.Sprintf("Day started (%s ago)",
formatDuration(duration)))
}
return currentActivityStyle.Render(fmt.Sprintf("Latest: %s (%s ago)",
lastEntry.Name, formatDuration(duration)))
}
func (tt *TimeTracker) getRecentActivities(limit int) []Activity {
activities := tt.getTodaysActivities()
if len(activities) > limit {
return activities[len(activities)-limit:]
}
return activities
}
func (tt *TimeTracker) getTodaysActivities() []Activity {
today := time.Now().Truncate(24 * time.Hour)
var todaysEntries []Entry
// Get today's entries
for _, entry := range tt.entries {
if entry.Timestamp.After(today) {
todaysEntries = append(todaysEntries, entry)
}
}
if len(todaysEntries) == 0 {
return []Activity{}
}
var activities []Activity
// Convert entries to activities (each activity represents time between entries)
for i := 0; i < len(todaysEntries); i++ {
entry := todaysEntries[i]
// Skip start entries - they don't represent completed work
if entry.Name == "Start" {
continue
}
// Find the previous entry to calculate duration
var start time.Time
if i == 0 {
// If this is the first entry, we can't calculate duration
continue
} else {
start = todaysEntries[i-1].Timestamp
}
end := entry.Timestamp
activity := parseActivity(entry, start, end, false) // No "current" activities anymore
activities = append(activities, activity)
}
return activities
}
func (tt *TimeTracker) getTodaysStats() struct {
WorkTime time.Duration
BreakTime time.Duration
TotalTime time.Duration
} {
activities := tt.getTodaysActivities()
var workTime, breakTime time.Duration
for _, activity := range activities {
switch activity.Type {
case Work:
workTime += activity.Duration
case Break:
breakTime += activity.Duration
}
}
return struct {
WorkTime time.Duration
BreakTime time.Duration
TotalTime time.Duration
}{
WorkTime: workTime,
BreakTime: breakTime,
TotalTime: workTime + breakTime,
}
}
func (tt *TimeTracker) getTodaysProjects() map[string]time.Duration {
activities := tt.getTodaysActivities()
projects := make(map[string]time.Duration)
for _, activity := range activities {
if activity.Type == Work {
projects[activity.Project] += activity.Duration
}
}
return projects
}
func (tt *TimeTracker) generateTodaysSummary() string {
stats := tt.getTodaysStats()
activities := tt.getTodaysActivities()
var summary strings.Builder
// Time summary
summary.WriteString(subtitleStyle.Render("Time Summary:") + "\n\n")
summary.WriteString(workStyle.Render(fmt.Sprintf(" Work: %s\n", formatDuration(stats.WorkTime))))
summary.WriteString(breakStyle.Render(fmt.Sprintf(" Break: %s\n", formatDuration(stats.BreakTime))))
summary.WriteString(subtitleStyle.Render(fmt.Sprintf(" Total: %s\n\n", formatDuration(stats.TotalTime))))
// Project breakdown
projects := make(map[string]time.Duration)
for _, activity := range activities {
if activity.Type == Work && activity.Project != "" {
projects[activity.Project] += activity.Duration
}
}
if len(projects) > 0 {
summary.WriteString(subtitleStyle.Render("Projects:") + "\n\n")
for project, duration := range projects {
summary.WriteString(workStyle.Render(fmt.Sprintf(" %s: %s\n", project, formatDuration(duration))))
}
}
return summary.String()
}
// Helper functions
func parseActivity(entry Entry, start, end time.Time, isCurrent bool) Activity {
name := entry.Name
activityType := Work
project := ""
task := name
// Determine activity type
if strings.HasSuffix(name, "***") {
activityType = Ignored
name = strings.TrimSuffix(name, " ***")
task = name
} else if strings.HasSuffix(name, "**") {
activityType = Break
name = strings.TrimSuffix(name, " **")
task = name
}
// Parse project:task format
if strings.Contains(name, ":") {
parts := strings.SplitN(name, ":", 2)
if len(parts) == 2 {
project = strings.TrimSpace(parts[0])
task = strings.TrimSpace(parts[1])
}
}
return Activity{
Name: name,
Start: start,
End: end,
Duration: end.Sub(start),
Type: activityType,
Project: project,
Task: task,
Comment: entry.Comment,
IsCurrent: isCurrent,
}
}
func formatDuration(d time.Duration) string {
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
return fmt.Sprintf("%dh%02d", hours, minutes)
}
func printCLIHelp() {
fmt.Println("tt - Time Tracker")
fmt.Println()
fmt.Println("USAGE:")
fmt.Println(" tt Start TUI interface")
fmt.Println(" tt [command] Run command and exit")
fmt.Println()
fmt.Println("COMMANDS:")
fmt.Println(" -s Start your day")
fmt.Println(" -a \"task name\" Add completed task")
fmt.Println(" -c \"comment\" Add comment (use with -a)")
fmt.Println(" -r Show today's report")
fmt.Println(" -x Extend last task to now")
fmt.Println(" -h Show this help")
fmt.Println()
fmt.Println("EXAMPLES:")
fmt.Println(" tt -s # Start your day")
fmt.Println(" tt -a \"Meeting: Standup\"")
fmt.Println(" tt -a \"Lunch **\" # Break task")
fmt.Println(" tt -a \"Dev work\" -c \"Fixed login bug\"")
fmt.Println(" tt -r # View today's report")
fmt.Println(" tt -x # Extend last task")
fmt.Println()
fmt.Println("TASK TYPES:")
fmt.Println(" Regular task: \"Meeting: Standup\"")
fmt.Println(" Break task: \"Lunch **\"")
fmt.Println(" Ignored task: \"Commuting ***\"")
}
func printTodaysReport(tracker *TimeTracker) {
activities := tracker.getTodaysActivities()