2024-10-03 20:37:54 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
|
2024-10-17 17:31:58 +00:00
|
|
|
"git.sr.ht/~timharek/yr/pkg/browser"
|
2024-10-03 20:37:54 +00:00
|
|
|
"git.sr.ht/~timharek/yr/yr"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tomorrowCmd = &cobra.Command{
|
|
|
|
Use: "tomorrow <location>",
|
|
|
|
Aliases: []string{"twm", "trm"},
|
|
|
|
Short: "Get tomorrow's forecasted weather",
|
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
Run: tomorrow,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(tomorrowCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tomorrow(cmd *cobra.Command, args []string) {
|
2024-10-17 14:36:54 +00:00
|
|
|
f := forecastHelper(args)
|
2024-10-03 20:37:54 +00:00
|
|
|
|
2024-10-17 14:36:54 +00:00
|
|
|
if isWeb {
|
2024-10-03 20:37:54 +00:00
|
|
|
url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=1", f.Coordinates.Latitude, f.Coordinates.Longitude)
|
|
|
|
err := browser.OpenURL(url)
|
|
|
|
if err != nil {
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
t := table.New()
|
|
|
|
|
2024-10-04 21:38:23 +00:00
|
|
|
tomorrows := []yr.Forecast{}
|
|
|
|
|
2024-10-03 20:37:54 +00:00
|
|
|
tomorrow := time.Now().AddDate(0, 0, 1)
|
|
|
|
for _, item := range f.Forecast {
|
|
|
|
if item.Time.Format(time.DateOnly) != tomorrow.Format(time.DateOnly) {
|
|
|
|
continue
|
|
|
|
}
|
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-03 20:37:54 +00:00
|
|
|
}
|
2024-10-04 21:38:23 +00:00
|
|
|
tomorrows = append(tomorrows, item)
|
2024-10-03 20:37:54 +00:00
|
|
|
t.Row(
|
2024-10-04 21:38:23 +00:00
|
|
|
fmt.Sprintf("tomorrow %s", item.Time.Format("15:04")),
|
2024-10-03 20:37:54 +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()),
|
|
|
|
)
|
|
|
|
}
|
2024-10-04 21:38:23 +00:00
|
|
|
|
2024-10-17 14:36:54 +00:00
|
|
|
if isJson {
|
2024-10-04 21:38:23 +00:00
|
|
|
j, err := json.MarshalIndent(tomorrows, "", " ")
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
fmt.Printf("%s", j)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-03 20:37:54 +00:00
|
|
|
fmt.Println(t)
|
|
|
|
}
|