[StumpWM, bin] Add weather module to StumpWM, convert we to POSIX sh

Convert script `we` from fish to POSIX shell

Add two new scripts:
- stump-choose-city
- stump-weather
The former one sets the default city used for weather. The latter
generates the output used by StumpWM’s mode-line.
This commit is contained in:
Lucien Cartier-Tilet 2022-05-06 23:27:01 +02:00
parent 46bae114e3
commit d78be92786
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
2 changed files with 86 additions and 26 deletions

View File

@ -2044,18 +2044,77 @@ CACHEFILE=$([ -n "$XDG_CACHE_HOME" ] && echo "$XDG_CACHE_HOME/wallpaper" || echo
* Weather * Weather
:PROPERTIES: :PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/.local/bin/we
:CUSTOM_ID: Weather-4ed00bb0 :CUSTOM_ID: Weather-4ed00bb0
:END: :END:
A quick and useful script I often use is a ~curl~ request to [[http://v2.wttr.in/][v2.wttr.in]] to get a ** StumpWM utilities
weather forecast in the terminal. By default, I want the request to be about the :PROPERTIES:
city I live in, but it is also possible for the script to accept as its :CUSTOM_ID: Weather-StumpWM-utilities-okvit9r05gj0
arguments a search inquiry. :END:
#+BEGIN_SRC fish *** ~stump-choose-city~
if count $argv > /dev/null :PROPERTIES:
set -l SEARCH (string join '+' $argv) :CUSTOM_ID: Weather-StumpWM-utilities-stump-choose-city-adyit9r05gj0
curl http://v2.wttr.in/~$SEARCH :HEADER-ARGS: :shebang "#!/usr/bin/env sh" :mkdirp yes :tangle ~/.local/bin/stump-choose-city
:END:
This script does two things. First, I get to choose a location which
weather will be displayed in StumpWMs mode-line. The script stores
the selected location in the file ~$HOME/.cache/weather-city~, but only
if the input is not an empty string, otherwise nothing will change.
Secondly, if this change is applied, ~stump-weather~ (described below)
is called in order to update what StumpWM displays.
#+begin_src sh
CITY=$(rofi -p "City:" -dmenu | sed -r 's/ +/\+/g')
if [ -n "$CITY" ]; then
printf "%s" "$CITY" > "$HOME"/.cache/weather-city
stump-weather > /dev/null 2>&1
fi
#+end_src
*** ~stump-weather~
:PROPERTIES:
:CUSTOM_ID: Weather-StumpWM-utilities-stump-weather-i21jt9r05gj0
:HEADER-ARGS: :shebang "#!/usr/bin/env sh" :mkdirp yes :tangle ~/.local/bin/stump-weather
:END:
This stript is quite simple: it displays some weather information in
the format /Location: weather temperature (felt temperature)/. We use
the content of the file ~$HOME/.cache/weather-city~ to know which
location to use in our request to ~wttr.in~. If ~wttr.in~ is unreachable
after two second, the request aborts and the script displays an error
message instead.
#+begin_src sh
CITY=$(cat "$HOME/.cache/weather-city")
WEATHER=$(curl wttr.in/~"${CITY}"?format="${CITY}:+%C+%t+(%f)" --connect-timeout 1 2> /dev/null)
if [ -n "$WEATHER" ]; then
printf "%s" "$WEATHER"
else else
curl http://v2.wttr.in/Aubervilliers printf "wttr.in unreachable"
end fi
#+end_src
** ~we~
:PROPERTIES:
:CUSTOM_ID: Weather-we-514jt9r05gj0
:HEADER-ARGS: :shebang "#!/usr/bin/env sh" :mkdirp yes :tangle ~/.local/bin/we
:END:
A quick and useful script I often use is a ~curl~ request to [[http://v2.wttr.in/][v2.wttr.in]]
to get a weather forecast in the terminal. If the user passes no
arguments to ~we~, the script will use the location designated in
~$HOME/.cache/weather-city~. Otherwise, it will use the options passed
by the user.
#+BEGIN_SRC sh
COUNT=0
LOCATION=""
if [ "$#" = 0 ]; then
LOCATION=$(cat "$HOME"/.cache/weather-city)
fi
if [ -z "$LOCATION" ]; then
for ARG in $*; do
if [ "${COUNT}" = 0 ]; then
LOCATION="${ARG}"
else
LOCATION="${LOCATION}+${ARG}"
fi
COUNT=$((COUNT + 1))
done
fi
curl https://v2.wttr.in/~"${LOCATION}"
#+END_SRC #+END_SRC

View File

@ -493,19 +493,20 @@ We can indicate what to display in our modeline. Each formatter will
be separated by a Powerline separator with the code point ~0xE0B0~ in be separated by a Powerline separator with the code point ~0xE0B0~ in
the font I am using (see §[[#Theme-Fonts-28pc8141v5j0]]). the font I am using (see §[[#Theme-Fonts-28pc8141v5j0]]).
#+name: modeline-format #+name: modeline-format
| Formatter | What it does | Command? | | Formatter | What it does | Command? |
|-----------+-------------------------------------------------------+----------| |---------------+-------------------------------------------------------+----------|
| ~%g~ | Display list of groups | | | ~%g~ | Display list of groups | |
| ~%W~ | Display list of windows in the current group and head | | | ~%W~ | Display list of windows in the current group and head | |
| ~^>~ | Rest of the modeline align to the right | | | ~^>~ | Rest of the modeline align to the right | |
| ~mu-unread~ | Display number of unread emails | yes | | ~stump-weather~ | Display the current weather | yes |
| ~%m~ | Display current MPD song | | | ~mu-unread~ | Display number of unread emails | yes |
| ~%I~ | Display Wifi status | | | ~%m~ | Display current MPD song | |
| ~%l~ | Display network usage | | | ~%I~ | Display Wifi status | |
| ~%C~ | Display CPU usage | | | ~%l~ | Display network usage | |
| ~%M~ | Display RAM usage | | | ~%C~ | Display CPU usage | |
| ~%B~ | Display battery status | | | ~%M~ | Display RAM usage | |
| ~%d~ | Display date | | | ~%B~ | Display battery status | |
| ~%d~ | Display date | |
#+name: modeline-format-gen #+name: modeline-format-gen
#+begin_src emacs-lisp :var elements=modeline-format :exports none #+begin_src emacs-lisp :var elements=modeline-format :exports none
@ -518,8 +519,8 @@ the font I am using (see §[[#Theme-Fonts-28pc8141v5j0]]).
elements) elements)
#+end_src #+end_src
#+RESULTS[7f62146daae5aab6014d303b57c306fe44c4087b]: modeline-format-gen #+RESULTS[4a774559ef9c88c74aa62a42c6f53fdc401815aa]: modeline-format-gen
: (("%g") ("%W") ("^>") ("mu-unread" . t) ("%m") ("%I") ("%l") ("%C") ("%M") ("%B") ("%d")) : (("%g") ("%W") ("^>") ("stump-weather" . t) ("mu-unread" . t) ("%m") ("%I") ("%l") ("%C") ("%M") ("%B") ("%d"))
#+begin_src lisp :noweb yes #+begin_src lisp :noweb yes
(defvar *mode-line-formatter-list* (defvar *mode-line-formatter-list*