-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
201 lines (175 loc) · 4.67 KB
/
main.go
File metadata and controls
201 lines (175 loc) · 4.67 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
package main
// A TUI timer displaying a countdown in minutes and seconds.
//
// Accepts a single command line argument to set the timer duration in minutes.
import (
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"time"
"charm.land/bubbles/v2/key"
"charm.land/bubbles/v2/progress"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"tiny-timer/status"
)
var version = "dev"
func main() {
title, countUp, clean, debug := parseFlags()
configureLogging(debug)
if clean {
handleCleanFlag()
return
}
if err := initDB(); err != nil {
fmt.Println("Error initializing database:", err)
os.Exit(1)
}
defer closeDBConnection()
targetDuration := calculateTargetDuration(countUp)
keys := createKeyBindings()
m := createModel(title, countUp, targetDuration, keys)
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Oh no!", err)
os.Exit(1)
}
}
func parseFlags() (title string, countUp bool, clean bool, debug bool) {
titleFlag := flag.String("title", "", "Optional title for the timer session")
countUpFlag := flag.Bool("count-up", false, "Enable count-up mode (logs task time after completion)")
cleanFlag := flag.Bool("clean", false, "Delete the database and exit")
debugFlag := flag.Bool("debug", false, "Enable debug logging to debug.log")
versionFlag := flag.Bool("version", false, "Print version and exit")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [minutes] [flags]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Positional arguments:\n")
fmt.Fprintf(os.Stderr, " minutes\n")
fmt.Fprintf(os.Stderr, " \tDuration in minutes for the timer (default: 25)\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
preprocessArgs()
flag.Parse()
if *versionFlag {
fmt.Println("tiny-timer version", version)
os.Exit(0)
}
return *titleFlag, *countUpFlag, *cleanFlag, *debugFlag
}
func preprocessArgs() {
args := os.Args[1:]
if len(args) > 0 {
if _, err := strconv.ParseInt(args[0], 10, 64); err == nil {
minutes := args[0]
newArgs := append(args[1:], minutes)
os.Args = append([]string{os.Args[0]}, newArgs...)
}
}
}
func configureLogging(debug bool) {
if debug {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: Failed to enable debug logging: %v\n", err)
} else {
defer f.Close()
}
} else {
log.SetOutput(io.Discard)
}
}
func handleCleanFlag() {
dbPath, err := getDBPath()
if err != nil {
fmt.Println("Error getting database path:", err)
os.Exit(1)
}
if _, err := os.Stat(dbPath); err == nil {
if err := os.Remove(dbPath); err != nil {
fmt.Println("Error deleting database:", err)
os.Exit(1)
}
fmt.Println("Database deleted successfully.")
} else if os.IsNotExist(err) {
fmt.Println("Database does not exist.")
} else {
fmt.Println("Error checking database:", err)
os.Exit(1)
}
}
func calculateTargetDuration(countUp bool) int64 {
var targetDurationInMinutes int64 = defaultDurationInMinutes
if flag.NArg() > 0 {
if arg, err := strconv.ParseInt(flag.Arg(0), 10, 64); err == nil && arg > 0 {
targetDurationInMinutes = arg
}
}
targetDuration := targetDurationInMinutes * 60
if countUp && flag.NArg() == 0 {
targetDuration = defaultCountUpDuration
}
return targetDuration
}
func createKeyBindings() keyMap {
return keyMap{
Done: key.NewBinding(
key.WithKeys("d"),
key.WithHelp("d", "done"),
),
History: key.NewBinding(
key.WithKeys("h"),
key.WithHelp("h", "history"),
),
Title: key.NewBinding(
key.WithKeys("t"),
key.WithHelp("t", "title"),
),
Minutes: key.NewBinding(
key.WithKeys("m"),
key.WithHelp("m", "minutes"),
),
Reset: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "reset"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q/esc", "quit"),
),
Confirm: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "confirm"),
),
Cancel: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "cancel"),
),
Backspace: key.NewBinding(
key.WithKeys("backspace"),
key.WithHelp("backspace", "delete"),
),
}
}
func createModel(title string, countUp bool, targetDuration int64, keys keyMap) model {
prog := progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage())
if countUp {
prog.SetPercent(0)
} else {
prog.SetPercent(1.0)
}
statusCmp := status.NewStatusCmp()
statusCmp.SetKeyMap(keys)
return model{
progress: prog,
startTime: time.Now().Unix(),
targetDuration: targetDuration,
title: title,
countUpMode: countUp,
help: newHelpModel(),
keys: keys,
status: statusCmp,
}
}