7f72ae918b
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.sr.ht/~timharek/yr/cmd/flags"
|
|
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
|
|
"github.com/pkg/browser"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var forecastCmd = &cobra.Command{
|
|
Use: "forecast <location>",
|
|
Aliases: []string{"f", "ls"},
|
|
Short: "Get forecasted weather",
|
|
Args: cobra.MaximumNArgs(1),
|
|
Run: forecast,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(forecastCmd)
|
|
rootCmd.PersistentFlags().IntP(flags.INTERVAL, "i", 0, "Number of hours to output")
|
|
}
|
|
|
|
func forecast(cmd *cobra.Command, args []string) {
|
|
helper := forecastHelper(cmd, args)
|
|
f := helper.f
|
|
|
|
interval, _ := cmd.Flags().GetInt(flags.INTERVAL)
|
|
|
|
if helper.isWeb {
|
|
url := fmt.Sprintf("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f.Coordinates.Latitude, f.Coordinates.Longitude)
|
|
err := browser.OpenURL(url)
|
|
if err != nil {
|
|
cobra.CheckErr(err)
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
if interval > 0 {
|
|
f.Forecast = f.Forecast[:interval]
|
|
}
|
|
|
|
if helper.isJson {
|
|
j, err := json.MarshalIndent(f, "", " ")
|
|
cobra.CheckErr(err)
|
|
fmt.Printf("%s", j)
|
|
return
|
|
}
|
|
|
|
t := table.New()
|
|
|
|
for _, item := range f.Forecast {
|
|
itemTime := item.Time.Local()
|
|
if helper.isUTC {
|
|
itemTime = item.Time
|
|
}
|
|
t.Row(
|
|
itemTime.Format("Mon, 2 Jan 15:04"),
|
|
fmt.Sprintf("%.1f °C", item.Temperature),
|
|
fmt.Sprintf("%.1f mm", item.Percipitation),
|
|
fmt.Sprintf("%.1f m/s %s", item.Wind.Speed, item.Wind.DirectionToString()),
|
|
)
|
|
}
|
|
fmt.Println(t)
|
|
}
|