yr/cmd/today.go
Tim Hårek Andreassen e06a647fd4
refactor: Move browser opening to function
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-10-04 20:44:57 +02:00

57 lines
1.1 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"time"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"github.com/spf13/cobra"
)
var todayCmd = &cobra.Command{
Use: "today <location>",
Short: "Get tomorrow's forecasted weather",
Args: cobra.MaximumNArgs(1),
Run: today,
}
func init() {
rootCmd.AddCommand(todayCmd)
}
func today(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args)
f := helper.f
if helper.isWeb {
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
}
if helper.isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
return
}
t := table.New()
today := time.Now()
for _, item := range f.Forecast {
if item.Time.Format(time.DateOnly) != today.Format(time.DateOnly) {
continue
}
itemTime := item.Time.Local()
if helper.isUTC {
itemTime = item.Time
}
t.Row(
itemTime.Format("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)
}