File: iecoli.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-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 # iecoli [regex/style pairs...]
  27 #
  28 # Insensitive Expressions COloring LInes tries to case-insensitively match
  29 # each line to the regexes given, coloring/styling with the named-style
  30 # associated to the first match, if 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 # if [ $# -eq 0 ]; then
  58 #     awk '/^# +iecoli /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  59 #     exit 0
  60 # fi
  61 
  62 
  63 awk '
  64 BEGIN {
  65     # enable case-insensitive regex-matching, or complain if unavailable
  66     if (IGNORECASE == "") {
  67         msg = "this variant of AWK lacks case-insensitive regex-matching"
  68         printf("\x1b[31m%s\x1b[0m\n", msg) > "/dev/stderr"
  69         exit 125
  70     }
  71     IGNORECASE = 1
  72 
  73     # alias-lookup table
  74     a["r"] = "red"
  75     a["g"] = "green"
  76     a["b"] = "blue"
  77     a["o"] = "orange"
  78     a["p"] = "purple"
  79     a["m"] = "magenta"
  80     a["h"] = "invert"
  81     a["i"] = "invert"
  82     a["u"] = "underline"
  83     a["or"] = "orange"
  84     a["ma"] = "magenta"
  85     a["hi"] = "invert"
  86     a["in"] = "invert"
  87     a["un"] = "underline"
  88     a["inv"] = "invert"
  89     a["mag"] = "magenta"
  90     a["grey"] = "gray"
  91     a["inverse"] = "invert"
  92     a["inverted"] = "invert"
  93     a["hilite"] = "invert"
  94     a["hilited"] = "invert"
  95     a["highlight"] = "invert"
  96     a["highlighted"] = "invert"
  97     a["underlined"] = "underline"
  98     a["bb"] = "blueback"
  99     a["gb"] = "greenback"
 100     a["mb"] = "magentaback"
 101     a["ob"] = "orangeback"
 102     a["pb"] = "purpleback"
 103     a["br"] = "redback"
 104     a["bg"] = "greenback"
 105     a["bm"] = "magentaback"
 106     a["bo"] = "orangeback"
 107     a["bp"] = "purpleback"
 108     a["br"] = "redback"
 109     a["bluebg"] = "blueback"
 110     a["graybg"] = "grayback"
 111     a["greenbg"] = "greenback"
 112     a["greyback"] = "grayback"
 113     a["greybg"] = "grayback"
 114     a["magentabg"] = "magentaback"
 115     a["orangebg"] = "orangeback"
 116     a["purplebg"] = "purpleback"
 117     a["redbg"] = "redback"
 118     a["magback"] = "magentaback"
 119     a["magbg"] = "magentaback"
 120     a["orback"] = "orangeback"
 121     a["orbg"] = "orangeback"
 122     a["purback"] = "purpleback"
 123     a["purbg"] = "purpleback"
 124 
 125     # style-lookup table
 126     s["red"] = "\x1b[38;2;204;0;0m"
 127     s["green"] = "\x1b[38;2;0;135;95m"
 128     s["blue"] = "\x1b[38;2;0;95;215m"
 129     s["orange"] = "\x1b[38;2;215;95;0m"
 130     s["purple"] = "\x1b[38;2;135;95;255m"
 131     s["magenta"] = "\x1b[38;2;215;0;255m"
 132     s["gray"] = "\x1b[38;2;168;168;168m"
 133     s["bold"] = "\x1b[1m"
 134     s["invert"] = "\x1b[7m"
 135     s["italic"] = "\x1b[3m"
 136     s["strike"] = "\x1b[9m"
 137     s["underline"] = "\x1b[4m"
 138     s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m"
 139     s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m"
 140     s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m"
 141     s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m"
 142     s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m"
 143     s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m"
 144     s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m"
 145 
 146     if (ARGC % 2 != 1) {
 147         printf "\x1b[31mmissing final style name\x1b[0m\n" > "/dev/stderr"
 148         exit 1
 149     }
 150 
 151     for (i = 1; i < ARGC; i++) {
 152         arg = ARGV[i]
 153         delete ARGV[i]
 154 
 155         if (i % 2 != 0) {
 156             exprs[++n] = arg
 157             continue
 158         }
 159 
 160         name = arg
 161         # resolve aliases
 162         if (a[name] != "") {
 163             name = a[name]
 164         }
 165 
 166         if (s[name] == "") {
 167             fmt = "\x1b[31mstyle/color %s not supported\x1b[0m\n"
 168             printf(fmt, name) > "/dev/stderr"
 169             exit 1
 170         }
 171 
 172         styles[n] = s[name]
 173         resetstyles[n] = "\x1b[0m" styles[n]
 174     }
 175 }
 176 
 177 {
 178     for (i = 1; i <= n; i++) {
 179         if ($0 ~ exprs[i]) {
 180             gsub(/\x1b\[0m/, resetstyles[i])
 181             printf "%s%s\x1b[0m\n", styles[i], $0; fflush()
 182             next
 183         }
 184     }
 185 
 186     print; fflush()
 187 }
 188 ' "$@"