-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
view.go
359 lines (318 loc) ยท 8.58 KB
/
view.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* This file is part of tz.
*
* tz is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* tz is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tz. If not, see <https://www.gnu.org/licenses/>.
**/
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/muesli/termenv"
xterm "golang.org/x/term"
)
type FormatStyle int
const (
DefaultFormatStyle FormatStyle = iota
IsoFormatStyle
UnixFormatStyle
)
func (fs FormatStyle) next() FormatStyle {
switch (fs) {
case DefaultFormatStyle:
return IsoFormatStyle
case IsoFormatStyle:
return UnixFormatStyle
default:
return DefaultFormatStyle
}
}
func (fs FormatStyle) previous() FormatStyle {
switch (fs) {
case DefaultFormatStyle:
return UnixFormatStyle
case UnixFormatStyle:
return IsoFormatStyle
default:
return DefaultFormatStyle
}
}
type ZoneStyle int
const (
AbbreviationZoneStyle ZoneStyle = iota
WithZOffsetZoneStyle
WithRelativeZoneStyle
)
func (zs ZoneStyle) next() ZoneStyle {
switch (zs) {
case AbbreviationZoneStyle:
return WithZOffsetZoneStyle
case WithZOffsetZoneStyle:
return WithRelativeZoneStyle
default:
return AbbreviationZoneStyle
}
}
func (zs ZoneStyle) previous() ZoneStyle {
switch (zs) {
case AbbreviationZoneStyle:
return WithRelativeZoneStyle
case WithRelativeZoneStyle:
return WithZOffsetZoneStyle
default:
return AbbreviationZoneStyle
}
}
// Width required to display 24 hours
const UIWidth = 94
const MinimumZoneHeaderPadding = 6
const MaximumZoneHeaderColumns = UIWidth + MinimumZoneHeaderPadding
func (m model) View() string {
s := normalTextStyle("\n What time is it?\n\n").String()
zoneHeaderWidth := MaximumZoneHeaderColumns
envWidth, envErr := strconv.Atoi(os.Getenv("COLUMNS"))
if envErr == nil {
zoneHeaderWidth = min(envWidth, zoneHeaderWidth)
} else {
fd := int(os.Stdout.Fd())
if xterm.IsTerminal(fd) {
termWidth, _, termErr := xterm.GetSize(fd)
if termErr == nil {
zoneHeaderWidth = min(termWidth, zoneHeaderWidth)
}
}
}
midnight := time.Date(
m.clock.t.Year(),
m.clock.t.Month(),
m.clock.t.Day(),
0, // Hours
m.clock.t.Minute(),
0, // Seconds
0, // Nanoseconds
m.clock.t.Location(),
)
midnightOffset := time.Duration(m.clock.t.UnixNano() - midnight.UnixNano())
cursorColumn := int(midnightOffset / time.Hour)
// Show hours for each zone
for i, zone := range m.zones {
hours := strings.Builder{}
dates := strings.Builder{}
timeInZone := zone.currentTime(m.clock.t)
midnightInZone := timeInZone.Add(-midnightOffset)
wasDST := midnightInZone.Add(-time.Hour).IsDST()
previousHour := midnightInZone.Add(-time.Hour).Hour()
highlighted := i == (m.highlighted - 1)
dateChanged := false
for column := 0; column < 24; column++ {
time := midnightInZone.Add(time.Duration(column) * time.Hour)
nowDST := time.IsDST()
hour := time.Hour()
out := termenv.String(fmt.Sprintf("%2d", hour))
out = out.Foreground(term.Color(hourColorCode(hour)))
// Cursor
if column == cursorColumn {
out = out.Background(term.Color(hourColorCode(hour)))
if hasDarkBackground {
out = out.Foreground(term.Color("#262626")).Bold()
} else {
out = out.Foreground(term.Color("#f1f1f1"))
}
}
hours.WriteString(out.String())
hours.WriteString(" ")
// Show the day under the hour, when the date changes.
if m.showDates {
if hour < previousHour {
dates.WriteString(formatDayChange(&m, zone))
dateChanged = true
}
if wasDST != nowDST {
if nowDST {
dates.WriteString("=DST")
} else {
dates.WriteString("โ DST")
}
} else if !dateChanged {
dates.WriteString(" ")
}
}
wasDST = nowDST
previousHour = hour
}
var datetime string
switch m.formatStyle {
case IsoFormatStyle:
datetime = timeInZone.Format("2006-01-02T15:04-07:00")
case UnixFormatStyle:
_, weekOfYear := timeInZone.ISOWeek()
dayOfYear := timeInZone.Format("__2")
yesNo := map[bool]string{true: "With", false: "No"}
datetime = fmt.Sprintf(
"%v DST, Week %v, Day %v, Unix %v",
yesNo[timeInZone.IsDST()],
weekOfYear,
dayOfYear,
timeInZone.Unix(),
)
default:
if m.isMilitary {
datetime = zone.ShortMT(m.clock.t)
} else {
datetime = zone.ShortDT(m.clock.t)
}
}
var zoneString = zone.VerboseString(timeInZone)
switch m.zoneStyle {
case WithZOffsetZoneStyle:
utcOffset := timeInZone.Format("Z-07:00")
zoneString = fmt.Sprintf("[%s] %s", utcOffset, zoneString)
case WithRelativeZoneStyle:
_, otherOffset := timeInZone.Zone()
_, localOffset := m.clock.t.Zone()
relativeOffset := m.clock.t.In(time.FixedZone("", otherOffset - localOffset)).Format("-07:00")
zoneString = fmt.Sprintf("[%s] %s", relativeOffset, zoneString)
default:
}
clockString := zone.ClockEmoji(m.clock.t)
usedZoneHeaderWidth := termenv.String(clockString + zoneString + datetime).Width()
unusedZoneHeaderWidth := max(0, zoneHeaderWidth - usedZoneHeaderWidth - MinimumZoneHeaderPadding)
rightAlignmentSpace := strings.Repeat(" ", unusedZoneHeaderWidth)
zoneHeader := fmt.Sprintf("%s %s %s%s", clockString, normalTextStyle(zoneString), rightAlignmentSpace, dateTimeStyle(datetime))
marker := " "
if highlighted {
marker = termenv.String(">>").Reverse().String()
}
lines := []string{zoneHeader, hours.String(), dates.String()}
for _, line := range lines {
s += fmt.Sprintf("%s%s\n", marker, line)
}
}
if m.interactive {
s += status(m)
}
return s
}
// Generate the help lines
func generateKeymapStrings(k Keymaps, showAll bool) []string {
helpKey := fmt.Sprintf("%s: help", k.Help[0])
quitKey := fmt.Sprintf("%s: quit", k.Quit[0])
if showAll {
delimiter := ", "
return []string {
strings.Join(
[]string {
helpKey,
fmt.Sprintf("%s/%s/%s: minutes", k.PrevMinute[0], k.NextMinute[0], k.ZeroMinute[0]),
fmt.Sprintf("%s/%s: hours", k.PrevHour[0], k.NextHour[0]),
fmt.Sprintf("%s/%s: days", k.PrevDay[0], k.NextDay[0]),
fmt.Sprintf("%s/%s: weeks", k.PrevWeek[0], k.NextWeek[0]),
fmt.Sprintf("%s: go to now", k.Now[0]),
fmt.Sprintf("%s/%s: highlight", k.NextLine[0], k.PrevLine[0]),
},
delimiter,
),
strings.Join(
[]string {
quitKey,
fmt.Sprintf("%s: toggle dates", k.ToggleDate[0]),
fmt.Sprintf("%s: toggle formats", k.NextFStyle[0]),
fmt.Sprintf("%s: toggle zone offsets", k.NextZStyle[0]),
fmt.Sprintf("%s: open in web", k.OpenWeb[0]),
},
delimiter,
),
}
} else {
return []string {
helpKey,
quitKey,
}
}
}
func status(m model) string {
var text []string = generateKeymapStrings(m.keymaps, m.showHelp)
backgroundPadding := strings.Repeat(" ", UIWidth)
for i, line := range text {
text[i] = (" " + line + backgroundPadding)[:UIWidth]
}
color := "#939183"
if hasDarkBackground {
color = "#605C5A"
}
status := termenv.String(strings.Join(text, "\n")).Foreground(term.Color(color))
return status.String()
}
func formatDayChange(m *model, z *Zone) string {
zTime := z.currentTime(m.clock.t)
if zTime.Hour() > m.clock.t.Hour() {
zTime = zTime.AddDate(0, 0, 1)
}
color := "#777266"
if hasDarkBackground {
color = "#7B7573"
}
str := termenv.String(fmt.Sprintf("๐ %s", zTime.Format("Mon 02")))
return str.Foreground(term.Color(color)).String()
}
// Return a color matching the time of the day at a given hour.
func hourColorCode(hour int) (color string) {
switch hour {
// Morning
case 7, 8:
if hasDarkBackground {
color = "#98E1D8"
} else {
color = "#35B6A6"
}
// Day
case 9, 10, 11, 12, 13, 14, 15, 16, 17:
if hasDarkBackground {
color = "#E8C64D"
} else {
color = "#FA8F2D"
}
// Evening
case 18, 19:
if hasDarkBackground {
color = "#C95F48"
} else {
color = "#FC6442"
}
// Night
default:
if hasDarkBackground {
color = "#5957C9"
} else {
color = "#664FC3"
}
}
return color
}
func dateTimeStyle(str string) termenv.Style {
color := "#777266"
if hasDarkBackground {
color = "#757575"
}
return termenv.String(str).Foreground(term.Color(color))
}
func normalTextStyle(str string) termenv.Style {
var color = "#32312B"
if hasDarkBackground {
color = "#ECEAD9"
}
return termenv.String(str).Foreground(term.Color(color))
}