File: gbmawk.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 # gbmawk [options...] [good...] [bad...] [meh...] [files...]
  27 #
  28 #
  29 # Good, Bad, Meh with AWK colors lines using up to 3 (optional) AWK conditions,
  30 # namely the `good` (green or blue), the `bad` (red), and the `meh` (gray).
  31 #
  32 # For the `good` matches, a colorblind-friendly blue is used instead of green
  33 # if either environment variable COLORBLIND or COLOR_BLIND is declared and set
  34 # to 1.
  35 #
  36 # The handy case-insensitive shortcut options may cause this tool to fail,
  37 # if the main AWK tool installed doesn't support the special IGNORECASE
  38 # variable.
  39 #
  40 # The AWK options available only in single-dash versions are
  41 #
  42 #   -F fs, -Ffs, -F=fs    make `fs` the field separator
  43 #
  44 # The other options are, available both in single and double-dash versions
  45 #
  46 #   -s, -solid    color-code backgrounds of lines satisfying conditions given
  47 #   -h, -help     show this help message
  48 #   -i, -ins      match regexes case-insensitively; may fail the default `awk`
  49 #   -tsv          split fields using tabs, same as using -F "\t"
  50 
  51 
  52 tsv=0
  53 buf=0
  54 ins=0
  55 solid=0
  56 cmd='awk'
  57 
  58 while [ $# -gt 0 ]; do
  59     case "$1" in
  60         -b|--b|-buffered|--buffered) buf=1; shift; continue ;;
  61 
  62         -F)
  63             [ $# -ge 2 ] && cmd="${cmd} -F $2"; shift 2; continue
  64             printf "expected value after -F option\n" >&2
  65             exit 1
  66         ;;
  67 
  68         -F*) cmd="${cmd} $1"; shift; continue ;;
  69 
  70         -h|--h|-help|--help)
  71             awk '/^# +gbmawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  72             exit 0
  73         ;;
  74 
  75         -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;;
  76 
  77         -s|--s|-solid|--solid) solid=1; shift; continue ;;
  78 
  79         -tsv|--tsv) tsv=1; shift; continue ;;
  80 
  81         -v)
  82             [ $# -ge 2 ] && cmd="${cmd} -v $2" && shift 2 && continue
  83             printf "expected variable assignment after -v option\n" >&2
  84             exit 1
  85         ;;
  86 
  87         -) break ;;
  88 
  89         --) shift; break ;;
  90 
  91         -*)
  92             printf "unsupported option '%s'\n" "$1" >&2
  93             exit 1
  94         ;;
  95     esac
  96 
  97     break
  98 done
  99 
 100 if [ $# -eq 0 ]; then
 101     awk '/^# +gbmawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
 102     exit 1
 103 fi
 104 
 105 good="${1:-0}"
 106 bad="${2:-0}"
 107 meh="${3:-0}"
 108 
 109 [ $# -gt 0 ] && shift
 110 [ $# -gt 0 ] && shift
 111 [ $# -gt 0 ] && shift
 112 
 113 # show all non-existing files given
 114 fail=0
 115 for arg in "$@"; do
 116     [ "${arg}" = "-" ] && continue
 117     [ -e "${arg}" ] && continue
 118     printf "no file named \"%s\"\n" "${arg}" >&2
 119     fail=1
 120 done
 121 
 122 [ "${fail}" -gt 0 ] && exit 2
 123 
 124 flush=''
 125 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 126     flush='; fflush()'
 127 fi
 128 
 129 ci='
 130     BEGIN {
 131         if (IGNORECASE == "") {
 132             m = "your `awk` command lacks case-insensitive regex-matching"
 133             print(m) > "/dev/stderr"
 134             exit 125
 135         }
 136         IGNORECASE = 1
 137     }
 138 '
 139 if [ "${ins}" -eq 0 ]; then
 140     ci=''
 141 fi
 142 
 143 # normal good-style is green, colorblind-friendly good-style is blue
 144 if [ "${solid}" -eq 1 ]; then
 145     gs='\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m'
 146 else
 147     gs='\x1b[38;2;0;135;95m'
 148 fi
 149 if [ "${COLORBLIND:-0}" -gt 0 ] || [ "${COLOR_BLIND:-0}" -gt 0 ]; then
 150     if [ "${solid}" -eq 1 ]; then
 151         gs='\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m'
 152     else
 153         gs='\x1b[38;2;0;95;215m'
 154     fi
 155 fi
 156 
 157 if [ "${solid}" -eq 1 ]; then
 158     bs='\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m'
 159     ms='\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m'
 160 else
 161     bs='\x1b[38;2;204;0;0m'
 162     ms='\x1b[38;2;168;168;168m'
 163 fi
 164 
 165 src="${ci}"'
 166     ('"${good}"') {
 167         gsub(/\x1b\[0m/, "\x1b[0m'$gs'")
 168         printf("'$gs'%s\x1b[0m\n", $0)'"${flush}"'
 169         next
 170     }
 171 
 172     ('"${bad}"') {
 173         gsub(/\x1b\[0m/, "\x1b[0m'$bs'")
 174         printf("'$bs'%s\x1b[0m\n", $0)'"${flush}"'
 175         next
 176     }
 177 
 178     ('"${meh}"') {
 179         gsub(/\x1b\[0m/, "\x1b[0m'$ms'")
 180         printf("'$ms'%s\x1b[0m\n", $0)'"${flush}"'
 181         next
 182     }
 183 
 184     { print'"${flush}"' }
 185 '
 186 
 187 if [ "${tsv}" -eq 1 ]; then
 188     ${cmd} -F "\t" "${src}" "$@"
 189 else
 190     ${cmd} "${src}" "$@"
 191 fi