File: ecoli.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 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 #   -h, -help                 show this help message
  37 #   -i, -ins, -insensitive    match the regexes given case-insensitively
  38 #
  39 # Some of the colors/styles available are:
  40 #
  41 #     blue         blueback
  42 #     bold
  43 #     gray         grayback
  44 #     green        greenback
  45 #     inverse
  46 #     magenta      magentaback
  47 #     orange       orangeback
  48 #     purple       purpleback
  49 #     red          redback
  50 #     underline
  51 #
  52 # Some style aliases are:
  53 #
  54 #     b       blue                  bb       blueback
  55 #     g       green                 gb       greenback
  56 #     m       magenta               mb       magentaback
  57 #     o       orange                ob       orangeback
  58 #     p       purple                pb       purpleback
  59 #     r       red                   rb       redback
  60 #     i       inverse (highlight)
  61 #     u       underline
  62 
  63 
  64 case "$1" in
  65     -h|--h|-help|--help)
  66         awk '/^# +ecoli /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  67         exit 0
  68     ;;
  69 esac
  70 
  71 case_insensitive=0
  72 case "$1" in
  73     -i|--i|-ins|--ins|-insensitive|--insensitive)
  74         case_insensitive=1
  75         shift
  76     ;;
  77 esac
  78 
  79 [ "$1" = '--' ] && shift
  80 
  81 command='awk'
  82 if [ -p /dev/stdout ] || [ -t 1 ]; then
  83     command='stdbuf -oL awk'
  84 fi
  85 
  86 ${command} -v ci="${case_insensitive}" '
  87     BEGIN {
  88         if (ci == 1 && IGNORECASE == "") {
  89             m = "your `awk` command lacks case-insensitive regex-matching"
  90             printf("\x1b[38;2;204;0;0m%s\x1b[0m\n", m) > "/dev/stderr"
  91             exit 125
  92         }
  93         if (ci == 1) IGNORECASE = 1
  94 
  95         # alias-lookup table
  96         a["r"] = "red"
  97         a["g"] = "green"
  98         a["b"] = "blue"
  99         a["o"] = "orange"
 100         a["p"] = "purple"
 101         a["m"] = "magenta"
 102         a["h"] = "invert"
 103         a["i"] = "invert"
 104         a["u"] = "underline"
 105         a["or"] = "orange"
 106         a["ma"] = "magenta"
 107         a["hi"] = "invert"
 108         a["in"] = "invert"
 109         a["un"] = "underline"
 110         a["inv"] = "invert"
 111         a["mag"] = "magenta"
 112         a["grey"] = "gray"
 113         a["inverse"] = "invert"
 114         a["inverted"] = "invert"
 115         a["hilite"] = "invert"
 116         a["hilited"] = "invert"
 117         a["highlight"] = "invert"
 118         a["highlighted"] = "invert"
 119         a["underlined"] = "underline"
 120         a["bb"] = "blueback"
 121         a["gb"] = "greenback"
 122         a["mb"] = "magentaback"
 123         a["ob"] = "orangeback"
 124         a["pb"] = "purpleback"
 125         a["rb"] = "redback"
 126         a["bg"] = "greenback"
 127         a["bm"] = "magentaback"
 128         a["bo"] = "orangeback"
 129         a["bp"] = "purpleback"
 130         a["br"] = "redback"
 131         a["bluebg"] = "blueback"
 132         a["graybg"] = "grayback"
 133         a["greenbg"] = "greenback"
 134         a["greyback"] = "grayback"
 135         a["greybg"] = "grayback"
 136         a["magentabg"] = "magentaback"
 137         a["orangebg"] = "orangeback"
 138         a["purplebg"] = "purpleback"
 139         a["redbg"] = "redback"
 140         a["magback"] = "magentaback"
 141         a["magbg"] = "magentaback"
 142         a["orback"] = "orangeback"
 143         a["orbg"] = "orangeback"
 144         a["purback"] = "purpleback"
 145         a["purbg"] = "purpleback"
 146 
 147         # style-lookup table
 148         s["red"] = "\x1b[38;2;204;0;0m"
 149         s["green"] = "\x1b[38;2;0;135;95m"
 150         s["blue"] = "\x1b[38;2;0;95;215m"
 151         s["orange"] = "\x1b[38;2;215;95;0m"
 152         s["purple"] = "\x1b[38;2;135;95;255m"
 153         s["magenta"] = "\x1b[38;2;215;0;255m"
 154         s["gray"] = "\x1b[38;2;168;168;168m"
 155         s["bold"] = "\x1b[1m"
 156         s["invert"] = "\x1b[7m"
 157         s["italic"] = "\x1b[3m"
 158         s["strike"] = "\x1b[9m"
 159         s["underline"] = "\x1b[4m"
 160         s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m"
 161         s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m"
 162         s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m"
 163         s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m"
 164         s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m"
 165         s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m"
 166         s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m"
 167 
 168         if (ARGC % 2 != 1) {
 169             printf "\x1b[31mmissing final style name\x1b[0m\n" > "/dev/stderr"
 170             exit 1
 171         }
 172 
 173         for (i = 1; i < ARGC; i++) {
 174             arg = ARGV[i]
 175             delete ARGV[i]
 176 
 177             if (i % 2 != 0) {
 178                 exprs[++n] = arg
 179                 continue
 180             }
 181 
 182             name = arg
 183             # resolve aliases
 184             if (a[name] != "") name = a[name]
 185 
 186             if (s[name] == "") {
 187                 fmt = "\x1b[31mstyle/color %s not supported\x1b[0m\n"
 188                 printf(fmt, name) > "/dev/stderr"
 189                 exit 1
 190             }
 191 
 192             styles[n] = s[name]
 193             resetstyles[n] = "\x1b[0m" styles[n]
 194         }
 195     }
 196 
 197     {
 198         # ignore cursor-movers, style-changers, and OSC sequences
 199         plain = $0
 200         gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 201         gsub(/\x1b\][^\x1b]+\x1b\\/, "", plain)
 202 
 203         for (i = 1; i <= n; i++) {
 204             if (plain ~ exprs[i]) {
 205                 gsub(/\x1b\[0m/, resetstyles[i])
 206                 printf "%s%s\x1b[0m\n", styles[i], $0
 207                 next
 208             }
 209         }
 210 
 211         print
 212     }
 213 ' "$@"