-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
240 lines (220 loc) · 4.7 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
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
// License: GPL-3.0-only
// (c) 2022 Dakota Walsh <[email protected]>
package main
import (
"io"
"log"
"os"
"strconv"
"time"
"git.sr.ht/~kota/calendar/calendar"
"git.sr.ht/~kota/calendar/config"
"git.sr.ht/~kota/calendar/help"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
)
// Version is overwritten at build time in the Makefile.
var Version = "development version"
// tickerMsg is a tea.Msg which should be returned with the current time.
type tickerMsg time.Time
// model is the top level Bubble Tea model for the whole program.
type model struct {
config *config.Config
mode mode
calendar calendar.Calendar
help help.Help
width int
height int
}
// Init the model in Bubble Tea.
func (m model) Init() tea.Cmd {
cmds := []tea.Cmd{
m.calendar.Init(),
fiveMinutes(),
}
return tea.Batch(cmds...)
}
// mode describes if the calendar or the help menu should be shown.
type mode uint8
const (
modeCalendar mode = iota
modeHelp
)
// fiveMinutes starts a timer which will return a tickerMsg after five minutes.
// This is used to update the "today" value on the calendar.
func fiveMinutes() tea.Cmd {
now := time.Now()
tomorrow := time.Date(
now.Year(),
now.Month(),
now.Day(),
now.Hour(),
now.Minute()+5,
0,
0,
now.Location(),
)
d := tomorrow.Sub(now)
return tea.Tick(d, func(t time.Time) tea.Msg {
return tickerMsg(t)
})
}
// Updates the model in the Bubble Tea update loop.
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case m.config.KeyHelp.Contains(msg.String()):
m.mode = modeHelp
case m.config.KeyQuit.Contains(msg.String()):
if m.mode == modeHelp {
m.mode = modeCalendar
} else {
return m, tea.Quit
}
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
case tickerMsg:
// Update the "today" value and kick off another timer.
m.calendar.SetToday(time.Now())
return m, fiveMinutes()
}
return m.propagate(msg)
}
// propagate an update to all children.
func (m model) propagate(msg tea.Msg) (tea.Model, tea.Cmd) {
var c tea.Cmd
if m.mode == modeHelp {
m.help, c = m.help.Update(msg)
} else {
m.calendar, c = m.calendar.Update(msg)
}
return m, c
}
// View renders the model in its current state.
func (m model) View() string {
if m.mode == modeHelp {
// Display the help menu.
return lipgloss.Place(
m.width,
m.height,
lipgloss.Center,
lipgloss.Center,
m.help.View(),
)
}
// Display the calendar.
return zone.Scan(lipgloss.Place(
m.width,
m.height,
lipgloss.Center,
lipgloss.Center,
m.calendar.View(),
))
}
// parseArgs reads the program's arguments to parse a starting selected time.
func parseArgs(args []string, now time.Time) time.Time {
switch len(args) {
case 1:
// Early exit if there were no arguments.
return now
case 2:
// Argument is either "day", "timestamp", or "monthname".
timestamp, err := time.Parse("2006-01-02", args[1])
if err == nil {
return timestamp
}
monthnameTime, err := time.Parse("January", args[1])
if err == nil {
return time.Date(
now.Year(),
monthnameTime.Month(),
now.Day(),
0, 0, 0, 0,
now.Location(),
)
}
day, err := strconv.Atoi(args[1])
if err == nil {
return time.Date(
now.Year(),
now.Month(),
day,
0, 0, 0, 0,
now.Location(),
)
}
return now
case 3:
day, err := strconv.Atoi(args[1])
if err != nil {
return now
}
month, err := strconv.Atoi(args[2])
if err != nil {
return now
}
return time.Date(
now.Year(),
time.Month(month),
day,
0, 0, 0, 0,
now.Location(),
)
case 4:
day, err := strconv.Atoi(args[1])
if err != nil {
return now
}
month, err := strconv.Atoi(args[2])
if err != nil {
return now
}
year, err := strconv.Atoi(args[3])
if err != nil {
return now
}
return time.Date(
year,
time.Month(month),
day,
0, 0, 0, 0,
now.Location(),
)
}
// Return now as a fallback.
return now
}
func main() {
log.SetPrefix("")
log.SetFlags(0)
log.SetOutput(io.Discard)
if len(os.Getenv("DEBUG")) > 0 {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
log.Fatalf("failed setting up debug logging: %v\n", err)
}
defer f.Close()
}
conf, err := config.Load()
if err != nil {
log.Fatalf("failed to load config: %v\n", err)
}
selected := parseArgs(os.Args, time.Now())
zone.NewGlobal()
p := tea.NewProgram(
model{
calendar: calendar.New(selected, conf),
help: help.New(Version),
config: conf,
},
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
if _, err := p.Run(); err != nil {
log.Fatalf("calendar has crashed: %v\n", err)
}
}