|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/DylanDevelops/tmpo/internal/storage" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + logLimit int |
| 14 | + logProject string |
| 15 | + logToday bool |
| 16 | + logWeek bool |
| 17 | +) |
| 18 | + |
| 19 | +var logCmd = &cobra.Command{ |
| 20 | + Use: "log", |
| 21 | + Short: "View time tracking history", |
| 22 | + Long: `Display past time tracking entries with optional filtering.`, |
| 23 | + Run: func(cmd *cobra.Command, args []string) { |
| 24 | + db, err := storage.Initialize() |
| 25 | + |
| 26 | + if err != nil { |
| 27 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 28 | + os.Exit(1) |
| 29 | + } |
| 30 | + |
| 31 | + defer db.Close() |
| 32 | + |
| 33 | + var entries []*storage.TimeEntry |
| 34 | + |
| 35 | + if logToday { |
| 36 | + start := time.Now().Truncate(24 * time.Hour) |
| 37 | + end := start.Add(24 * time.Hour) |
| 38 | + entries, err = db.GetEntriesByDateRange(start, end) |
| 39 | + } else if logWeek { |
| 40 | + now := time.Now() |
| 41 | + weekday := int(now.Weekday()) |
| 42 | + if weekday == 0 { |
| 43 | + weekday = 7 // sunday |
| 44 | + } |
| 45 | + |
| 46 | + start := now.AddDate(0, 0, -weekday+1).Truncate(24 * time.Hour) |
| 47 | + end := start.AddDate(0, 0, 7) |
| 48 | + entries, err = db.GetEntriesByDateRange(start, end) |
| 49 | + } else if logProject != "" { |
| 50 | + entries, err = db.GetEntriesByProject(logProject) |
| 51 | + } else { |
| 52 | + entries, err = db.GetEntries(logLimit) |
| 53 | + } |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + |
| 60 | + if len(entries) == 0 { |
| 61 | + fmt.Println("No time entries found.") |
| 62 | + |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Printf("\n[tmpo] Time Entries (%d total)\n\n", len(entries)) |
| 67 | + |
| 68 | + var totalDuration time.Duration |
| 69 | + currentDate := "" |
| 70 | + |
| 71 | + for _, entry := range entries { |
| 72 | + entryDate := entry.StartTime.Format("Mon, Jan 2, 2006") |
| 73 | + if entryDate != currentDate { |
| 74 | + if currentDate != "" { |
| 75 | + fmt.Println() |
| 76 | + } |
| 77 | + |
| 78 | + fmt.Printf("─── %s ───\n", entryDate) |
| 79 | + currentDate = entryDate |
| 80 | + } |
| 81 | + |
| 82 | + duration := entry.Duration() |
| 83 | + totalDuration += duration |
| 84 | + |
| 85 | + timeRange := entry.StartTime.Format("3:04 PM") |
| 86 | + if entry.EndTime != nil { |
| 87 | + timeRange += " - " + entry.EndTime.Format("3:04 PM") |
| 88 | + } else { |
| 89 | + timeRange += " - (running)" |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Printf(" %s %-20s %s\n", timeRange, entry.ProjectName, formatDuration(duration)) |
| 93 | + if entry.Description != "" { |
| 94 | + fmt.Printf(" └─ %s\n", entry.Description) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fmt.Printf("\n─────────────────────────────────────────\n") |
| 99 | + fmt.Printf("Total Time: %s\n", formatDuration(totalDuration)) |
| 100 | + }, |
| 101 | +} |
| 102 | + |
| 103 | +func init() { |
| 104 | + rootCmd.AddCommand(logCmd) |
| 105 | + |
| 106 | + logCmd.Flags().IntVarP(&logLimit, "limit", "l", 10, "Number of entries to show") |
| 107 | + logCmd.Flags().StringVarP(&logProject, "project", "p", "", "Filter by project name") |
| 108 | + logCmd.Flags().BoolVarP(&logToday, "today", "t", false, "Show today's entries") |
| 109 | + logCmd.Flags().BoolVarP(&logWeek, "week", "w", false, "Show this week's entries") |
| 110 | +} |
0 commit comments