-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (91 loc) · 2.65 KB
/
main.go
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
package main
import (
"fmt"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
func main() {
config := loadConfig()
format := config.ShowStatsFormat
if config.SessionStatAsSeconds {
format = "seconds"
}
tracker, err := NewRuntimeTracker("")
if err != nil {
fail("error creating tracker: %s", err)
}
if len(os.Args) > 1 && os.Args[1] == "-s" {
stats := tracker.GetStats()
fmt.Println(formatStat("Daily", stats.Daily[stats.CurrentDay], config.ShowStatsFormat))
fmt.Println(formatStat("Weekly", stats.Weekly[stats.CurrentWeek], config.ShowStatsFormat))
fmt.Println(formatStat("Monthly", stats.Monthly[stats.CurrentMonth], config.ShowStatsFormat))
fmt.Println(formatStat("Yearly", stats.Yearly[stats.CurrentYear], config.ShowStatsFormat))
os.Exit(0)
}
if err := findGitDir(); err != nil {
fail(err.Error())
}
stagedFiles, err := filesInStaging()
if err != nil {
fail(err.Error())
}
commitSearchTerm := ""
if len(os.Args) > 1 && os.Args[1] == "-m" {
commitSearchTerm = os.Args[2]
}
if config.StoreRuntime || config.ShowRuntime {
tracker.Start()
}
m := newModel(config, stagedFiles, commitSearchTerm)
if _, err := tea.NewProgram(m).Run(); err != nil {
fail(err.Error())
}
fmt.Println("")
if !m.Finished() {
fail("terminated")
}
msg, withBody := m.CommitMessage()
if err := commit(msg, withBody, config.SignOffCommits); err != nil {
fail("error committing: %s", err)
}
if config.StoreRuntime || config.ShowRuntime {
err := tracker.Stop()
if err != nil {
fail("error stopping tracker: %s", err)
}
if config.ShowRuntime && !config.ShowStats {
stats := tracker.GetStats()
fmt.Println()
fmt.Println(formatStat("Session", stats.Session, format))
}
}
if config.ShowStats {
stats := tracker.GetStats()
fmt.Println()
fmt.Println(formatStat("Session", stats.Session, format))
fmt.Println(formatStat("Daily", stats.Daily[stats.CurrentDay], config.ShowStatsFormat))
fmt.Println(formatStat("Weekly", stats.Weekly[stats.CurrentWeek], config.ShowStatsFormat))
fmt.Println(formatStat("Monthly", stats.Monthly[stats.CurrentMonth], config.ShowStatsFormat))
fmt.Println(formatStat("Yearly", stats.Yearly[stats.CurrentYear], config.ShowStatsFormat))
}
}
func formatStat(stat string, seconds float32, format string) string {
var value float32
switch format {
case "minutes":
value = seconds / 60.0
case "hours":
value = seconds / 3600.0
default:
value = seconds
}
return fmt.Sprintf(" > %s: %.2f %s", stat, value, format)
}
func fail(format string, args ...interface{}) {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
_, _ = fmt.Fprintf(os.Stderr, format, args...)
os.Exit(1)
}