aboutsummaryrefslogtreecommitdiff
path: root/internal/render/render.go
blob: 7df9b66da77218ef5abc57a883d1d58d4bb35493 (plain) (blame)
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
// Package render turns fetched weather into terminal output. It is pure: no
// network, no clock beyond what it is given, so every rule below is testable.
package render

import (
	"fmt"
	"math"
	"strings"

	"github.com/lukaszkasprzak/prognosis/internal/config"
	"github.com/lukaszkasprzak/prognosis/internal/i18n"
	"github.com/lukaszkasprzak/prognosis/internal/imgw"
	"github.com/lukaszkasprzak/prognosis/internal/openmeteo"
)

// RainInterestPct is the chance of rain below which the mm and rain columns are
// hidden: on a dry day they are a block of zeroes that pushes real content
// sideways.
const RainInterestPct = 20

// View is everything one run needs to render.
type View struct {
	Label      string
	TZ         string
	Rows       []openmeteo.Row
	Sun        map[string][2]string
	Daily      map[string]float64
	Pollen     map[string]float64
	Warnings   []imgw.Warning
	WarnNote   string
	WarnFailed bool
}

type ctx struct {
	cfg   config.Config
	cat   *i18n.Catalog
	c     func(string, string) string
	width int
	wet   bool
}

// celsius converts a displayed temperature back to Celsius.
//
// IMGW's thresholds are defined in Celsius, so they must be compared in
// Celsius: a warning threshold does not move because the display was switched
// to Fahrenheit. Without this, 85F (a mild 29C) renders in the "IMGW would warn
// about this" red.
func (x ctx) celsius(v float64) float64 {
	if x.cfg.Units == "imperial" {
		return (v - 32) * 5 / 9
	}
	return v
}

// Render produces the whole output.
func Render(v View, cfg config.Config, width int, colour bool) string {
	cx := ctx{
		cfg:   cfg,
		cat:   i18n.For(cfg.DisplayLang),
		c:     Styler(colour),
		width: width,
		wet:   isWet(v.Rows),
	}
	var out []string
	out = append(out, cx.warnings(v)...)
	out = append(out, cx.header(v)...)
	out = append(out, cx.table(v)...)
	if cfg.Graph {
		out = append(out, cx.chart(v)...)
	}
	text := strings.Join(out, "\n")
	// Applied last, one character for one, so the layout above is unaffected.
	if cfg.ASCII {
		text = ASCII(text, cfg.Units)
	}
	return text
}

func isWet(rows []openmeteo.Row) bool {
	for _, r := range rows {
		if mm, ok := r.Val("precipitation"); ok && mm > 0 {
			return true
		}
		if p, ok := r.Val("precipitation_probability"); ok && p >= RainInterestPct {
			return true
		}
	}
	return false
}

// warnings renders IMGW warnings, or an explicit line saying why none are shown.
//
// WarnFailed must never look the same as "none in force": silence would be read
// as all-clear.
func (x ctx) warnings(v View) []string {
	var out []string
	switch {
	case x.WarnDisabled():
		return nil
	case v.WarnFailed:
		out = append(out, x.c(Dim, " "+x.cat.Word("warnings")+": "+x.cat.Word("could_not_check")))
	case v.WarnNote != "":
		out = append(out, x.c(Dim, " "+x.cat.Word("warnings")+": "+v.WarnNote))
	}
	for _, w := range v.Warnings {
		style := Yellow
		switch w.Level {
		case "2":
			style = Red
		case "3":
			style = Bold + ";" + Red
		}
		head := fmt.Sprintf(" ! %s  %s %s  %s -> %s  (%s%%)",
			w.Event, x.cat.Word("level"), w.Level,
			clip(w.From), clip(w.To), w.Probability)
		out = append(out, x.c(style, head))
		for _, line := range wrap(w.Text, x.width) {
			out = append(out, x.c(Dim, "   "+line))
		}
	}
	if len(out) > 0 {
		out = append(out, "")
	}
	return out
}

// WarnDisabled reports whether warnings were switched off in config.
func (x ctx) WarnDisabled() bool { return !x.cfg.Warnings }

// clip shortens "2026-08-10 11:00:00" to "08-10 11:00".
func clip(s string) string {
	if len(s) >= 16 {
		return s[5:16]
	}
	return s
}

func (x ctx) header(v View) []string {
	var out []string
	first := v.Rows[0].When
	title := v.Label + "   " + x.cat.Date(first)
	if x.cfg.Minimal {
		// Just the place and the date: no sun times, no summary, no pollen.
		return append(out, x.c(Bold, title))
	}
	sun, hasSun := v.Sun[first.Format("2006-01-02")]
	suffix := ""
	if hasSun {
		suffix = fmt.Sprintf("   %s %s  %s %s",
			sun[0], x.cat.Word("up"), sun[1], x.cat.Word("down"))
	}
	tz := ""
	if v.TZ != "" {
		tz = "  " + v.TZ
	}
	// Keep it to one line where it fits; a phone is narrow enough that it often
	// does not, and a wrapped title reads worse than two deliberate lines.
	if hasSun && DisplayWidth(title)+DisplayWidth(suffix)+DisplayWidth(tz) <= x.width {
		out = append(out, x.c(Bold, title)+x.c(Dim, suffix+tz))
	} else {
		out = append(out, x.c(Bold, title)+x.c(Dim, tz))
		if hasSun {
			out = append(out, x.c(Dim, fmt.Sprintf(" %s  %s %s   %s %s",
				x.cat.Word("sun"), sun[0], x.cat.Word("up"), sun[1], x.cat.Word("down"))))
		}
	}

	if len(v.Daily) > 0 {
		var bits []string
		lo, okLo := v.Daily["temperature_2m_min"]
		hi, okHi := v.Daily["temperature_2m_max"]
		if okLo && okHi {
			bits = append(bits, fmt.Sprintf("%d-%d°", Deg(lo), Deg(hi)))
		}
		if mm, ok := v.Daily["precipitation_sum"]; ok {
			if mm == 0 {
				bits = append(bits, x.cat.Word("dry"))
			} else {
				bits = append(bits, fmt.Sprintf("%s %.1fmm %s %.0fh",
					x.cat.Word("rainfall"), mm, x.cat.Word("over"),
					v.Daily["precipitation_hours"]))
			}
		}
		if sun, ok := v.Daily["sunshine_duration"]; ok {
			if day, ok2 := v.Daily["daylight_duration"]; ok2 {
				bits = append(bits, fmt.Sprintf("%s %s %s %s %s",
					x.cat.Word("sun"), hm(sun), x.cat.Word("of"), hm(day),
					x.cat.Word("daylight")))
			}
		}
		label := " " + Pad(x.cat.Word("day"), 6) + " "
		// Joined with wide separators when it fits; only a line too long for the
		// terminal is re-wrapped, and then on single spaces.
		joined := strings.Join(bits, "   ")
		lines := []string{joined}
		if DisplayWidth(label)+DisplayWidth(joined) > x.width {
			lines = wrap(joined, x.width-DisplayWidth(label))
		}
		for i, line := range lines {
			prefix := label
			if i > 0 {
				prefix = strings.Repeat(" ", DisplayWidth(label))
			}
			out = append(out, x.c(Dim, prefix+line))
		}
	}

	if len(v.Pollen) > 0 {
		var bits []string
		for _, s := range sortedByValue(v.Pollen) {
			band := PollenBand(s, v.Pollen[s])
			// A species the user named is always shown, even at zero: they named
			// it because they react to it, and "none today" is what they wanted
			// to know. With pollen=all nobody chose, so absent taxa are dropped
			// rather than printing a line of zeroes.
			if band == "none" && !x.cfg.PollenExplicit {
				continue
			}
			text := fmt.Sprintf("%s %.1f", x.cat.Species(s), v.Pollen[s])
			if band != "" {
				text += " " + x.cat.Band(band)
			}
			bits = append(bits, text)
		}
		if len(bits) > 0 {
			if len(bits) > 4 {
				bits = bits[:4]
			}
			out = append(out, x.c(Dim, " "+Pad(x.cat.Word("pollen"), 6)+" ")+strings.Join(bits, "  "))
		}
	}
	return out
}

func hm(seconds float64) string {
	s := int(seconds)
	return fmt.Sprintf("%dh%02dm", s/3600, (s%3600)/60)
}

func sortedByValue(m map[string]float64) []string {
	keys := make([]string, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	for i := 1; i < len(keys); i++ {
		for j := i; j > 0 && m[keys[j]] > m[keys[j-1]]; j-- {
			keys[j], keys[j-1] = keys[j-1], keys[j]
		}
	}
	return keys
}

func wrap(text string, width int) []string {
	if width < 8 {
		width = 8
	}
	var lines []string
	var line string
	for _, word := range strings.Fields(text) {
		switch {
		case line == "":
			line = word
		case DisplayWidth(line)+1+DisplayWidth(word) <= width:
			line += " " + word
		default:
			lines = append(lines, line)
			line = word
		}
	}
	if line != "" {
		lines = append(lines, line)
	}
	return lines
}

func round1(v float64) string { return fmt.Sprintf("%.1f", v) }

func compass(deg float64) string {
	dirs := []string{"↓", "↙", "←", "↖", "↑", "↗", "→", "↘"}
	i := int(math.Mod(math.Round(deg/45), 8))
	if i < 0 {
		i += 8
	}
	return dirs[i]
}