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