Skip to content

Commit c0010fe

Browse files
Merge pull request #20 from DylanDevelops/ravel/fix-sorting
fix: `tmpo stats` now lists projects in the same order every time
2 parents f5c29fe + 4f83d5d commit c0010fe

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

cmd/stats.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"sort"
67
"time"
78

89
"github.com/DylanDevelops/tmpo/internal/storage"
@@ -121,7 +122,16 @@ func ShowPeriodStats(entries []*storage.TimeEntry, periodName string) {
121122

122123
fmt.Println()
123124
ui.PrintInfo(4, ui.Bold("By Project"), "")
124-
for project, duration := range projectStats {
125+
126+
// Sort projects alphabetically for consistent display order
127+
var projects []string
128+
for project := range projectStats {
129+
projects = append(projects, project)
130+
}
131+
sort.Strings(projects)
132+
133+
for _, project := range projects {
134+
duration := projectStats[project]
125135
percentage := (duration.Seconds() / totalDuration.Seconds()) * 100
126136
fmt.Printf(" %s %s (%.1f%%)\n", ui.Bold(fmt.Sprintf("%-20s", project)), ui.FormatDuration(duration), percentage)
127137

@@ -174,20 +184,29 @@ func ShowAllTimeStats(entries []*storage.TimeEntry, db *storage.Database) {
174184
}
175185
}
176186

177-
projects, _ := db.GetAllProjects()
187+
allProjects, _ := db.GetAllProjects()
178188

179189
ui.PrintSuccess(ui.EmojiStats, ui.Bold("All-Time Statistics"))
180190
ui.PrintInfo(4, ui.Bold("Total Time"), fmt.Sprintf("%s (%.2f hours)", ui.FormatDuration(totalDuration), totalDuration.Hours()))
181191
ui.PrintInfo(4, ui.Bold("Total Entries"), fmt.Sprintf("%d", len(entries)))
182-
ui.PrintInfo(4, ui.Bold("Projects Tracked"), fmt.Sprintf("%d", len(projects)))
192+
ui.PrintInfo(4, ui.Bold("Projects Tracked"), fmt.Sprintf("%d", len(allProjects)))
183193

184194
if hasAnyEarnings {
185195
ui.PrintInfo(4, ui.Bold("Earnings"), fmt.Sprintf("$%.2f", totalEarnings))
186196
}
187197

188198
fmt.Println()
189199
ui.PrintInfo(4, ui.Bold("By Project"), "")
190-
for project, duration := range projectStats {
200+
201+
// Sort projects alphabetically for consistent display order
202+
var projects []string
203+
for project := range projectStats {
204+
projects = append(projects, project)
205+
}
206+
sort.Strings(projects)
207+
208+
for _, project := range projects {
209+
duration := projectStats[project]
191210
percentage := (duration.Seconds() / totalDuration.Seconds()) * 100
192211
fmt.Printf(" %s %s (%.1f%%)\n", ui.Bold(fmt.Sprintf("%-20s", project)), ui.FormatDuration(duration), percentage)
193212

0 commit comments

Comments
 (0)