Render dynamic weather for ASCII landscapes. Inspired and powered by ~iajrz's
climate program.
1package main
2
3import (
4 "os/exec"
5 "strings"
6)
7
8// Forecast is a representation of the current state of the weather.
9type Forecast struct {
10 raw string
11 time TimeOfDay
12 cloudiness Cloudiness
13 raininess Raininess
14 visibility Visibility
15 windiness Windiness
16}
17
18// NewForecast parses the output of ~iajrz's climate program.
19// TODO?: boolean randomize option to generate completely random weather as a demo mode?
20func NewForecast() (Forecast, error) {
21 out, err := exec.Command("/home/iajrz/climate").Output()
22 if err != nil {
23 return Forecast{}, err
24 }
25 rawWeather := string(out)
26 return Forecast{
27 raw: rawWeather,
28 time: TimeOfDay(findSubstring(rawWeather, timeStrings)),
29 cloudiness: Cloudiness(findSubstring(rawWeather, cloudStrings)),
30 raininess: Raininess(findSubstring(rawWeather, rainStrings)),
31 visibility: Visibility(findSubstring(rawWeather, visibilityStrings)),
32 windiness: Windiness(findSubstring(rawWeather, windStrings)),
33 }, nil
34}
35
36func (f Forecast) String() string {
37 return f.raw
38}
39
40func findSubstring(s string, substrings []string) int {
41 for i := range substrings {
42 if strings.Contains(s, substrings[i]) {
43 return i
44 }
45 }
46 return 0
47}
48
49type TimeOfDay int
50
51const (
52 EarlyMorning TimeOfDay = iota
53 Morning
54 Afternoon
55 Night
56)
57
58var timeStrings = []string{
59 "early morning",
60 "morning",
61 "afternoon",
62 "night",
63}
64
65type Cloudiness int
66
67const (
68 ClearSky Cloudiness = iota
69 AlmostClear
70 PartlyCloudy
71 MostlyCloudy
72 Cloudy
73)
74
75var cloudStrings = []string{
76 "clear",
77 "almost clear",
78 "partly cloudy",
79 "mostly cloudy",
80 "cloudy",
81}
82
83type Raininess int
84
85const (
86 NoRain Raininess = iota
87 Drizzle
88 LightShower
89 Shower
90 HeavyShower
91)
92
93var rainStrings = []string{
94 "no rain",
95 "a drizzle",
96 "a light shower",
97 "a shower",
98 "a heavy shower",
99}
100
101type Visibility int
102
103const (
104 NoFog Visibility = iota
105 Haze
106 Mist
107 Fog
108 HeavyFog
109)
110
111var visibilityStrings = []string{
112 "visibility",
113 "There's haze",
114 "There's mist",
115 "There's fog",
116 "There's heavy fog",
117}
118
119type Windiness int
120
121const (
122 NoWind Windiness = iota
123 Breeze
124 StiffWind
125)
126
127var windStrings = []string{
128 "no breeze",
129 "light breeze",
130 "stiff wind",
131}