-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
210 lines (186 loc) · 4.94 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
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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/timer"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
mcobra "github.com/muesli/mango-cobra"
"github.com/muesli/roff"
"github.com/spf13/cobra"
)
type model struct {
name string
altscreen bool
startTimeFormat string
duration time.Duration
passed time.Duration
start time.Time
timer timer.Model
progress progress.Model
quitting bool
interrupting bool
}
func (m model) Init() tea.Cmd {
return m.timer.Init()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case timer.TickMsg:
var cmds []tea.Cmd
var cmd tea.Cmd
m.passed += m.timer.Interval
pct := m.passed.Milliseconds() * 100 / m.duration.Milliseconds()
cmds = append(cmds, m.progress.SetPercent(float64(pct)/100))
m.timer, cmd = m.timer.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
case tea.WindowSizeMsg:
m.progress.Width = msg.Width - padding*2 - 4
winHeight, winWidth = msg.Height, msg.Width
if !m.altscreen && m.progress.Width > maxWidth {
m.progress.Width = maxWidth
}
return m, nil
case timer.StartStopMsg:
var cmd tea.Cmd
m.timer, cmd = m.timer.Update(msg)
return m, cmd
case timer.TimeoutMsg:
m.quitting = true
return m, tea.Quit
case progress.FrameMsg:
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(progress.Model)
return m, cmd
case tea.KeyMsg:
if key.Matches(msg, quitKeys) {
m.quitting = true
return m, tea.Quit
}
if key.Matches(msg, intKeys) {
m.interrupting = true
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
if m.quitting || m.interrupting {
return ""
}
var startTimeFormat string
switch strings.ToLower(m.startTimeFormat) {
case "24h":
startTimeFormat = "15:04" // See: https://golang.cafe/blog/golang-time-format-example.html
default:
startTimeFormat = time.Kitchen
}
result := boldStyle.Render(m.start.Format(startTimeFormat))
if m.name != "" {
result += ": " + italicStyle.Render(m.name)
}
result += " - " + boldStyle.Render(m.timer.View()) + "\n" + m.progress.View()
if m.altscreen {
return altscreenStyle.
MarginTop((winHeight - 2) / 2).
Render(result)
}
return result
}
var (
name string
altscreen bool
startTimeFormat string
winHeight, winWidth int
version = "dev"
quitKeys = key.NewBinding(key.WithKeys("esc", "q"))
intKeys = key.NewBinding(key.WithKeys("ctrl+c"))
altscreenStyle = lipgloss.NewStyle().MarginLeft(padding)
boldStyle = lipgloss.NewStyle().Bold(true)
italicStyle = lipgloss.NewStyle().Italic(true)
)
const (
padding = 2
maxWidth = 80
)
var rootCmd = &cobra.Command{
Use: "timer",
Short: "timer is like sleep, but with progress report",
Version: version,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addSuffixIfArgIsNumber(&(args[0]), "s")
duration, err := time.ParseDuration(args[0])
if err != nil {
return err
}
var opts []tea.ProgramOption
if altscreen {
opts = append(opts, tea.WithAltScreen())
}
interval := time.Second
if duration < time.Minute {
interval = 100 * time.Millisecond
}
m, err := tea.NewProgram(model{
duration: duration,
timer: timer.NewWithInterval(duration, interval),
progress: progress.New(progress.WithDefaultGradient()),
name: name,
altscreen: altscreen,
startTimeFormat: startTimeFormat,
start: time.Now(),
}, opts...).Run()
if err != nil {
return err
}
if m.(model).interrupting {
return fmt.Errorf("interrupted")
}
if name != "" {
cmd.Printf("%s ", name)
}
cmd.Printf("finished!\n")
return nil
},
}
var manCmd = &cobra.Command{
Use: "man",
Short: "Generates man pages",
SilenceUsage: true,
DisableFlagsInUseLine: true,
Hidden: true,
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
manPage, err := mcobra.NewManPage(1, rootCmd)
if err != nil {
return err
}
_, err = fmt.Fprint(os.Stdout, manPage.Build(roff.NewDocument()))
return err
},
}
func init() {
rootCmd.Flags().StringVarP(&name, "name", "n", "", "timer name")
rootCmd.Flags().BoolVarP(&altscreen, "fullscreen", "f", false, "fullscreen")
rootCmd.Flags().StringVarP(&startTimeFormat, "format", "", "", "Specify start time format, possible values: 24h, kitchen")
rootCmd.AddCommand(manCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func addSuffixIfArgIsNumber(s *string, suffix string) {
_, err := strconv.ParseFloat(*s, 64)
if err == nil {
*s = *s + suffix
}
}