File: ecoli.awk 1 #!/usr/bin/awk -f 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2024 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 [regex/style pairs...] 27 # 28 # Expressions COloring LInes tries to match each line to the regexes given, 29 # coloring/styling with the named-style associated to the first match, if 30 # any. 31 # 32 # Lines not matching any regex stay verbatim. 33 # 34 # The colors/styles available are: 35 # blue 36 # bold 37 # gray 38 # green 39 # inverse 40 # magenta 41 # orange 42 # purple 43 # red 44 # underline 45 # 46 # Some style aliases are: 47 # b blue 48 # g green 49 # m magenta 50 # o orange 51 # p purple 52 # r red 53 # u underline 54 # hi inverse (highlight) 55 56 57 BEGIN { 58 # alias-lookup table 59 a["r"] = "red" 60 a["g"] = "green" 61 a["b"] = "blue" 62 a["o"] = "orange" 63 a["p"] = "purple" 64 a["m"] = "magenta" 65 a["h"] = "invert" 66 a["i"] = "invert" 67 a["u"] = "underline" 68 a["or"] = "orange" 69 a["ma"] = "magenta" 70 a["hi"] = "invert" 71 a["in"] = "invert" 72 a["un"] = "underline" 73 a["inv"] = "invert" 74 a["mag"] = "magenta" 75 a["grey"] = "gray" 76 a["inverse"] = "invert" 77 a["inverted"] = "invert" 78 a["hilite"] = "invert" 79 a["hilited"] = "invert" 80 a["highlight"] = "invert" 81 a["highlighted"] = "invert" 82 a["underlined"] = "underline" 83 a["bb"] = "blueback" 84 a["gb"] = "greenback" 85 a["mb"] = "magentaback" 86 a["ob"] = "orangeback" 87 a["pb"] = "purpleback" 88 a["br"] = "redback" 89 a["bg"] = "greenback" 90 a["bm"] = "magentaback" 91 a["bo"] = "orangeback" 92 a["bp"] = "purpleback" 93 a["br"] = "redback" 94 a["bluebg"] = "blueback" 95 a["graybg"] = "grayback" 96 a["greenbg"] = "greenback" 97 a["greyback"] = "grayback" 98 a["greybg"] = "grayback" 99 a["magentabg"] = "magentaback" 100 a["orangebg"] = "orangeback" 101 a["purplebg"] = "purpleback" 102 a["redbg"] = "redback" 103 a["magback"] = "magentaback" 104 a["magbg"] = "magentaback" 105 a["orback"] = "orangeback" 106 a["orbg"] = "orangeback" 107 a["purback"] = "purpleback" 108 a["purbg"] = "purpleback" 109 110 # style-lookup table 111 s["red"] = "\x1b[38;2;204;0;0m" 112 s["green"] = "\x1b[38;2;0;135;95m" 113 s["blue"] = "\x1b[38;2;0;95;215m" 114 s["orange"] = "\x1b[38;2;215;95;0m" 115 s["purple"] = "\x1b[38;2;135;95;255m" 116 s["magenta"] = "\x1b[38;2;215;0;255m" 117 s["gray"] = "\x1b[38;2;168;168;168m" 118 s["bold"] = "\x1b[1m" 119 s["invert"] = "\x1b[7m" 120 s["italic"] = "\x1b[3m" 121 s["strike"] = "\x1b[9m" 122 s["underline"] = "\x1b[4m" 123 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 124 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 125 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 126 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 127 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 128 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 129 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 130 131 if (ARGC % 2 != 1) { 132 printf "\x1b[31mmissing final style name\x1b[0m\n" > "/dev/stderr" 133 exit 1 134 } 135 136 for (i = 1; i < ARGC; i++) { 137 arg = ARGV[i] 138 delete ARGV[i] 139 140 if (i % 2 != 0) { 141 exprs[++n] = arg 142 continue 143 } 144 145 name = arg 146 # resolve aliases 147 if (a[name] != "") { 148 name = a[name] 149 } 150 151 if (s[name] == "") { 152 fmt = "\x1b[31mstyle/color %s not supported\x1b[0m\n" 153 printf(fmt, name) > "/dev/stderr" 154 exit 1 155 } 156 157 styles[n] = s[name] 158 resetstyles[n] = "\x1b[0m" styles[n] 159 } 160 } 161 162 { 163 for (i = 1; i <= n; i++) { 164 if ($0 ~ exprs[i]) { 165 gsub(/\x1b\[0m/, resetstyles[i]) 166 printf "%s%s\x1b[0m\n", styles[i], $0; fflush() 167 next 168 } 169 } 170 171 print; fflush() 172 }