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