2024-10-02 18:28:00 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-10-03 20:37:54 +00:00
|
|
|
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
|
2024-10-02 18:28:00 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var forecastCmd = &cobra.Command{
|
|
|
|
Use: "forecast <location>",
|
|
|
|
Aliases: []string{"f", "ls"},
|
2024-10-18 07:28:18 +00:00
|
|
|
Short: "Forecasted weather",
|
2024-10-02 18:28:00 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
Run: forecast,
|
|
|
|
}
|
|
|
|
|
2024-10-17 14:36:54 +00:00
|
|
|
var (
|
|
|
|
interval int
|
|
|
|
)
|
|
|
|
|
2024-10-02 18:28:00 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(forecastCmd)
|
2024-10-17 14:36:54 +00:00
|
|
|
rootCmd.Flags().IntVarP(&interval, "interval", "i", 0, "Number of hours to output")
|
2024-10-02 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func forecast(cmd *cobra.Command, args []string) {
|
2024-10-17 14:36:54 +00:00
|
|
|
f := forecastHelper(args)
|
2024-10-02 18:28:00 +00:00
|
|
|
|
2024-10-17 14:36:54 +00:00
|
|
|
if isWeb {
|
2024-10-04 18:44:57 +00:00
|
|
|
openBrowser("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f)
|
2024-10-03 14:05:18 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 18:40:49 +00:00
|
|
|
if interval > 0 {
|
2024-10-03 14:05:18 +00:00
|
|
|
f.Forecast = f.Forecast[:interval]
|
2024-10-02 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 14:36:54 +00:00
|
|
|
if isJson {
|
2024-10-04 18:51:20 +00:00
|
|
|
outputJson(f.Forecast)
|
2024-10-02 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 20:37:54 +00:00
|
|
|
t := table.New()
|
2024-10-02 18:28:00 +00:00
|
|
|
|
2024-10-03 14:05:18 +00:00
|
|
|
for _, item := range f.Forecast {
|
2024-10-17 14:36:54 +00:00
|
|
|
if !isUTC {
|
2024-10-04 21:38:23 +00:00
|
|
|
item.Time = item.Time.Local()
|
2024-10-02 18:54:36 +00:00
|
|
|
}
|
2024-10-02 18:28:00 +00:00
|
|
|
t.Row(
|
2024-10-04 21:38:23 +00:00
|
|
|
item.Time.Format("Mon, 2 Jan 15:04"),
|
2024-10-02 18:28:00 +00:00
|
|
|
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)
|
|
|
|
}
|