yr/cmd/today.go
Tim Hårek Andreassen 13d9c41af3
refactor: Move output json to helper func
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-10-04 20:51:20 +02:00

53 lines
1 KiB
Go

package cmd
import (
"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 {
outputJson(f.Forecast)
}
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)
}