From cea7b560d0275186fcaf6f18c91c6c68849b069c Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Thu, 27 Feb 2020 00:15:41 +0100 Subject: [PATCH] Welcome the --one-liner flag, terminal multiplexer lovers * Adding a new --one-liner flag that will return the weather on one line * Updating README with usage and tmux configuration example --- README.md | 31 ++++++++++++++++++++++++++++++- main.go | 12 ++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cf1d05f..715a3f0 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,38 @@ Flags: -f, --format=FORMAT Query format (overrides everything else) -L, --location=LOCATION Specify explicite location -l, --language=LANGUAGE Specify explicite language + -O, --one-liner One liner outpet (for the terminal multiplexer lovers out there) --extra-information Print extra information --version Show application version. ``` On top of the following command-line flags, `go-cmw` can also take advantage of environment variables. -`go-cmw` can take advantage of `GO_CMW_FORMAT`, `GO_CMW_LOCATION` and `GO_CMW_LANGUAGE` environment variables if set. \ No newline at end of file +`go-cmw` can take advantage of `GO_CMW_FORMAT`, `GO_CMW_LOCATION` and `GO_CMW_LANGUAGE` environment variables if set. + +```console +$ go-cmw --zero +Weather report: Paris + + \ / Clear + .-. 1..4 °C + ― ( ) ― ↗ 11 km/h + `-’ 10 km + / \ 0.3 mm +``` + +`go-cmw` makes it easier to integrate the weather with your favorite terminal multiplexer. + +```console +$ go-cmw --one-liner +Paris: ☀️ +4°C +``` + +Example configuration for `tmux`. + +```console +# Let us update sensibly every 30 minutes +set -g status-interval 1800 + +WEATHER='#(go-cmw --one-liner)' +set -g status-right '... $WEATHER ...' +``` \ No newline at end of file diff --git a/main.go b/main.go index 1718618..af7b35a 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ const ( ADDFRAME MIDTRANSPARENCY FORMAT + ONELINER LOCATION LANGUAGE PNG @@ -114,7 +115,7 @@ func (w weather) print(resp *http.Response) { if err != nil { log.Fatal("Error reading body. ", err) } - fmt.Printf("%s\n", body) + fmt.Printf("%s", body) } // `weather` method to download the weather @@ -161,7 +162,8 @@ func (cmdP *cmdParams) generateParamFormat() (string, bool, error) { affix = append(affix, "?") // If --format is specified, let's not bother and simply return it - if cmdP.Flags&FORMAT == FORMAT { + // If --one-liner is specified, we know the format is set so let's return it + if cmdP.Flags&FORMAT == FORMAT || cmdP.Flags&ONELINER == ONELINER { return cmdP.Format, false, nil } @@ -260,6 +262,7 @@ var ( format = app.Flag("format", "Query format (overrides everything else)").Short('f').OverrideDefaultFromEnvar("GO_CMW_FORMAT").String() location = app.Flag("location", "Specify explicite location").Short('L').OverrideDefaultFromEnvar("GO_CMW_LOCATION").String() language = app.Flag("language", "Specify explicite language").Short('l').OverrideDefaultFromEnvar("GO_CMW_LANGUAGE").String() + oneLiner = app.Flag("one-liner", "One liner outpet (for the terminal multiplexer lovers out there)").Short('O').Default("false").Bool() extraInformation = app.Flag("extra-information", "Print extra information").Default("false").Bool() ) @@ -394,6 +397,11 @@ func flagParser(cmdP *cmdParams) { cmdP.Language = *language } + if *oneLiner { + cmdP.Flags += ONELINER + cmdP.Format = "?format=3" + } + } // Create a function to generate the url that we'll be calling shortly.