File: ecoli.sh 1 #!/bin/sh 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 # ecoli [regex/style pairs...] 27 # 28 # Expressions COloring LInes tries to match each line to the regexes given, 29 # coloring/styling with the named-style associated to the first match, if 30 # 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 '/^# +ecoli/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 59 # exit 0 60 # fi 61 62 63 awk ' 64 BEGIN { 65 # alias-lookup table 66 a["r"] = "red" 67 a["g"] = "green" 68 a["b"] = "blue" 69 a["o"] = "orange" 70 a["p"] = "purple" 71 a["m"] = "magenta" 72 a["h"] = "invert" 73 a["i"] = "invert" 74 a["u"] = "underline" 75 a["or"] = "orange" 76 a["ma"] = "magenta" 77 a["hi"] = "invert" 78 a["in"] = "invert" 79 a["un"] = "underline" 80 a["inv"] = "invert" 81 a["mag"] = "magenta" 82 a["grey"] = "gray" 83 a["inverse"] = "invert" 84 a["inverted"] = "invert" 85 a["hilite"] = "invert" 86 a["hilited"] = "invert" 87 a["highlight"] = "invert" 88 a["highlighted"] = "invert" 89 a["underlined"] = "underline" 90 91 # style-lookup table 92 s["red"] = "\x1b[38;5;1m" 93 s["green"] = "\x1b[38;5;29m" 94 s["blue"] = "\x1b[38;5;26m" 95 s["orange"] = "\x1b[38;5;166m" 96 s["purple"] = "\x1b[38;5;99m" 97 s["magenta"] = "\x1b[38;5;165m" 98 s["gray"] = "\x1b[38;5;248m" 99 s["bold"] = "\x1b[1m" 100 s["invert"] = "\x1b[7m" 101 s["italic"] = "\x1b[3m" 102 s["underline"] = "\x1b[4m" 103 104 n = 0 105 if (ARGC % 2 != 1) { 106 printf "\x1b[31mmissing final style name\x1b[0m\n" > "/dev/stderr" 107 exit 1 108 } 109 110 for (i = 1; i < ARGC; i++) { 111 arg = ARGV[i] 112 delete ARGV[i] 113 114 if (i % 2 != 0) { 115 exprs[n] = arg 116 continue 117 } 118 119 name = arg 120 # resolve aliases 121 if (a[name] != "") { 122 name = a[name] 123 } 124 125 if (s[name] == "") { 126 fmt = "\x1b[31mstyle/color %s not supported\x1b[0m\n" 127 printf(fmt, name) > "/dev/stderr" 128 exit 1 129 } 130 131 styles[n] = s[name] 132 n++ 133 } 134 } 135 136 { 137 for (i in styles) { 138 if ($0 ~ exprs[i]) { 139 printf "%s%s\x1b[0m\n", styles[i], $0 140 fflush() 141 next 142 } 143 } 144 145 print 146 fflush() 147 } 148 ' "$@"