From 992a167c12776a36da388c3836a7d5e2f86f0ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Fri, 4 Oct 2024 21:00:06 +0200 Subject: [PATCH] feat: Add `now --oneline` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim Hårek Andreassen --- cmd/flags/flags.go | 1 + cmd/now.go | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index babf6d6..000c993 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -7,4 +7,5 @@ const ( INTERVAL = "interval" UTC = "utc" WEB = "web" + ONE_LINE = "oneline" ) diff --git a/cmd/now.go b/cmd/now.go index f2418ca..9d76de9 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -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,