File: tempo.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # tempo [options...] [places...]
  27 #
  28 # Show the current date/time, a partial calendar with the 3 months `around`
  29 # the current date, and weather forecast(s) for the places (anywhere in the
  30 # world) given, using ANSI styles to make things stand out.
  31 #
  32 # The name `tempo` comes from Italian: it means either `time` or `weather`,
  33 # depending on the context. Appropriately, this tool can tell you both.
  34 #
  35 # When not given any places to fetch weather forecasts for, this tool is a
  36 # quick way to check the current time/date and month(s).
  37 #
  38 # The options are, available both in single and double-dash versions
  39 #
  40 #   -h, -help    show this help message
  41 #   -w, -wide    wide-mode, to get a few more days on narrow terminals
  42 
  43 
  44 case "$1" in
  45     -h|--h|-help|--help)
  46         {
  47             awk '/^# +tempo /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  48             printf "Each place/argument allows the prefixes shown below.\n\n"
  49             printf "\r\n" | curl --show-error -s telnet://graph.no:79 2>&1
  50         } | less -MKiCRS
  51         exit 0
  52     ;;
  53 esac
  54 
  55 width="$(($(tput cols) - 2))"
  56 weather_width="${width}"
  57 case "$1" in
  58     -w|--w|-wide|--wide|-wider|--wider)
  59         weather_width="$((width * 2))"
  60         shift
  61     ;;
  62 esac
  63 
  64 [ "$1" = '--' ] && shift
  65 
  66 {
  67     # show the current date/time center-aligned
  68     printf "%*s\e[38;2;78;154;6m%s\e[0m  \e[38;2;52;101;164m%s\e[0m\n\n" \
  69         "$((width / 2 - 11))" "" "$(date +'%a %b %d %Y')" "$(date +'%H:%M')"
  70 
  71     # debian linux's `cal` can't highlight the current day
  72     if [ -e "/usr/bin/ncal" ]; then
  73         # `ncal` has a weird way to highlight the current day
  74         ncal -C -3 | sed -E 's/_\x08(.)/\x1b[7m\1\x1b[0m/g'
  75     else
  76         cal -3
  77     fi | awk -v n="$((width / 2 - 32))" '{ printf "%*s%s\n", n, "", $0 }'
  78 
  79     for place in "$@"; do
  80         printf "\n\e[7m%-${width}s\e[0m\n" "${place}"
  81 
  82         printf "%s~%s\r\n\r\n" "${place}" "${weather_width}" |
  83             curl --show-error -s telnet://graph.no:79 2>&1 | sed -u -E \
  84                 -e 's/ *\r?$//' \
  85                 -e '/^\[/d' \
  86                 -e 's/^ *-= *([^=]+) +=- *$/\1\n/' \
  87                 -e 's/-/\x1b[38;2;196;160;0m●\x1b[0m/g' \
  88                 -e 's/^( +)\x1b\[38;2;196;160;0m●\x1b\[0m/\1-/g' \
  89                 -e 's/\|/\x1b[38;2;52;101;164m█\x1b[0m/g' \
  90                 -e 's/=V=/-\x1b[48;2;216;150;0mV\x1b[0m-/g' \
  91                 -e 's/=/\x1b[38;2;218;218;218m█\x1b[0m/g' \
  92                 -e 's/#/\x1b[48;2;218;218;218mF\x1b[0m/g' \
  93                 -e 's/([=\^][=\^]*)/\x1b[38;2;164;164;164m\1\x1b[0m/g' \
  94                 -e 's/\*/○/g' \
  95                 -e 's/_/\x1b[48;2;216;200;0m_\x1b[0m/g' \
  96                 -e 's/([0-9][0-9]\/[0-9][0-9])/\x1b[7m\1\x1b[0m/g' | awk 1
  97     done
  98 } | less -MKiCRS --header=11