[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:
parent
46bae114e3
commit
d78be92786
@ -2044,18 +2044,77 @@ CACHEFILE=$([ -n "$XDG_CACHE_HOME" ] && echo "$XDG_CACHE_HOME/wallpaper" || echo
|
||||
|
||||
* Weather
|
||||
:PROPERTIES:
|
||||
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/.local/bin/we
|
||||
:CUSTOM_ID: Weather-4ed00bb0
|
||||
: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. By default, I want the request to be about the
|
||||
city I live in, but it is also possible for the script to accept as its
|
||||
arguments a search inquiry.
|
||||
#+BEGIN_SRC fish
|
||||
if count $argv > /dev/null
|
||||
set -l SEARCH (string join '+' $argv)
|
||||
curl http://v2.wttr.in/~$SEARCH
|
||||
** StumpWM utilities
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: Weather-StumpWM-utilities-okvit9r05gj0
|
||||
:END:
|
||||
*** ~stump-choose-city~
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: Weather-StumpWM-utilities-stump-choose-city-adyit9r05gj0
|
||||
: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 StumpWM’s 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
|
||||
curl http://v2.wttr.in/Aubervilliers
|
||||
end
|
||||
printf "wttr.in unreachable"
|
||||
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
|
||||
|
@ -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
|
||||
the font I am using (see §[[#Theme-Fonts-28pc8141v5j0]]).
|
||||
#+name: modeline-format
|
||||
| Formatter | What it does | Command? |
|
||||
|-----------+-------------------------------------------------------+----------|
|
||||
| ~%g~ | Display list of groups | |
|
||||
| ~%W~ | Display list of windows in the current group and head | |
|
||||
| ~^>~ | Rest of the modeline align to the right | |
|
||||
| ~mu-unread~ | Display number of unread emails | yes |
|
||||
| ~%m~ | Display current MPD song | |
|
||||
| ~%I~ | Display Wifi status | |
|
||||
| ~%l~ | Display network usage | |
|
||||
| ~%C~ | Display CPU usage | |
|
||||
| ~%M~ | Display RAM usage | |
|
||||
| ~%B~ | Display battery status | |
|
||||
| ~%d~ | Display date | |
|
||||
| Formatter | What it does | Command? |
|
||||
|---------------+-------------------------------------------------------+----------|
|
||||
| ~%g~ | Display list of groups | |
|
||||
| ~%W~ | Display list of windows in the current group and head | |
|
||||
| ~^>~ | Rest of the modeline align to the right | |
|
||||
| ~stump-weather~ | Display the current weather | yes |
|
||||
| ~mu-unread~ | Display number of unread emails | yes |
|
||||
| ~%m~ | Display current MPD song | |
|
||||
| ~%I~ | Display Wifi status | |
|
||||
| ~%l~ | Display network usage | |
|
||||
| ~%C~ | Display CPU usage | |
|
||||
| ~%M~ | Display RAM usage | |
|
||||
| ~%B~ | Display battery status | |
|
||||
| ~%d~ | Display date | |
|
||||
|
||||
#+name: modeline-format-gen
|
||||
#+begin_src emacs-lisp :var elements=modeline-format :exports none
|
||||
@ -518,8 +519,8 @@ the font I am using (see §[[#Theme-Fonts-28pc8141v5j0]]).
|
||||
elements)
|
||||
#+end_src
|
||||
|
||||
#+RESULTS[7f62146daae5aab6014d303b57c306fe44c4087b]: modeline-format-gen
|
||||
: (("%g") ("%W") ("^>") ("mu-unread" . t) ("%m") ("%I") ("%l") ("%C") ("%M") ("%B") ("%d"))
|
||||
#+RESULTS[4a774559ef9c88c74aa62a42c6f53fdc401815aa]: modeline-format-gen
|
||||
: (("%g") ("%W") ("^>") ("stump-weather" . t) ("mu-unread" . t) ("%m") ("%I") ("%l") ("%C") ("%M") ("%B") ("%d"))
|
||||
|
||||
#+begin_src lisp :noweb yes
|
||||
(defvar *mode-line-formatter-list*
|
||||
|
Loading…
Reference in New Issue
Block a user