Skip to content

Commit

Permalink
Add hourly rain forecast sparklines (#39)
Browse files Browse the repository at this point in the history
Updates #38
  • Loading branch information
FiloSottile authored and jessfraz committed Jan 24, 2018
1 parent 105a948 commit f0c9afe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
44 changes: 43 additions & 1 deletion forecast/print.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package forecast

import (
"bytes"
"fmt"
"math"
"strings"
Expand Down Expand Up @@ -75,6 +76,16 @@ func epochFormatTime(seconds int64) string {
return epochTime.Format("3:04pm MST")
}

func epochFormatHour(seconds int64) string {
epochTime := time.Unix(0, seconds*int64(time.Second))
s := epochTime.Format("3pm")
s = s[:len(s)-1]
if len(s) == 2 {
s += " "
}
return s
}

func getIcon(iconStr string) (icon string, err error) {
color := "blue"
// steralize the icon string name
Expand Down Expand Up @@ -220,7 +231,38 @@ func PrintCurrent(forecast Forecast, geolocation geocode.Geocode, ignoreAlerts b
}
}

return printCommon(forecast.Currently, unitsFormat)
if err := printCommon(forecast.Currently, unitsFormat); err != nil {
return err
}

if forecast.Hourly.Summary != "" {
fmt.Printf("%s\n\n", forecast.Hourly.Summary)

var ticks = []rune(" ▁▂▃▄▅▆▇█")
rainForecast, showRain := &bytes.Buffer{}, false
for i := 0; i < 16; i++ {
p := forecast.Hourly.Data[i].PrecipProbability
t := int(p*float64(len(ticks)-2)) + 1
if p == 0 {
t = 0
} else {
showRain = true
}
rainForecast.WriteRune(ticks[t])
rainForecast.WriteRune(ticks[t])
rainForecast.WriteRune(' ')
}
if showRain {
fmt.Printf("Rain chance: %s\n", rainForecast)
fmt.Printf(" ")
for i := 0; i < 4; i++ {
fmt.Printf("%s ", epochFormatHour(forecast.Hourly.Data[i*4].Time))
}
fmt.Printf("\n\n")
}
}

return nil
}

// PrintDaily pretty prints the daily forecast data.
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
days int
ignoreAlerts bool
hideIcon bool
noForecast bool
server string
vrsn bool
client bool
Expand Down Expand Up @@ -50,6 +51,7 @@ func init() {
flag.IntVar(&days, "d", 0, "No. of days to get forecast (shorthand)")
flag.BoolVar(&ignoreAlerts, "ignore-alerts", false, "Ignore alerts in weather output")
flag.BoolVar(&hideIcon, "hide-icon", false, "Hide the weather icons from being output")
flag.BoolVar(&noForecast, "no-forecast", false, "Hide the forecast for the next 16 hourse")

flag.Usage = func() {
flag.PrintDefaults()
Expand Down Expand Up @@ -108,7 +110,10 @@ func main() {
Latitude: geo.Latitude,
Longitude: geo.Longitude,
Units: units,
Exclude: []string{"hourly", "minutely"},
Exclude: []string{"minutely"},
}
if noForecast {
data.Exclude = append(data.Exclude, "hourly")
}

fc, err := forecast.Get(fmt.Sprintf("%s/forecast", server), data)
Expand Down

0 comments on commit f0c9afe

Please sign in to comment.