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
|
package cli
import (
"errors"
"fmt"
"math/rand/v2"
"strconv"
"strings"
"time"
"codeberg.org/snonux/timr/internal/ascii"
timrTimer "codeberg.org/snonux/timr/internal/timer"
tuiapp "codeberg.org/snonux/timr/internal/tui"
"codeberg.org/snonux/timr/internal/worktime"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)
func newTimerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "timer",
Short: "Stopwatch timer operations",
}
cmd.AddCommand(newTimerStartCmd())
cmd.AddCommand(newTimerStopCmd())
cmd.AddCommand(newTimerContinueCmd())
cmd.AddCommand(newTimerResetCmd())
cmd.AddCommand(newTimerStatusCmd())
cmd.AddCommand(newTimerPromptCmd())
cmd.AddCommand(newTimerTrackCmd())
cmd.AddCommand(newTimerLiveCmd())
return cmd
}
func newTimerStartCmd() *cobra.Command {
return &cobra.Command{
Use: "start",
Short: "Start the timer",
RunE: func(cmd *cobra.Command, args []string) error {
hasElapsed, err := timerHasElapsed()
if err != nil {
return err
}
output, err := timrTimer.StartTimer(hasElapsed)
if err != nil {
return err
}
if err := syncWorktimeWithTimer(cmd, true); err != nil {
return err
}
return printOutput(cmd, output)
},
}
}
func newTimerStopCmd() *cobra.Command {
return &cobra.Command{
Use: "stop",
Short: "Stop the timer",
RunE: func(cmd *cobra.Command, args []string) error {
output, err := timrTimer.StopTimer()
if err != nil {
return fmt.Errorf("stop timer: %w", err)
}
if err := syncWorktimeWithTimer(cmd, false); err != nil {
return err
}
return printOutput(cmd, output)
},
}
}
func newTimerContinueCmd() *cobra.Command {
return &cobra.Command{
Use: "continue",
Short: "Continue a stopped timer",
RunE: func(cmd *cobra.Command, args []string) error {
hasElapsed, err := timerHasElapsed()
if err != nil {
return err
}
output := "Timer is at 0, cannot continue."
if hasElapsed {
output, err = timrTimer.StartTimer(true)
if err != nil {
return err
}
}
return printOutput(cmd, output)
},
}
}
func newTimerResetCmd() *cobra.Command {
return &cobra.Command{
Use: "reset",
Short: "Reset the timer",
RunE: func(cmd *cobra.Command, args []string) error {
output, err := timrTimer.ResetTimer()
if err != nil {
return fmt.Errorf("reset timer: %w", err)
}
return printOutput(cmd, output)
},
}
}
func newTimerStatusCmd() *cobra.Command {
var raw bool
var rawMinutes bool
cmd := &cobra.Command{
Use: "status",
Short: "Show timer status",
RunE: func(cmd *cobra.Command, args []string) error {
if raw && rawMinutes {
return errors.New("only one of --raw or --raw-minutes can be set")
}
var (
output string
err error
)
switch {
case raw:
output, err = timrTimer.GetRawStatus()
case rawMinutes:
output, err = timrTimer.GetRawMinutesStatus()
default:
output, err = timrTimer.GetStatus()
}
if err != nil {
return err
}
return printOutput(cmd, output)
},
}
cmd.Flags().BoolVar(&raw, "raw", false, "Show elapsed time in seconds")
cmd.Flags().BoolVar(&rawMinutes, "raw-minutes", false, "Show elapsed time in minutes")
return cmd
}
func newTimerPromptCmd() *cobra.Command {
return &cobra.Command{
Use: "prompt",
Short: "Show prompt-friendly timer status",
RunE: func(cmd *cobra.Command, args []string) error {
output, err := timrTimer.GetPromptStatus()
if err != nil {
return fmt.Errorf("get prompt timer status: %w", err)
}
return printOutput(cmd, output)
},
}
}
func newTimerTrackCmd() *cobra.Command {
return &cobra.Command{
Use: "track <description>",
Short: "Track elapsed time to Taskwarrior and reset timer",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
description := strings.Join(args, " ")
output, err := timrTimer.TrackTime(description)
if err != nil {
return fmt.Errorf("track timer entry %q: %w", description, err)
}
return printOutput(cmd, output)
},
}
}
func newTimerLiveCmd() *cobra.Command {
var font string
cmd := &cobra.Command{
Use: "live",
Short: "Launch interactive live timer view",
RunE: func(cmd *cobra.Command, args []string) error {
selectedFont := strings.TrimSpace(font)
if selectedFont == "" {
selectedFont = ascii.AllFonts[rand.IntN(len(ascii.AllFonts))]
}
model, err := tuiapp.NewTimerModel(selectedFont, currentConfig(cmd))
if err != nil {
return err
}
program := tea.NewProgram(model)
return program.Start()
},
}
cmd.Flags().StringVarP(&font, "font", "f", "", "Font for live timer (doom, mono12, rebel, ansi, ansiShadow)")
return cmd
}
func printOutput(cmd *cobra.Command, output string) error {
if output == "" {
return nil
}
_, err := fmt.Fprintln(cmd.OutOrStdout(), output)
return err
}
func syncWorktimeWithTimer(cmd *cobra.Command, start bool) error {
cfg := currentConfig(cmd)
if !cfg.AutoWorktimeLogin {
return nil
}
ctx, err := resolveWorkContext(cmd)
if err != nil {
return err
}
now := time.Now()
if start {
_, err = worktime.Login(ctx.dbDir, ctx.host, "work", now, "auto timer start")
} else {
_, err = worktime.Logout(ctx.dbDir, ctx.host, "work", now, "auto timer stop")
}
if err == nil {
return nil
}
// Avoid failing timer commands on no-op state sync mismatches.
if errors.Is(err, worktime.ErrAlreadyLoggedIn) || errors.Is(err, worktime.ErrNotLoggedIn) {
return nil
}
return err
}
func timerHasElapsed() (bool, error) {
rawStatus, err := timrTimer.GetRawStatus()
if err != nil {
return false, fmt.Errorf("get raw timer status: %w", err)
}
status, err := strconv.ParseFloat(rawStatus, 64)
if err != nil {
return false, fmt.Errorf("parse raw timer status %q: %w", rawStatus, err)
}
return status > 0, nil
}
|