File: tempo.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 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      show this help message
  41 #   -help   show this help message
  42 #
  43 #   -w      wide-mode, to get forecasts for a couple of days
  44 #   -wide   wide-mode, to get forecasts for a couple of days
  45 
  46 
  47 case "$1" in
  48     -h|--h|-help|--help)
  49         {
  50             awk '/^# +tempo /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  51             printf "Each place/argument allows the prefixes shown below.\n\n"
  52             printf "\r\n" | curl --show-error -s telnet://graph.no:79 2>&1
  53         } | less -MKiCRS
  54         exit 0
  55     ;;
  56 esac
  57 
  58 width="$(($(tput cols) - 2))"
  59 weather_width="${width}"
  60 case "$1" in
  61     -w|--w|-wide|--wide|-wider|--wider)
  62         weather_width="$((width * 2))"
  63         shift
  64     ;;
  65 esac
  66 
  67 [ "$1" = "--" ] && shift
  68 
  69 {
  70     # show the current date/time center-aligned
  71     printf "%*s" "$((width / 2 - 11))" ""
  72     printf "\e[38;2;78;154;6m%s\e[0m  \e[38;2;52;101;164m%s\e[0m\n\n" \
  73         "$(date +'%a %b %d %Y')" "$(date +'%H:%M')"
  74 
  75     # debian linux has a different `cal` app which highlights the day
  76     if [ -e "/usr/bin/ncal" ]; then
  77         # fix debian/ncal's weird way to highlight the current day
  78         ncal -C -3 | sed -E 's/_\x08(.)/\x1b[7m\1\x1b[0m/g'
  79     else
  80         cal -3
  81     fi | awk -v n="$((width / 2 - 32))" '{ printf "%*s%s\n", n, "", $0 }'
  82 
  83     for place in "$@"; do
  84         printf "\n\e[7m%-${width}s\e[0m\n" "${place}"
  85 
  86         printf "%s~%s\r\n\r\n" "${place}" "${weather_width}" |
  87         curl --show-error -s telnet://graph.no:79 2>&1 |
  88         sed -u -E \
  89             -e 's/ *\r?$//' \
  90             -e '/^\[/d' \
  91             -e 's/^ *-= *([^=]+) +=- *$/\1\n/' \
  92             -e 's/-/\x1b[38;2;196;160;0m●\x1b[0m/g' \
  93             -e 's/^( +)\x1b\[38;2;196;160;0m●\x1b\[0m/\1-/g' \
  94             -e 's/\|/\x1b[38;2;52;101;164m█\x1b[0m/g' \
  95             -e 's/#/\x1b[38;2;218;218;218m█\x1b[0m/g' \
  96             -e 's/([=\^][=\^]*)/\x1b[38;2;164;164;164m\1\x1b[0m/g' \
  97             -e 's/\*/○/g' \
  98             -e 's/_/\x1b[48;2;216;200;0m_\x1b[0m/g' \
  99             -e 's/([0-9][0-9]\/[0-9][0-9])/\x1b[7m\1\x1b[0m/g' | awk 1
 100     done
 101 } | less -MKiCRS --header=11