From e2b29a75f139e163872dee499626596c754fc566 Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 22 Mar 2018 16:23:43 +0000 Subject: Add -n flag, and slightly improve output --- weather.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/weather.go b/weather.go index 1c51a80..d55297b 100644 --- a/weather.go +++ b/weather.go @@ -5,7 +5,6 @@ package main // TODO: split out met office specific stuff to separate module // TODO: add other weather providers // TODO: add -v verbose flag to show all data -// TODO: add -n flag to return a certain number of days import ( "encoding/json" @@ -59,6 +58,8 @@ type WeatherParams struct { WT int // Weather Type } +var numdays = flag.Int("n", 2, "number of days to show") + func main() { var r Response var id string @@ -82,12 +83,16 @@ func main() { log.Fatal(err) } - for i := len(r.BestFcst.Forecast.Location.Days) - 1; i >= 0; i-- { - d := r.BestFcst.Forecast.Location.Days[i] - fmt.Printf("-----------------------------\n") + for i, d := range r.BestFcst.Forecast.Location.Days { + if *numdays > 0 && i >= *numdays { + break + } + if i > 0 { + fmt.Printf("---------------------------------------------------------\n") + } fmt.Printf("%s\n", d.Date) - prettyWeather(" Day ", d.DayValues.WeatherParameters) - prettyWeather(" Night ", d.NightValues.WeatherParameters) + prettyWeather(" Day", d.DayValues.WeatherParameters) + prettyWeather(" Night", d.NightValues.WeatherParameters) for _, t := range d.TimeSteps.TimeStep { prettyWeather(t.Time, t.WeatherParameters) -- cgit v1.2.3