2024-10-02 18:28:00 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2024-10-03 16:53:14 +00:00
|
|
|
"git.sr.ht/~timharek/yr/cmd/flags"
|
2024-10-03 20:37:54 +00:00
|
|
|
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
|
2024-10-03 14:05:18 +00:00
|
|
|
"github.com/pkg/browser"
|
2024-10-02 18:28:00 +00:00
|
|
|
"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)
|
2024-10-02 18:40:49 +00:00
|
|
|
rootCmd.PersistentFlags().IntP(flags.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-04 18:39:05 +00:00
|
|
|
helper := forecastHelper(cmd, args)
|
|
|
|
f := helper.f
|
2024-10-02 18:28:00 +00:00
|
|
|
|
2024-10-04 18:39:05 +00:00
|
|
|
interval, _ := cmd.Flags().GetInt(flags.INTERVAL)
|
2024-10-02 18:28:00 +00:00
|
|
|
|
2024-10-04 18:39:05 +00:00
|
|
|
if helper.isWeb {
|
2024-10-03 14:05:18 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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-04 18:39:05 +00:00
|
|
|
if helper.isJson {
|
2024-10-02 18:28:00 +00:00
|
|
|
j, err := json.MarshalIndent(f, "", " ")
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
fmt.Printf("%s", j)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-02 18:54:36 +00:00
|
|
|
itemTime := item.Time.Local()
|
2024-10-04 18:39:05 +00:00
|
|
|
if helper.isUTC {
|
2024-10-02 18:54:36 +00:00
|
|
|
itemTime = item.Time
|
|
|
|
}
|
2024-10-02 18:28:00 +00:00
|
|
|
t.Row(
|
2024-10-03 20:48:50 +00:00
|
|
|
itemTime.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)
|
|
|
|
}
|