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 using the online english dictionary from
  29 # https://dict.org
  30 
  31 
  32 if [ $# -eq 0 ]; then
  33     printf "\x1b[31mno words to lookup given\x1b[0m\n" > /dev/stderr
  34     exit 1
  35 fi
  36 
  37 # handle leading options
  38 case "$1" in
  39     -h|--h|-help|--help)
  40         # show help message, extracting the info-comment at the start
  41         # of this file, and quit
  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 "\n\x1b[7m%-80s\x1b[0m\n" "${word}"
  52 
  53     curl -s "dict://dict.org/d:${word}" | awk '
  54     # code 151 has the specific source for the match, so
  55     # use a unique style for that kind of metadata line
  56     /^151 / {
  57         printf "\x1b[38;5;4m%s\x1b[0m\n", $0
  58         next
  59     }
  60 
  61     # make error-type metadata lines stand out
  62     /^[3-5][0-9]{2} [a-z]/ {
  63         nerr++
  64         printf "\x1b[38;5;124m%s\x1b[0m\n", $0
  65         next
  66     }
  67 
  68     # gray out ok-type metadata lines
  69     /^[12][0-9]{2} / || /^\.$/ {
  70         printf "\x1b[38;5;244m%s\x1b[0m\n", $0
  71         next
  72     }
  73 
  74     # keep all other lines unstyled
  75     1
  76 
  77     # fail the script if any lookup failed
  78     END {
  79         if (nerr > 0) exit 1
  80     }
  81 '
  82 
  83     # any lookup failure eventually results in the whole script failing,
  84     # but only after trying to show results for all words given
  85     if [ $? -ne 0 ]; then
  86         errcode=1
  87     fi
  88 done
  89 
  90 exit "${errcode}"