#!/usr/bin/awk -f # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # iecoli [regex/style pairs...] # # Insensitive Expressions COloring LInes tries to case-insensitively match # each line to the regexes given, coloring/styling with the named-style # associated to the first match, if any. # # Lines not matching any regex stay verbatim. # # The colors/styles available are: # blue # bold # gray # green # inverse # magenta # orange # purple # red # underline # # Some style aliases are: # b blue # g green # m magenta # o orange # p purple # r red # u underline # hi inverse (highlight) BEGIN { # enable case-insensitive regex-matching, or complain if unavailable if (IGNORECASE == "") { msg = "this variant of AWK lacks case-insensitive regex-matching" printf("\x1b[31m%s\x1b[0m\n", msg) > "/dev/stderr" exit 125 } IGNORECASE = 1 # alias-lookup table a["r"] = "red" a["g"] = "green" a["b"] = "blue" a["o"] = "orange" a["p"] = "purple" a["m"] = "magenta" a["h"] = "invert" a["i"] = "invert" a["u"] = "underline" a["or"] = "orange" a["ma"] = "magenta" a["hi"] = "invert" a["in"] = "invert" a["un"] = "underline" a["inv"] = "invert" a["mag"] = "magenta" a["grey"] = "gray" a["inverse"] = "invert" a["inverted"] = "invert" a["hilite"] = "invert" a["hilited"] = "invert" a["highlight"] = "invert" a["highlighted"] = "invert" a["underlined"] = "underline" a["bb"] = "blueback" a["gb"] = "greenback" a["mb"] = "magentaback" a["ob"] = "orangeback" a["pb"] = "purpleback" a["br"] = "redback" a["bg"] = "greenback" a["bm"] = "magentaback" a["bo"] = "orangeback" a["bp"] = "purpleback" a["br"] = "redback" a["bluebg"] = "blueback" a["graybg"] = "grayback" a["greenbg"] = "greenback" a["greyback"] = "grayback" a["greybg"] = "grayback" a["magentabg"] = "magentaback" a["orangebg"] = "orangeback" a["purplebg"] = "purpleback" a["redbg"] = "redback" a["magback"] = "magentaback" a["magbg"] = "magentaback" a["orback"] = "orangeback" a["orbg"] = "orangeback" a["purback"] = "purpleback" a["purbg"] = "purpleback" # style-lookup table s["red"] = "\x1b[38;2;204;0;0m" s["green"] = "\x1b[38;2;0;135;95m" s["blue"] = "\x1b[38;2;0;95;215m" s["orange"] = "\x1b[38;2;215;95;0m" s["purple"] = "\x1b[38;2;135;95;255m" s["magenta"] = "\x1b[38;2;215;0;255m" s["gray"] = "\x1b[38;2;168;168;168m" s["bold"] = "\x1b[1m" s["invert"] = "\x1b[7m" s["italic"] = "\x1b[3m" s["strike"] = "\x1b[9m" s["underline"] = "\x1b[4m" s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" if (ARGC % 2 != 1) { printf "\x1b[31mmissing final style name\x1b[0m\n" > "/dev/stderr" exit 1 } for (i = 1; i < ARGC; i++) { arg = ARGV[i] delete ARGV[i] if (i % 2 != 0) { exprs[++n] = arg continue } name = arg # resolve aliases if (a[name] != "") { name = a[name] } if (s[name] == "") { fmt = "\x1b[31mstyle/color %s not supported\x1b[0m\n" printf(fmt, name) > "/dev/stderr" exit 1 } styles[n] = s[name] resetstyles[n] = "\x1b[0m" styles[n] } } { for (i = 1; i <= n; i++) { if ($0 ~ exprs[i]) { gsub(/\x1b\[0m/, resetstyles[i]) printf "%s%s\x1b[0m\n", styles[i], $0; fflush() next } } print; fflush() }