forked from caarlos0/timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (134 loc) · 3.29 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
package main
import (
"fmt"
"os"
"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"
"github.com/muesli/coral"
mcoral "github.com/muesli/mango-coral"
"github.com/muesli/roff"
)
type model struct {
name string
duration time.Duration
start time.Time
timer timer.Model
progress progress.Model
quitting 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
step := 100.0 / (m.duration).Seconds()
cmds = append(cmds, m.progress.IncrPercent(step/100.0))
// pm, cmd := m.progress.Update(msg)
// cmds = append(cmds, cmd)
// m.progress = pm.(progress.Model)
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
if 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
}
}
return m, nil
}
func (m model) View() string {
if m.quitting {
return "\n"
}
result := boldStyle.Render(m.start.Format(time.Kitchen))
if m.name != "" {
result += ": " + italicStyle.Render(m.name)
}
result += " - " + boldStyle.Render(m.timer.View()) + "\n" + m.progress.View()
return result
}
var (
name string
version = "dev"
quitKeys = key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q", "quit"),
)
boldStyle = lipgloss.NewStyle().Bold(true)
italicStyle = lipgloss.NewStyle().Italic(true)
)
const (
padding = 2
maxWidth = 80
)
var rootCmd = &coral.Command{
Use: "timer",
Short: "timer is like sleep, but with progress report",
Version: version,
SilenceUsage: true,
Args: coral.ExactArgs(1),
RunE: func(cmd *coral.Command, args []string) error {
duration, err := time.ParseDuration(args[0])
if err != nil {
return err
}
m := model{
duration: duration,
timer: timer.NewWithInterval(duration, time.Second),
progress: progress.New(progress.WithDefaultGradient()),
name: name,
start: time.Now(),
}
return tea.NewProgram(m).Start()
},
}
var manCmd = &coral.Command{
Use: "man",
Short: "Generates man pages",
SilenceUsage: true,
DisableFlagsInUseLine: true,
Hidden: true,
Args: coral.NoArgs,
RunE: func(cmd *coral.Command, args []string) error {
manPage, err := mcoral.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.AddCommand(manCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}