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 insensitive=0
  40 case "$1" in
  41     -h|--h|-help|--help)
  42         awk '/^# +gbm /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  43         exit 0
  44     ;;
  45     -i|--i|-ins|--ins|-insensitive|--insensitive)
  46         insensitive=1
  47         shift
  48     ;;
  49 esac
  50 
  51 [ "$1" = '--' ] && shift
  52 
  53 command='awk'
  54 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then
  55     command='stdbuf -oL awk'
  56 fi
  57 
  58 ${command} -v ci="${insensitive}" '
  59     BEGIN {
  60         got_good = ARGC > 1
  61         got_bad = ARGC > 2
  62         got_meh = ARGC > 3
  63         good = ARGV[1]
  64         bad = ARGV[2]
  65         meh = ARGV[3]
  66         delete ARGV[1]
  67         delete ARGV[2]
  68         delete ARGV[3]
  69 
  70         if (ci && IGNORECASE == "") {
  71             msg = "this variant of AWK lacks case-insensitive regex-matching"
  72             print(msg) > "/dev/stderr"
  73             exit 125
  74         }
  75         if (ci) IGNORECASE = 1
  76 
  77         # normal good-style is green, colorblind-friendly good-style is blue
  78         cb = ENVIRON["COLORBLIND"] != 0 || ENVIRON["COLOR_BLIND"] != 0
  79         good_style = cb ? "\x1b[38;2;0;95;215m" : "\x1b[38;2;0;135;95m"
  80         good_fmt = good_style "%s\x1b[0m\n"
  81         good_reset = "\x1b[0m" good_style
  82     }
  83 
  84     got_good && $0 ~ good {
  85         gsub(/\x1b\[0m/, good_reset)
  86         printf good_fmt, $0
  87         next
  88     }
  89 
  90     got_bad && $0 ~ bad {
  91         gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;204;0;0m")
  92         printf "\x1b[38;2;204;0;0m%s\x1b[0m\n", $0
  93         next
  94     }
  95 
  96     got_meh && $0 ~ meh {
  97         gsub(/\x1b\[0m/, "\x1b[0m\x1b[38;2;168;168;168m")
  98         printf "\x1b[38;2;168;168;168m%s\x1b[0m\n", $0
  99         next
 100     }
 101 
 102     1
 103 ' "$@"