#!/bin/sh # The MIT License (MIT) # # Copyright © 2024 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. # ecoli [regex/style pairs...] # # Expressions COloring LInes tries to 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) # if [ $# -eq 0 ]; then # awk '/^# +ecoli/, /^$/ { gsub(/^# ?/, ""); print }' "$0" # exit 0 # fi awk ' BEGIN { # 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" # style-lookup table s["red"] = "\x1b[38;5;1m" s["green"] = "\x1b[38;5;29m" s["blue"] = "\x1b[38;5;26m" s["orange"] = "\x1b[38;5;166m" s["purple"] = "\x1b[38;5;99m" s["magenta"] = "\x1b[38;5;165m" s["gray"] = "\x1b[38;5;248m" s["bold"] = "\x1b[1m" s["invert"] = "\x1b[7m" s["italic"] = "\x1b[3m" s["underline"] = "\x1b[4m" n = 0 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] n++ } } { for (i in styles) { if ($0 ~ exprs[i]) { printf "%s%s\x1b[0m\n", styles[i], $0 fflush() next } } print fflush() } ' "$@"