File: ecoli.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 # ecoli [options...] [regex/style pairs...]
  27 #
  28 #
  29 # Expressions COloring LInes tries to match each line read from the standard
  30 # input to the regexes given, coloring/styling with the named-style paired
  31 # to the first matching regex, if any. Lines not matching any regex stay the
  32 # same.
  33 #
  34 # The options are, available both in single and double-dash versions
  35 #
  36 #   -back, -background, -bg     automatically turn styles into background ones
  37 #   -h, -help                   show this help message
  38 #   -hi, -hilite, -highlight    all arguments are regexes to highlight matches
  39 #   -i, -ins, -insensitive      match the regexes given case-insensitively
  40 #
  41 # Some of the colors/styles available are:
  42 #
  43 #     blue         blueback
  44 #     bold
  45 #     gray         grayback
  46 #     green        greenback
  47 #     inverse
  48 #     magenta      magentaback
  49 #     orange       orangeback
  50 #     purple       purpleback
  51 #     red          redback
  52 #     underline
  53 #
  54 # Some style aliases are:
  55 #
  56 #     b       blue                  bb       blueback
  57 #     g       green                 gb       greenback
  58 #     m       magenta               mb       magentaback
  59 #     o       orange                ob       orangeback
  60 #     p       purple                pb       purpleback
  61 #     r       red                   rb       redback
  62 #     i       inverse (highlight)
  63 #     u       underline
  64 
  65 
  66 buf=0
  67 bg=0
  68 hilite=0
  69 ins=0
  70 
  71 while [ $# -gt 0 ]; do
  72     case "$1" in
  73         -b|--b|-buffered|--buffered) buf=1; shift; continue ;;
  74 
  75         -back|--back|-background|-background|-bg|--bg)
  76             bg=1; shift; continue
  77         ;;
  78 
  79         -h|--h|-help|--help)
  80             awk '/^# +ecoli /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  81             exit 0
  82         ;;
  83 
  84         -hi|--hi|-hilite|--hilite|-highlight|--highlight|-highlighted|\
  85         --highlighted|-inv|--inv|-invert|--invert|-inverted|--inverted)
  86             hilite=1; shift; continue
  87         ;;
  88 
  89         -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;;
  90 
  91         --) shift; break ;;
  92 
  93         -*)
  94             printf "%s: unsupported option '%s'\n" "$0" "$1" >&2
  95             exit 1
  96         ;;
  97     esac
  98 
  99     break
 100 done
 101 
 102 flush=0
 103 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 104     flush=1
 105 fi
 106 
 107 hi="${hilite}"
 108 
 109 awk -v flush="${flush}" -v hi="${hi}" -v autoback="${bg}" -v ci="${ins}" '
 110 BEGIN {
 111     if (ci == 1 && IGNORECASE == "") {
 112         m = "your `awk` command lacks case-insensitive regex-matching"
 113         print(m) > "/dev/stderr"
 114         exit 125
 115     }
 116     if (ci == 1) IGNORECASE = 1
 117 
 118     # alias-lookup table
 119     a["r"] = "red"
 120     a["g"] = "green"
 121     a["b"] = "blue"
 122     a["o"] = "orange"
 123     a["p"] = "purple"
 124     a["m"] = "magenta"
 125     a["h"] = "invert"
 126     a["i"] = "invert"
 127     a["u"] = "underline"
 128     a["or"] = "orange"
 129     a["ma"] = "magenta"
 130     a["hi"] = "invert"
 131     a["in"] = "invert"
 132     a["un"] = "underline"
 133     a["inv"] = "invert"
 134     a["mag"] = "magenta"
 135     a["grey"] = "gray"
 136     a["inverse"] = "invert"
 137     a["inverted"] = "invert"
 138     a["hilite"] = "invert"
 139     a["hilited"] = "invert"
 140     a["highlight"] = "invert"
 141     a["highlighted"] = "invert"
 142     a["underlined"] = "underline"
 143     a["bb"] = "blueback"
 144     a["gb"] = "greenback"
 145     a["mb"] = "magentaback"
 146     a["ob"] = "orangeback"
 147     a["pb"] = "purpleback"
 148     a["rb"] = "redback"
 149     a["bg"] = "greenback"
 150     a["bm"] = "magentaback"
 151     a["bo"] = "orangeback"
 152     a["bp"] = "purpleback"
 153     a["br"] = "redback"
 154     a["bluebg"] = "blueback"
 155     a["graybg"] = "grayback"
 156     a["greenbg"] = "greenback"
 157     a["greyback"] = "grayback"
 158     a["greybg"] = "grayback"
 159     a["magentabg"] = "magentaback"
 160     a["orangebg"] = "orangeback"
 161     a["purplebg"] = "purpleback"
 162     a["redbg"] = "redback"
 163     a["magback"] = "magentaback"
 164     a["magbg"] = "magentaback"
 165     a["orback"] = "orangeback"
 166     a["orbg"] = "orangeback"
 167     a["purback"] = "purpleback"
 168     a["purbg"] = "purpleback"
 169 
 170     # style-lookup table
 171     s["red"] = "\x1b[38;2;204;0;0m"
 172     s["green"] = "\x1b[38;2;0;135;95m"
 173     s["blue"] = "\x1b[38;2;0;95;215m"
 174     s["orange"] = "\x1b[38;2;215;95;0m"
 175     s["purple"] = "\x1b[38;2;135;95;255m"
 176     s["magenta"] = "\x1b[38;2;215;0;255m"
 177     s["gray"] = "\x1b[38;2;168;168;168m"
 178     s["bold"] = "\x1b[1m"
 179     s["invert"] = "\x1b[7m"
 180     s["italic"] = "\x1b[3m"
 181     s["strike"] = "\x1b[9m"
 182     s["underline"] = "\x1b[4m"
 183     s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m"
 184     s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m"
 185     s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m"
 186     s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m"
 187     s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m"
 188     s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m"
 189     s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m"
 190 
 191     if ((ARGC % 2 != 1) && !hi) {
 192         printf "missing final style name\n" > "/dev/stderr"
 193         exit 1
 194     }
 195 
 196     for (i = 1; i < ARGC; i++) {
 197         arg = ARGV[i]
 198         delete ARGV[i]
 199 
 200         if (hi) {
 201             exprs[++n] = arg
 202             styles[n] = "\x1b[7m"
 203             resetstyles[n] = "\x1b[0m\x1b[7m"
 204             continue
 205         }
 206 
 207         if (i % 2 != 0) {
 208             exprs[++n] = arg
 209             continue
 210         }
 211 
 212         name = arg
 213 
 214         # resolve aliases
 215         if (a[name] != "") name = a[name]
 216 
 217         style = s[name]
 218 
 219         if (autoback && (name !~ /back/)) {
 220             name = name "back"
 221             style = s[name]
 222         }
 223 
 224         if (style == "") {
 225             fmt = "style/color \"%s\" not supported\n"
 226             printf(fmt, name) > "/dev/stderr"
 227             exit 1
 228         }
 229 
 230         styles[n] = style
 231         resetstyles[n] = "\x1b[0m" style
 232     }
 233 }
 234 
 235 {
 236     # ignore cursor-movers, style-changers, and OSC sequences
 237     plain = $0
 238     gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 239     gsub(/\x1b\][^\x1b]+\x1b\\/, "", plain)
 240 
 241     for (i = 1; i <= n; i++) {
 242         if (plain ~ exprs[i]) {
 243             gsub(/\x1b\[0m/, resetstyles[i])
 244             printf "%s%s\x1b[0m\n", styles[i], $0
 245             if (flush) fflush()
 246             next
 247         }
 248     }
 249 
 250     print
 251     if (flush) fflush()
 252 }
 253 ' "$@"