yr/cmd/today.go

65 lines
1.3 KiB
Go
Raw Normal View History

package cmd
import (
"encoding/json"
"fmt"
"os"
"time"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"github.com/pkg/browser"
"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 {
url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f.Coordinates.Latitude, f.Coordinates.Longitude)
err := browser.OpenURL(url)
if err != nil {
cobra.CheckErr(err)
}
os.Exit(0)
}
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)
}