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 
  36     # show help message, extracting the info-comment at the start
  37     # of this file, and quit
  38     awk '/^# +def/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  39     exit 0
  40 fi
  41 
  42 # handle leading options
  43 case "$1" in
  44     -h|--h|-help|--help)
  45         # show help message, extracting the info-comment at the start
  46         # of this file, and quit
  47         awk '/^# +def/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  48         exit 0
  49     ;;
  50 esac
  51 
  52 # keep track of this script's eventual success/failure
  53 errcode=0
  54 
  55 for word in "$@"; do
  56     printf "\n\x1b[7m%-80s\x1b[0m\n" "${word}"
  57 
  58     curl -s "dict://dict.org/d:${word}" | awk '
  59     # code 151 has the specific source for the match, so
  60     # use a unique style for that kind of metadata line
  61     /^151 / {
  62         printf "\x1b[38;5;4m%s\x1b[0m\n", $0
  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;5;124m%s\x1b[0m\n", $0
  70         next
  71     }
  72 
  73     # gray out ok-type metadata lines
  74     /^[12][0-9]{2} / || /^\.$/ {
  75         printf "\x1b[38;5;244m%s\x1b[0m\n", $0
  76         next
  77     }
  78 
  79     # keep all other lines unstyled
  80     1
  81 
  82     # fail the script if any lookup failed
  83     END {
  84         if (nerr > 0) exit 1
  85     }
  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
  94 
  95 exit "${errcode}"