-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
view.go
178 lines (152 loc) Β· 3.78 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
/**
* 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"
"strings"
"github.com/muesli/termenv"
)
// Width required to display 24 hours
const UIWidth = 94
func (m model) View() string {
s := normalTextStyle("\n What time is it?\n\n").String()
// Show hours for each zone
for zi, zone := range m.zones {
hours := strings.Builder{}
dates := strings.Builder{}
startHour := 0
if zi > 0 {
startHour = (zone.currentTime().Hour() - m.zones[0].currentTime().Hour()) % 24
}
dateChanged := false
for i := startHour; i < startHour+24; i++ {
hour := ((i % 24) + 24) % 24 // mod 24
out := termenv.String(fmt.Sprintf("%2d", hour))
out = out.Foreground(term.Color(hourColorCode(hour)))
// Cursor
if m.hour == i-startHour {
out = out.Background(term.Color("#00B67F"))
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 {
continue
}
if hour == 0 {
dates.WriteString(formatDayChange(&m, zone))
dateChanged = true
}
if !dateChanged {
dates.WriteString(" ")
}
}
var datetime string
if m.isMilitary {
datetime = zone.ShortMT()
} else {
datetime = zone.ShortDT()
}
zoneHeader := fmt.Sprintf("%s %s %s", zone.ClockEmoji(), normalTextStyle(zone.String()), dateTimeStyle(datetime))
s += fmt.Sprintf(" %s\n %s\n %s\n", zoneHeader, hours.String(), dates.String())
}
if m.interactive {
s += status()
}
return s
}
func status() string {
text := " q: quit, d: toggle date"
for {
text += " "
if len(text) > UIWidth {
text = text[0:UIWidth]
break
}
}
color := "#939183"
if hasDarkBackground {
color = "#605C5A"
}
status := termenv.String(text).Foreground(term.Color(color))
return status.String()
}
func formatDayChange(m *model, z *Zone) string {
zTime := z.currentTime()
if zTime.Hour() > m.now.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))
}