feat: Add now --oneline

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-04 21:00:06 +02:00
parent 13d9c41af3
commit 992a167c12
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
2 changed files with 21 additions and 5 deletions

View file

@ -7,4 +7,5 @@ const (
INTERVAL = "interval"
UTC = "utc"
WEB = "web"
ONE_LINE = "oneline"
)

View file

@ -2,7 +2,9 @@ package cmd
import (
"fmt"
"os"
"git.sr.ht/~timharek/yr/cmd/flags"
"github.com/spf13/cobra"
)
@ -16,12 +18,16 @@ var nowCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(nowCmd)
rootCmd.PersistentFlags().Bool(flags.ONE_LINE, false, "Output in a single line")
}
func now(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args)
f := helper.f
isOneLine, err := cmd.Flags().GetBool(flags.ONE_LINE)
cobra.CheckErr(err)
if helper.isWeb {
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
}
@ -36,12 +42,21 @@ func now(cmd *cobra.Command, args []string) {
if helper.isUTC {
itemTime = n.Time
}
if isOneLine {
fmt.Printf("%s %s: %.1f °C, %.1f mm, %.1f m/s from %s\n",
n.Location,
itemTime.Format("Monday 2 Jan 15:04"),
n.Temperature,
n.Percipitation,
n.Wind.Speed,
n.Wind.DirectionToString(),
)
os.Exit(0)
}
fmt.Printf(
`%s %s:
Temperature: %.1f °C
Rain: %.1f mm
Wind: %.1f m/s from %s
`,
`%s %s: Temperature: %.1f °C Rain: %.1f mm Wind: %.1f m/s from %s `,
n.Location,
itemTime.Format("Monday 2 Jan 15:04"),
n.Temperature,