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