File: gbm.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 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 # gbm [options...] [good regex...] [bad regex...] [meh regex...] [files...]
  27 #
  28 # Good, Bad, Meh colors lines using up to 3 regular expressions, keeping all
  29 # other input lines verbatim.
  30 #
  31 # For the `good` matches, a colorblind-friendly blue is used instead of green
  32 # if either environment variable COLORBLIND or COLOR_BLIND is declared and set
  33 # to 1.
  34 #
  35 # The case-insensitive-comparison option is any of `-i`, `--i`, `-ins`, or
  36 # `--ins`. The help option is `-h`, `--h`, `-help`, or `--help`.
  37 
  38 
  39 buffered=0
  40 insensitive=0
  41 
  42 while [ $# -gt 0 ]; do
  43     case "$1" in
  44         -b|--b|-buffered|--buffered)
  45             buffered=1
  46             shift
  47             continue
  48         ;;
  49 
  50         -h|--h|-help|--help)
  51             awk '/^# +gbm /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  52             exit 0
  53         ;;
  54 
  55         -i|--i|-ins|--ins|-insensitive|--insensitive)
  56             insensitive=1
  57             shift
  58             continue
  59         ;;
  60     esac
  61 
  62     break
  63 done
  64 
  65 [ "$1" = '--' ] && shift
  66 
  67 # show all non-existing files given
  68 i=0
  69 failed=0
  70 for arg in "$@"; do
  71     i=$(( i + 1 ))
  72     if [ "${i}" -lt 4 ]; then
  73         continue
  74     fi
  75     if [ "${arg}" = "-" ]; then
  76         continue
  77     fi
  78     if [ ! -e "${arg}" ]; then
  79         printf "no file named \"%s\"\n" "${arg}" >&2
  80         failed=1
  81     fi
  82 done
  83 
  84 if [ "${failed}" -gt 0 ]; then
  85     exit 2
  86 fi
  87 
  88 flush=0
  89 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
  90     flush=1
  91 fi
  92 
  93 awk -v flush="${flush}" -v ci="${insensitive}" '
  94     BEGIN {
  95         got_good = ARGC > 1
  96         got_bad = ARGC > 2
  97         got_meh = ARGC > 3
  98         good = ARGV[1]
  99         bad = ARGV[2]
 100         meh = ARGV[3]
 101         delete ARGV[1]
 102         delete ARGV[2]
 103         delete ARGV[3]
 104 
 105         if (ci && IGNORECASE == "") {
 106             msg = "this variant of AWK lacks case-insensitive regex-matching"
 107             print(msg) > "/dev/stderr"
 108             exit 125
 109         }
 110         if (ci) IGNORECASE = 1
 111 
 112         # normal good-style is green, colorblind-friendly good-style is blue
 113         cb = ENVIRON["COLORBLIND"] != 0 || ENVIRON["COLOR_BLIND"] != 0
 114         good_style = cb ? "\x1b[38;2;0;95;215m" : "\x1b[38;2;0;135;95m"
 115         good_fmt = good_style "%s\x1b[0m\n"
 116         good_reset = "\x1b[0m" good_style
 117     }
 118 
 119     got_good && $0 ~ good {
 120         gsub(/\x1b\[0m/, good_reset)
 121         printf good_fmt, $0
 122         if (flush) fflush()
 123         next
 124     }
 125 
 126     got_bad && $0 ~ bad {
 127         gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;204;0;0m")
 128         printf "\x1b[38;2;204;0;0m%s\x1b[0m\n", $0
 129         if (flush) fflush()
 130         next
 131     }
 132 
 133     got_meh && $0 ~ meh {
 134         gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;168;168;168m")
 135         printf "\x1b[38;2;168;168;168m%s\x1b[0m\n", $0
 136         if (flush) fflush()
 137         next
 138     }
 139 
 140     { print; if (flush) fflush() }
 141 ' "$@"