File: coma.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 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 # coma [options...] [regex/style pairs...]
  27 #
  28 #
  29 # COlor MAtches tries to match each line read from the standard input to the
  30 # regexes given, coloring/styling with the named-style paired to all matching
  31 # regexes, if any. Lines not matching any regex stay the same, lines matching
  32 # multiple regexes are repeated, each time restyled according to the relevant
  33 # style/color.
  34 #
  35 # The options are, available both in single and double-dash versions
  36 #
  37 #   -back, -background, -bg    automatically turn styles into background ones
  38 #   -f, -filter                filter out (ignore) lines with no matches
  39 #   -h, -help                  show this help message
  40 #   -i, -ins, -insensitive     match the regexes given case-insensitively
  41 #
  42 # Some of the colors/styles available are:
  43 #
  44 #     blue         blueback
  45 #     bold
  46 #     gray         grayback
  47 #     green        greenback
  48 #     inverse
  49 #     magenta      magentaback
  50 #     orange       orangeback
  51 #     purple       purpleback
  52 #     red          redback
  53 #     underline
  54 #
  55 # Some style aliases are:
  56 #
  57 #     b       blue                  bb       blueback
  58 #     g       green                 gb       greenback
  59 #     m       magenta               mb       magentaback
  60 #     o       orange                ob       orangeback
  61 #     p       purple                pb       purpleback
  62 #     r       red                   rb       redback
  63 #     i       inverse (highlight)
  64 #     u       underline
  65 
  66 
  67 buf=0
  68 bg=0
  69 ins=0
  70 fil=0
  71 
  72 while [ $# -gt 0 ]; do
  73     case "$1" in
  74         -b|--b|-buffered|--buffered) buf=1; shift; continue ;;
  75 
  76         -back|--back|-background|-background|-bg|--bg)
  77             bg=1; shift; continue
  78         ;;
  79 
  80         -f|--f|-filter|--filter) fil=1; shift; continue ;;
  81 
  82         -fi|-if) fil=1; ins=1; shift; continue ;;
  83 
  84         -h|--h|-help|--help)
  85             awk '/^# +coma /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  86             exit 0
  87         ;;
  88 
  89         -i|--i|-ins|--ins|-insensitive|--insensitive) ins=1; shift; continue ;;
  90 
  91         --) shift; break ;;
  92 
  93         -*)
  94             printf "%s: unsupported option '%s'\n" "$0" "$1" >&2
  95             exit 1
  96         ;;
  97     esac
  98 
  99     break
 100 done
 101 
 102 flush=0
 103 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
 104     flush=1
 105 fi
 106 
 107 awk -v flush="${flush}" -v heavy="${bg}" -v filter="${fil}" -v ci="${ins}" '
 108 BEGIN {
 109     if (ci == 1 && IGNORECASE == "") {
 110         m = "your `awk` command lacks case-insensitive regex-matching"
 111         print(m) > "/dev/stderr"
 112         exit 125
 113     }
 114     if (ci == 1) IGNORECASE = 1
 115 
 116     # alias-lookup table
 117     a["r"] = "red"
 118     a["g"] = "green"
 119     a["b"] = "blue"
 120     a["o"] = "orange"
 121     a["p"] = "purple"
 122     a["m"] = "magenta"
 123     a["h"] = "invert"
 124     a["i"] = "invert"
 125     a["u"] = "underline"
 126     a["or"] = "orange"
 127     a["ma"] = "magenta"
 128     a["hi"] = "invert"
 129     a["in"] = "invert"
 130     a["un"] = "underline"
 131     a["inv"] = "invert"
 132     a["mag"] = "magenta"
 133     a["grey"] = "gray"
 134     a["inverse"] = "invert"
 135     a["inverted"] = "invert"
 136     a["hilite"] = "invert"
 137     a["hilited"] = "invert"
 138     a["highlight"] = "invert"
 139     a["highlighted"] = "invert"
 140     a["underlined"] = "underline"
 141     a["bb"] = "blueback"
 142     a["gb"] = "greenback"
 143     a["mb"] = "magentaback"
 144     a["ob"] = "orangeback"
 145     a["pb"] = "purpleback"
 146     a["rb"] = "redback"
 147     a["bg"] = "greenback"
 148     a["bm"] = "magentaback"
 149     a["bo"] = "orangeback"
 150     a["bp"] = "purpleback"
 151     a["br"] = "redback"
 152     a["bluebg"] = "blueback"
 153     a["graybg"] = "grayback"
 154     a["greenbg"] = "greenback"
 155     a["greyback"] = "grayback"
 156     a["greybg"] = "grayback"
 157     a["magentabg"] = "magentaback"
 158     a["orangebg"] = "orangeback"
 159     a["purplebg"] = "purpleback"
 160     a["redbg"] = "redback"
 161     a["magback"] = "magentaback"
 162     a["magbg"] = "magentaback"
 163     a["orback"] = "orangeback"
 164     a["orbg"] = "orangeback"
 165     a["purback"] = "purpleback"
 166     a["purbg"] = "purpleback"
 167 
 168     # style-lookup table
 169     s["red"] = "\x1b[38;2;204;0;0m"
 170     s["green"] = "\x1b[38;2;0;135;95m"
 171     s["blue"] = "\x1b[38;2;0;95;215m"
 172     s["orange"] = "\x1b[38;2;215;95;0m"
 173     s["purple"] = "\x1b[38;2;135;95;255m"
 174     s["magenta"] = "\x1b[38;2;215;0;255m"
 175     s["gray"] = "\x1b[38;2;168;168;168m"
 176     s["bold"] = "\x1b[1m"
 177     s["invert"] = "\x1b[7m"
 178     s["italic"] = "\x1b[3m"
 179     s["strike"] = "\x1b[9m"
 180     s["underline"] = "\x1b[4m"
 181     s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m"
 182     s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m"
 183     s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m"
 184     s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m"
 185     s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m"
 186     s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m"
 187     s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m"
 188 
 189     if (ARGC % 2 != 1) {
 190         printf "missing final style name\n" > "/dev/stderr"
 191         exit 1
 192     }
 193 
 194     for (i = 1; i < ARGC; i++) {
 195         arg = ARGV[i]
 196         delete ARGV[i]
 197 
 198         if (i % 2 != 0) {
 199             exprs[++n] = arg
 200             continue
 201         }
 202 
 203         name = arg
 204 
 205         # resolve aliases
 206         if (a[name] != "") name = a[name]
 207 
 208         style = s[name]
 209 
 210         if (heavy && (name !~ /back/)) {
 211             name = name "back"
 212             style = s[name]
 213         }
 214 
 215         if (style == "") {
 216             fmt = "style/color \"%s\" not supported\n"
 217             printf(fmt, name) > "/dev/stderr"
 218             exit 1
 219         }
 220 
 221         replacements[n] = style "&\x1b[0m"
 222     }
 223 }
 224 
 225 {
 226     # ignore cursor-movers, style-changers, and OSC sequences
 227     plain = $0
 228     gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain)
 229     gsub(/\x1b\][^\x1b]+\x1b\\/, "", plain)
 230 
 231     matches = 0
 232     for (i = 1; i <= n; i++) {
 233         if (plain ~ exprs[i]) {
 234             matches++
 235             restyled = plain
 236             gsub(exprs[i], replacements[i], restyled)
 237             print restyled
 238         }
 239     }
 240 
 241     if (matches > 0) {
 242         if (flush) fflush()
 243         next
 244     } else if (filter) next
 245 
 246     print plain
 247     if (flush) fflush()
 248 }
 249 ' "$@"