File: lawk.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 # lawk [options...] [awk expression...] [files...]
  27 #
  28 #
  29 # Legend with AWK color-codes lines using the results of the AWK expression
  30 # given.
  31 #
  32 # Each distinct result of the expression is used consistently, even though
  33 # the limited palette means colors can be recycled across differing results,
  34 # given enough of those.
  35 #
  36 # A colorblind-friendly palette which avoids red is available instead of the
  37 # default one, if either environment variable COLORBLIND or COLOR_BLIND is
  38 # declared and set to 1.
  39 #
  40 # The handy case-insensitive shortcut options may cause this tool to fail,
  41 # if the main AWK tool installed doesn't support the special IGNORECASE
  42 # variable.
  43 #
  44 # The AWK options available only in single-dash versions are
  45 #
  46 #   -F fs, -Ffs, -F=fs    make `fs` the field separator
  47 #
  48 # The other options are, available both in single and double-dash versions
  49 #
  50 #   -back        use background colors/styles instead
  51 #   -h, -help    show this help message
  52 #   -i, -ins     match regexes case-insensitively; may fail the default `awk`
  53 #   -tsv         split fields using tabs, same as using -F "\t"
  54 #   -s, -sym     use symbols as legends, instead of styles/colors
  55 #   -verbose     also emit the color-coded keys/legend to stderr at the end
  56 
  57 
  58 buf=0
  59 back=0
  60 tsv=0
  61 ins=0
  62 sym=0
  63 verbose=0
  64 cmd='awk'
  65 
  66 while [ $# -gt 0 ]; do
  67     case "$1" in
  68         -b|--b|-buffered|--buffered) buf=1; shift; continue ;;
  69 
  70         -back|--back) back=1; shift; continue ;;
  71 
  72         -F)
  73             if [ $# -lt 2 ]; then
  74                 printf "expected value after -F option\n" >&2
  75                 exit 1
  76             fi
  77             cmd="${cmd} -F $2"; shift 2; continue
  78         ;;
  79 
  80         -F*) cmd="${cmd} $1"; shift; continue ;;
  81 
  82         -h|--h|-help|--help)
  83             awk '/^# +lawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  84             exit 0
  85         ;;
  86 
  87         -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;;
  88 
  89         -s|--s|-sym|--sym|-symbols|--symbols) sym=1; shift; continue ;;
  90 
  91         -tsv|--tsv) tsv=1; shift; continue ;;
  92 
  93         -v)
  94             if [ $# -lt 2 ]; then
  95                 printf "expected variable assignment after -v option\n" >&2
  96                 exit 1
  97             fi
  98             cmd="${cmd} -v $1"; shift 2; continue
  99         ;;
 100 
 101         -verbose|--verbose) verbose=1; shift; continue ;;
 102 
 103         -) break ;;
 104 
 105         --) shift; break ;;
 106 
 107         -*)
 108             printf "%s: unsupported option '%s'\n" "$0" "$1" >&2
 109             exit 1
 110         ;;
 111     esac
 112 
 113     break
 114 done
 115 
 116 code="${1:-\$0}"
 117 [ $# -gt 0 ] && shift
 118 
 119 # show all non-existing files given
 120 failed=0
 121 for arg in "$@"; do
 122     [ "${arg}" = "-" ] && continue
 123     [ -e "${arg}" ] && continue
 124     printf "%s: no file named \"%s\"\n" "$0" "${arg}" >&2
 125     failed=1
 126 done
 127 
 128 [ "${failed}" -gt 0 ] && exit 2
 129 
 130 flush=''
 131 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 132     flush='fflush()'
 133 fi
 134 
 135 ci='
 136 BEGIN {
 137     if (IGNORECASE == "") {
 138         m = "your `awk` command lacks case-insensitive regex-matching"
 139         print(m) > "/dev/stderr"
 140         exit 125
 141     }
 142     IGNORECASE = 1
 143 }
 144 '
 145 if [ "${ins}" -eq 0 ]; then
 146     ci=''
 147 fi
 148 
 149 # normal colors
 150 palette1='
 151     CB = ENVIRON["COLORBLIND"] != 0 || ENVIRON["COLOR_BLIND"] != 0
 152     PALETTE[C++] = "\x1b[38;2;0;95;215m" # blue
 153     PALETTE[C++] = "\x1b[38;2;215;95;0m" # orange
 154     PALETTE[C++] = "\x1b[38;2;135;95;255m" # purple
 155     PALETTE[C++] = "\x1b[38;2;0;175;215m" # cyan
 156     PALETTE[C++] = "\x1b[38;2;255;135;255m" # pink
 157     PALETTE[C++] = "\x1b[38;2;0;135;95m" # green
 158     if (!CB) PALETTE[C++] = "\x1b[38;2;204;0;0m" # red
 159     PALETTE[C++] = "\x1b[38;2;168;168;168m" # gray
 160 '
 161 
 162 # background colors
 163 palette2='
 164     CB = ENVIRON["COLORBLIND"] != 0 || ENVIRON["COLOR_BLIND"] != 0
 165     PALETTE[C++] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" # blue
 166     PALETTE[C++] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" # orange
 167     PALETTE[C++] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" # purple
 168     PALETTE[C++] = "\x1b[48;2;0;175;215m\x1b[38;2;238;238;238m" # cyan
 169     PALETTE[C++] = "\x1b[48;2;255;135;255m\x1b[38;2;238;238;238m" # pink
 170     PALETTE[C++] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" # green
 171     if (!CB) PALETTE[C++] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" # red
 172     PALETTE[C++] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" # gray
 173 '
 174 
 175 if [ "${sym}" -eq 1 ]; then
 176     palette=''
 177 elif [ "${back}" -eq 0 ]; then
 178     palette="${palette1}"
 179 else
 180     palette="${palette2}"
 181 fi
 182 
 183 src="${ci}"'
 184 BEGIN {
 185 '"${palette}"'
 186     if (!SYM) for (I = 0; I < C; I++) RESET[I] = "\x1b[0m" PALETTE[I]
 187     # split makes indexing SYMBOLS 1-based
 188     if (SYM) split("ABCDEFGHIJKLMNOPQRSTUVWXYZ@%&.", SYMBOLS, "")
 189     if (SYM) C = length(SYMBOLS)
 190 }
 191 
 192 {
 193     K = ('"${code}"')
 194 
 195     I = INDICES[K]
 196     if (I == "") {
 197         I = N % C
 198         INDICES[K] = I
 199         KEYS[N + 0] = K
 200         N++
 201     }
 202 
 203     if (!SYM) {
 204         gsub(/\x1b\[0m/, RESET[I])
 205         printf("%s%s\x1b[0m\n", PALETTE[I], $0)
 206     } else {
 207         printf("%s\t%s\n", SYMBOLS[I + 1], $0)
 208     }
 209     if (FLUSH) fflush()
 210 }
 211 
 212 END {
 213     for (i = 0; i < N && VERBOSE; i++) {
 214         k = KEYS[i]
 215         j = INDICES[k]
 216         if (!SYM) {
 217             printf("%s%s\x1b[0m\n", PALETTE[j], k) > "/dev/stderr"
 218         } else {
 219             printf("%s\t%s\x1b[0m\n", SYMBOLS[j + 1], k) > "/dev/stderr"
 220         }
 221     }
 222 }
 223 '
 224 
 225 f="${flush}"
 226 s="${sym}"
 227 v="${verbose}"
 228 
 229 if [ "${tsv}" -eq 1 ]; then
 230     ${cmd} -F "\t" -v FLUSH="$f" -v SYM="$s" -v VERBOSE="$v" "${src}" "$@"
 231 else
 232     ${cmd} -v FLUSH="$f" -v SYM="$s" -v VERBOSE="$v" "${src}" "$@"
 233 fi