aboutsummaryrefslogtreecommitdiff
path: root/internal/render/ascii.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-08-13 13:04:10 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-08-13 13:04:10 +0200
commite14a7db4ffe3c4e0f15f6b37a980501a8d74d26b (patch)
tree670ef0897839871a64d3a3bb2e17e242e7d6c385 /internal/render/ascii.go
downloadprognosis-e14a7db4ffe3c4e0f15f6b37a980501a8d74d26b.tar.gz
prognosis-e14a7db4ffe3c4e0f15f6b37a980501a8d74d26b.zip
Initial commit: prognosis, the Go implementation
An hour-by-hour forecast for the terminal, with official IMGW warnings for Polish locations. Replaces the Python version, whose cache file format it keeps so the two can coexist until this reaches parity. Open-Meteo provides the forecast, geocoding and pollen; GUGiK turns coordinates into a TERYT powiat code; IMGW supplies the warnings, filtered to that powiat rather than the whole country. Only the two lookups that never change are cached. Forecasts never are. Silence is never allowed to read as all-clear: "no warnings in force" and "the check failed" are reported as distinct states. Place names are resolved without guessing. A name matching several places is refused with a numbered list carrying each candidate's region and coordinates, and -pick N chooses one and remembers it. A stray positional beside -l is an error, so an unquoted "Wiry, PL" cannot silently resolve to somewhere else. The cache is written one entry per line with sorted keys, and treated as disposable but not worthless: an entry that will not parse is skipped and the rest kept, and a file that will not parse at all is moved to cache.json.bad rather than overwritten. No third-party dependencies. `make ci` is the gate: gofmt clean, vet, tests.
Diffstat (limited to 'internal/render/ascii.go')
-rw-r--r--internal/render/ascii.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/internal/render/ascii.go b/internal/render/ascii.go
new file mode 100644
index 0000000..4203bc9
--- /dev/null
+++ b/internal/render/ascii.go
@@ -0,0 +1,67 @@
+package render
+
+import "strings"
+
+// asciiMap is deliberately one rune to one ASCII character.
+//
+// Substitution happens after the table has been laid out, so anything that
+// changed the number of characters would shear every column to its right. That
+// constraint is why the wind arrows become single letters rather than the
+// two-letter compass points that would read better.
+var asciiMap = map[rune]string{
+ // Polish diacritics, so a Polish forecast survives GSM-7.
+ 'ą': "a", 'ć': "c", 'ę': "e", 'ł': "l", 'ń': "n",
+ 'ó': "o", 'ś': "s", 'ź': "z", 'ż': "z",
+ 'Ą': "A", 'Ć': "C", 'Ę': "E", 'Ł': "L", 'Ń': "N",
+ 'Ó': "O", 'Ś': "S", 'Ź': "Z", 'Ż': "Z",
+
+ // Wind arrows. The arrow points the way the wind blows, so v is a northerly.
+ '↑': "^", '↓': "v", '←': "<", '→': ">",
+ '↖': "\\", '↗': "/", '↙': "/", '↘': "\\",
+
+ // Chart: blocks and the axis rule.
+ '█': "#", '▇': "#", '▆': "=", '▅': "=",
+ '▄': "_", '▃': ":", '▂': ".", '▁': ".",
+ '│': "|",
+}
+
+// ASCII rewrites output to pure ASCII, one character for one character.
+//
+// The point is SMS: a single non-ASCII character forces the whole message from
+// GSM-7 into UCS-2, which cuts a segment from 160 characters to 70. A degree
+// sign alone therefore more than doubles the cost of sending a forecast.
+//
+// The degree sign becomes the unit letter, which is both ASCII and clearer to
+// someone reading it cold: "29C" rather than "29".
+func ASCII(s, units string) string {
+ degree := "C"
+ if units == "imperial" {
+ degree = "F"
+ }
+ runes := []rune(s)
+ var b strings.Builder
+ b.Grow(len(s))
+ for i, r := range runes {
+ switch {
+ case r == '°':
+ // Prose from IMGW already writes "30°C"; appending the unit again
+ // would give "30CC". Only a bare degree sign gains the letter.
+ if i+1 < len(runes) && (runes[i+1] == 'C' || runes[i+1] == 'F') {
+ continue
+ }
+ b.WriteString(degree)
+ case r < 128:
+ b.WriteRune(r)
+ default:
+ if sub, ok := asciiMap[r]; ok {
+ b.WriteString(sub)
+ } else {
+ // Anything unmapped -- a place name in another script, a weather
+ // code description we have not transliterated -- becomes '?'
+ // rather than silently vanishing and misaligning the row.
+ b.WriteString("?")
+ }
+ }
+ }
+ return b.String()
+}