diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-08-25 15:42:55 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-08-25 15:42:55 +0200 |
| commit | 92e1106b91f836339a957312a1ea4449c55114c7 (patch) | |
| tree | ee3726c567054e3f3a4eec692dcf5fb49099318b /internal/config/config.go | |
| parent | e14a7db4ffe3c4e0f15f6b37a980501a8d74d26b (diff) | |
| download | prognosis-92e1106b91f836339a957312a1ea4449c55114c7.tar.gz prognosis-92e1106b91f836339a957312a1ea4449c55114c7.zip | |
Stand alone, document, and let the config show anythingv0.1.1
prognosis no longer reads ~/.wegorc. Falling back to another program's
configuration made it useless without wego installed, and hid the fact that
it has no way to know where you are: location= is now required in its own
config, and the error says so and shows how to set it.
Custom columns. Any field the two Open-Meteo APIs expose can be displayed by
declaring it -- column.birch = air:birch_pollen -- and then naming it in
columns=. The source is explicit because forecast and air-quality are separate
services with separate fields; an air column costs one extra request, made only
when one is declared. Air values merge onto forecast hours by timestamp rather
than array index, since nothing guarantees the two endpoints start at the same
hour and merging by position would shift a column by an hour unnoticed. A value
the API withholds renders blank, not zero: for an allergen those are different
claims.
Pollen selection no longer privileges grass. A species named in pollen= is
shown even at zero, because you named it for a reason; pollen=all shows only
what is present, or the line is six zeroes. Grass had been special-cased, which
forced it on someone allergic to birch while hiding theirs.
Temperature colours are compared in Celsius whatever the display units. In
imperial, 85F -- a mild 29C -- was rendering in the red that means IMGW would
issue a heat warning.
A man page, checked by make lint and installed by make install. The Makefile
gains PREFIX/DESTDIR for packaging, a version stamped into the binary, a
release target that refuses to tag a dirty tree or a version with no changelog
entry, cross-compilation for six platforms, and a pre-push hook.
Also: humidity in the default columns, -weather for output meant for someone
else, -ascii so an SMS stays in GSM-7 rather than dropping to 70-character
UCS-2 segments, and -pollen and -version.
Diffstat (limited to 'internal/config/config.go')
| -rw-r--r-- | internal/config/config.go | 162 |
1 files changed, 147 insertions, 15 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 51d8c27..b2b96cf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,8 +1,8 @@ // Package config reads prognosis' KEY=VALUE configuration file. // -// The format is deliberately the same shape as wego's ~/.wegorc: one KEY=VALUE -// per line, '#' starts a comment, values are never quoted. Parsing it here -// rather than pulling in a config library keeps the binary dependency-free. +// The format is one KEY=VALUE per line, '#' starts a comment, and values are +// never quoted. Parsing it here rather than pulling in a config library keeps +// the binary dependency-free. package config import ( @@ -37,6 +37,22 @@ var columnFields = map[string]string{ "visibility": "visibility", } +// Sources a custom column can draw from. They are separate Open-Meteo APIs with +// different field sets, so a column has to say which one it means. +var validSources = map[string]bool{"forecast": true, "air": true} + +// CustomColumn is a column defined in the config rather than built in, so a user +// can display a field prognosis never anticipated -- an allergen, soil +// temperature, anything the two APIs expose. +type CustomColumn struct { + Source string // "forecast" or "air" + Field string // the API field name, verbatim + Label string // header text; defaults to the column name + Width int // 0 means derive it from the label + Decimals int // digits after the point + Suffix string // appended to the value, e.g. "°" or "%" +} + var ( validIcons = map[string]bool{"nerd": true, "emoji": true, "none": true} validColors = map[string]bool{"auto": true, "always": true, "never": true} @@ -67,6 +83,16 @@ type Config struct { // key, because it describes one invocation rather than a preference. Minimal bool + // PollenExplicit records that the user named the species rather than asking + // for "all". A named species is shown even at zero -- you asked for it, so + // its absence is information -- while "all" shows only what is present, or + // the line would be six zeros of noise. + PollenExplicit bool + + // Custom holds columns declared in the config, keyed by the short name used + // in Columns. + Custom map[string]CustomColumn + // ASCII restricts output to ASCII so an SMS stays in GSM-7 (160 characters // per segment) instead of UCS-2 (70). One degree sign costs more than half // the message. @@ -79,7 +105,7 @@ func Default() Config { return Config{ Hours: 12, Units: "metric", - Columns: []string{"hour", "temp", "feels", "conditions", "mm", "rain"}, + Columns: []string{"hour", "temp", "feels", "conditions", "humidity", "mm", "rain"}, Icons: "nerd", Graph: true, GraphHeight: 5, @@ -87,6 +113,7 @@ func Default() Config { Pollen: append([]string(nil), AllSpecies...), Color: "auto", DisplayLang: "en", + Custom: map[string]CustomColumn{}, } } @@ -108,13 +135,32 @@ func ValidColumns() []string { return names } -// Fields returns the Open-Meteo hourly fields the selected columns need. +// Fields returns the forecast-API hourly fields the selected columns need. // Only what is displayed is requested, so a narrow table costs a small response. func (c Config) Fields() []string { + return c.fieldsFor("forecast") +} + +// AirFields returns the air-quality-API hourly fields custom columns need. It is +// empty unless the config declares one, so the extra request is only made when +// something actually needs it. +func (c Config) AirFields() []string { + return c.fieldsFor("air") +} + +func (c Config) fieldsFor(source string) []string { seen := map[string]bool{} var out []string for _, col := range c.Columns { - f := columnFields[col] + var f string + if cc, ok := c.Custom[col]; ok { + if cc.Source != source { + continue + } + f = cc.Field + } else if source == "forecast" { + f = columnFields[col] + } if f == "" || seen[f] { continue } @@ -138,11 +184,24 @@ func (c Config) Has(column string) bool { // Validate rejects unusable settings, naming the offending value and listing // what would have been accepted. A silently blank column is worse than an error. func (c Config) Validate() error { + for name, cc := range c.Custom { + if _, clash := columnFields[name]; clash { + return fmt.Errorf("column.%s: %q is a built-in column; pick another name", name, name) + } + if cc.Source == "" || cc.Field == "" { + return fmt.Errorf("%q has label/width/decimals but no column.%s = source:field", + name, name) + } + } for _, col := range c.Columns { - if _, ok := columnFields[col]; !ok { - return fmt.Errorf("unknown column %q; valid: %s", - col, strings.Join(ValidColumns(), ", ")) + if _, ok := columnFields[col]; ok { + continue } + if _, ok := c.Custom[col]; ok { + continue + } + return fmt.Errorf("unknown column %q; valid: %s (or declare it: column.%s = air:FIELD)", + col, strings.Join(ValidColumns(), ", "), col) } if !validIcons[c.Icons] { return fmt.Errorf("unknown icons %q; valid: emoji, nerd, none", c.Icons) @@ -214,7 +273,64 @@ func Load(path string) (Config, error) { return cfg, sc.Err() } +// customKey splits "label.birch" into ("label", "birch"). +func customKey(key string) (attr, name string, ok bool) { + attr, name, ok = strings.Cut(key, ".") + if !ok || name == "" { + return "", "", false + } + switch attr { + case "column", "label", "width", "decimals", "suffix": + return attr, name, true + } + return "", "", false +} + +func (c *Config) setCustom(attr, name, value string) error { + if c.Custom == nil { + c.Custom = map[string]CustomColumn{} + } + cc := c.Custom[name] + switch attr { + case "column": + src, field, ok := strings.Cut(value, ":") + if !ok { + return fmt.Errorf("column.%s: expected source:field, got %q; sources: air, forecast", + name, value) + } + src, field = strings.TrimSpace(src), strings.TrimSpace(field) + if !validSources[src] { + return fmt.Errorf("column.%s: unknown source %q; valid: air, forecast", name, src) + } + if field == "" { + return fmt.Errorf("column.%s: no field given after %q:", name, src) + } + cc.Source, cc.Field = src, field + case "label": + cc.Label = value + case "suffix": + cc.Suffix = value + case "width": + n, err := strconv.Atoi(value) + if err != nil || n < 1 { + return fmt.Errorf("width.%s: %q is not a positive number", name, value) + } + cc.Width = n + case "decimals": + n, err := strconv.Atoi(value) + if err != nil || n < 0 || n > 6 { + return fmt.Errorf("decimals.%s: %q is not a number between 0 and 6", name, value) + } + cc.Decimals = n + } + c.Custom[name] = cc + return nil +} + func (c *Config) set(key, value string) error { + if attr, name, ok := customKey(key); ok { + return c.setCustom(attr, name, value) + } switch key { case "location": c.Location = value @@ -237,11 +353,11 @@ func (c *Config) set(key, value string) error { case "pollen": switch value { case "all": - c.Pollen = append([]string(nil), AllSpecies...) + c.Pollen, c.PollenExplicit = append([]string(nil), AllSpecies...), false case "none": - c.Pollen = nil + c.Pollen, c.PollenExplicit = nil, false default: - c.Pollen = splitList(value) + c.Pollen, c.PollenExplicit = splitList(value), true } case "hours": n, err := strconv.Atoi(value) @@ -298,8 +414,8 @@ const template = `# prognosis configuration # One KEY=VALUE per line. '#' starts a comment. Values are not quoted. # Command line flags override everything here. -# Place to query. When empty, location= from ~/.wegorc is used, so prognosis -# and wego never disagree about where you are. +# Place to query: a name, or "lat,lon". Required -- prognosis has no other way +# to know where you are, and will not guess. location=%s # Default span in hours. -n and -d override it. @@ -324,7 +440,11 @@ graph_height=%d # Official IMGW warnings for your powiat (Poland only). warnings=%t -# Pollen species to report, or "all" / "none". +# Which allergens to report, or whether to report any at all. +# none no pollen line +# all every species that has a reading +# birch,mugwort exactly these, always -- even at zero, because a species +# you name is one you react to pollen=%s # Restrict output to ASCII: no degree sign, no diacritics, no block drawing. @@ -334,6 +454,18 @@ ascii=%t # auto (colour when stdout is a terminal) | always | never color=%s +# Columns prognosis does not ship with. Declare a short name against a source +# and a field, then put the name in columns= above. "forecast" is the weather +# API, "air" the air-quality one that carries the allergens; an air column costs +# one extra request, made only when you declare one. See prognosis(1). +# +# column.birch = air:birch_pollen +# column.soil = forecast:soil_temperature_0cm +# label.birch = birch # header; defaults to the name +# width.birch = 6 # defaults to fit the label +# decimals.birch = 1 # digits after the point, default 0 +# suffix.soil = C # appended to the value + # Language for everything prognosis writes itself -- headers, condition names, # labels, dates, pollen species: en | pl. IMGW publishes its warning text in # Polish only, so that text stays Polish whatever this is set to. |
