File: def.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 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 # def [words...]
  27 #
  28 # DEFine/lookup words via the online english dictionary from https://dict.org
  29 
  30 
  31 if [ $# -eq 0 ]; then
  32     # printf "\e[31mno words to lookup given\e[0m\n" > /dev/stderr
  33     # exit 1
  34 
  35     awk '/^# +def/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  36     exit 0
  37 fi
  38 
  39 # handle leading options
  40 case "$1" in
  41     -h|--h|-help|--help)
  42         awk '/^# +def/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  43         exit 0
  44     ;;
  45 esac
  46 
  47 # keep track of this script's eventual success/failure
  48 errcode=0
  49 
  50 for word in "$@"; do
  51     # printf "\e[48;2;218;218;218m%-80s\e[0m\n" "${word}"
  52     printf "\n\e[7m%-80s\e[0m\n" "${word}"
  53 
  54     curl -s "dict://dict.org/d:${word}" | sed 's-\r--g' | awk '
  55         # get rid of carriage-returns at the end of lines
  56         { gsub(/\r/, "") }
  57 
  58         # code 151 has the specific source for the match, so
  59         # use a unique style for that kind of metadata line
  60         /^151 / {
  61             printf "\x1b[38;2;52;101;164m%s\x1b[0m\n", $0
  62             fflush()
  63             next
  64         }
  65 
  66         # make error-type metadata lines stand out
  67         /^[3-5][0-9]{2} [a-z]/ {
  68             nerr++
  69             printf "\x1b[38;2;175;0;0m%s\x1b[0m\n", $0
  70             fflush()
  71             next
  72         }
  73 
  74         # gray out ok-type metadata lines
  75         /^[12][0-9]{2} / || /^\.$/ {
  76             printf "\x1b[38;2;128;128;128m%s\x1b[0m\n", $0
  77             fflush()
  78             next
  79         }
  80 
  81         # keep all other lines unstyled
  82         { print; fflush() }
  83 
  84         # fail the script if any lookup failed
  85         END { if (nerr > 0) exit 1 }
  86     '
  87 
  88     # any lookup failure eventually results in the whole script failing,
  89     # but only after trying to show results for all words given
  90     if [ $? -ne 0 ]; then
  91         errcode=1
  92     fi
  93 done | less -JMKiCRS
  94 
  95 exit "${errcode}"