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
|
package main
import (
"fmt"
"strconv"
"strings"
"time"
"charm.land/lipgloss/v2"
"github.com/NimbleMarkets/ntcharts/linechart/timeserieslinechart"
)
var block = lipgloss.NewStyle().
Padding(1).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Magenta)
var bold = lipgloss.NewStyle().
Bold(true)
var tStyle = bold
// TODO Faint should render based on light/dark terminal theme
var faint = lipgloss.NewStyle().
Foreground(lipgloss.BrightWhite)
var temperature_colors = lipgloss.Blend1D(20,
lipgloss.Color("#94006f"),
lipgloss.Blue,
lipgloss.Blue,
lipgloss.Green,
lipgloss.Color("#ff5500"),
lipgloss.Red,
)
// TODO Handle light theme
var rain_colors = lipgloss.Blend1D(10,
lipgloss.White,
lipgloss.Blue,
)
func main() {
weather, coords := GetWeather()
header := fmt.Sprintf("%s, %s (%s, %s)\n",
bold.Render(coords.Name),
bold.Render(coords.State),
faint.Render(ff(weather.Lat)),
faint.Render(ff(weather.Lon)),
)
current := fmtCurrent(weather)
minutely := fmtMinutely(weather)
hourly := fmtHourly(weather)
lipgloss.Println(lipgloss.JoinVertical(lipgloss.Center,
header,
current,
minutely,
hourly,
))
}
func fmtHourly(w WeatherResponse) string {
hours := make([]string, len(w.Hourly))
// TODO Make this not ugly and include units
for i, v := range w.Hourly {
// For now only show next 8
// 48 available later
if i > 7 {
break
}
temperature_color := int(v.Temp / 5)
tStyle = tStyle.Foreground(temperature_colors[temperature_color])
rain_color := int(v.Pop * 9)
rStyle := tStyle.Foreground(rain_colors[rain_color])
now := time.Unix(v.Dt, 0).Local()
hours[i] = block.Render(lipgloss.JoinVertical(lipgloss.Center,
now.Format(time.DateOnly),
now.Format(time.Kitchen),
tStyle.Render(fmt.Sprintf("%g°F", v.Temp)),
rStyle.Render(fmt.Sprintf("雨%d%%", int(v.Pop*100))),
))
}
return lipgloss.JoinHorizontal(lipgloss.Center, hours...)
}
/*
func fmtHourlyGraph(w WeatherResponse) string {
dataset := make([]timeserieslinechart.TimePoint, len(w.Hourly))
for i, v := range w.Hourly {
fmt.Println(linechart.DefaultLabelFormatter()(i, float64(v.Dt)))
dataset[i] = timeserieslinechart.TimePoint{
Time: time.Unix(v.Dt, 0).Local(),
Value: v.Temp,
}
}
chart := timeserieslinechart.New(
80,
10,
timeserieslinechart.WithTimeSeries(dataset),
timeserieslinechart.WithXLabelFormatter(func(i int, _ float64) string {
res := time.Unix(w.Hourly[0].Dt, 0).Add(time.Hour * time.Duration(i))
return res.Local().Format(time.Kitchen)
}),
)
chart.DrawBraille()
return block.Render(chart.View())
}
*/
func fmtMinutely(w WeatherResponse) string {
dataset := make([]timeserieslinechart.TimePoint, len(w.Minutely))
empty := true
for i, v := range w.Minutely {
now := time.Unix(v.Dt, 0).Local()
if v.Precipitation != 0 {
empty = false
}
dataset[i] = timeserieslinechart.TimePoint{
Time: now,
Value: v.Precipitation,
}
}
if empty {
return ""
}
tslc := timeserieslinechart.New(
80,
10,
timeserieslinechart.WithXLabelFormatter(func(_ int, d float64) string {
res := (int64(d) - w.Minutely[0].Dt) / 60
return strconv.Itoa(int(res))
}),
timeserieslinechart.WithTimeSeries(dataset),
)
tslc.DrawBraille()
return block.BorderForeground(lipgloss.Blue).Render(
lipgloss.JoinVertical(lipgloss.Center,
bold.Render("Precipitation"),
lipgloss.JoinHorizontal(lipgloss.Center,
"mm/h",
tslc.View(),
),
"minutes",
),
)
}
func fmtCurrent(w WeatherResponse) string {
// TODO This color gradient only handles 0-100
temperature_color := int(w.Current.Temp / 5)
tStyle = tStyle.Foreground(temperature_colors[temperature_color])
var b strings.Builder
// Temperature
fmt.Fprintf(&b, "%s°F (%s°F)",
tStyle.Render(ff(w.Current.Temp)),
tStyle.Render(ff(w.Current.FeelsLike)),
)
temp := block.Render(b.String())
b.Reset()
fmt.Fprintf(&b, "Humidity: %s%%",
bold.Render(strconv.Itoa(w.Current.Humidity)),
)
humidity := block.Render(b.String())
b.Reset()
fmt.Fprintf(&b, "Clouds: %s%%",
bold.Render(strconv.Itoa(w.Current.Clouds)),
)
clouds := block.Render(b.String())
b.Reset()
fmt.Fprintf(&b, "%s° at %s MPH",
bold.Render(strconv.Itoa(w.Current.WindDeg)),
bold.Render(ff(w.Current.WindSpeed)),
)
wind := block.Render(b.String())
b.Reset()
var rain string
if w.Current.Rain != nil {
rain = block.
BorderForeground(lipgloss.Blue).
Render(fmt.Sprintf("Rain:\n%v mm/h", w.Current.Rain.Hour))
b.Reset()
}
var snow string
if w.Current.Snow != nil {
snow = block.
BorderForeground(lipgloss.Blue).
Render(fmt.Sprintf("Snow:\n%v mm/h", w.Current.Snow.Hour))
b.Reset()
}
now := time.Unix(w.Current.Dt, 0).Local()
subheader := fmt.Sprintf("%s with %s", now.Format(time.Kitchen), w.Current.Weather[0].Description)
return lipgloss.JoinVertical(lipgloss.Center,
subheader,
lipgloss.JoinHorizontal(lipgloss.Top,
temp,
humidity,
clouds,
wind,
rain,
snow,
),
)
}
// [f]ormat [f]loat
func ff(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
|