yr/cmd/forecast.go
Fredrik Kvalvik 9449cba68d
style: right align cells for better readability
improve readability by right-aligning text. This
makes reading a mix of positive/negative numbers,
or 1/2 digit numbers easier.

This also makes it a bit easier to parse a longer
forecast when it spans mulitple days, as the time
stays in the same place.
2024-11-25 19:27:09 +01:00

57 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"github.com/spf13/cobra"
)
var forecastCmd = &cobra.Command{
Use: "forecast <location>",
Aliases: []string{"f", "ls"},
Short: "Forecasted weather",
Args: cobra.MaximumNArgs(1),
Run: forecast,
}
var (
interval int
)
func init() {
rootCmd.AddCommand(forecastCmd)
forecastCmd.Flags().IntVarP(&interval, "interval", "i", 0, "Number of hours to output")
}
func forecast(cmd *cobra.Command, args []string) {
f := forecastHelper(args)
if isWeb {
openBrowser("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f)
}
if interval > 0 {
f.Forecast = f.Forecast[:interval]
}
if isJson {
outputJson(f.Forecast)
}
t := table.New()
for _, item := range f.Forecast {
if !isUTC {
item.Time = item.Time.Local()
}
t.Row(
item.Time.Format("Mon, 2 Jan 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)
}