File: stawk.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 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 # stawk [style] [awk condition...] [filenames...] 27 # 28 # 29 # STyle (lines) satisfying an AWK condition/expression, keeping all other 30 # lines the same. When not given a condition, all lines are styled. 31 # 32 # The colors/styles available are: 33 # blue 34 # bold 35 # gray 36 # green 37 # inverse 38 # magenta 39 # orange 40 # purple 41 # red 42 # underline 43 # 44 # Some style aliases are: 45 # b blue 46 # g green 47 # m magenta 48 # o orange 49 # p purple 50 # r red 51 # u underline 52 # hi inverse (highlight) 53 54 55 case "$1" in 56 -h|--h|-help|--help) 57 awk '/^# +stawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 58 exit 0 59 ;; 60 esac 61 62 # if [ $# -lt 1 ]; then 63 # awk '/^# +stawk /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 64 # exit 0 65 # fi 66 67 [ "$1" = "--" ] && shift 68 69 command='awk' 70 if [ -p /dev/stdout ] || [ -t 1 ]; then 71 command='stdbuf -oL awk' 72 fi 73 74 name="${1:-invert}" 75 [ $# -gt 0 ] && shift 76 cond="${1:-1}" 77 [ $# -gt 0 ] && shift 78 79 ${command} -v name="${name}" ' 80 BEGIN { 81 # alias-lookup table 82 a["r"] = "red" 83 a["g"] = "green" 84 a["b"] = "blue" 85 a["o"] = "orange" 86 a["p"] = "purple" 87 a["m"] = "magenta" 88 a["h"] = "invert" 89 a["i"] = "invert" 90 a["u"] = "underline" 91 a["or"] = "orange" 92 a["ma"] = "magenta" 93 a["hi"] = "invert" 94 a["in"] = "invert" 95 a["un"] = "underline" 96 a["inv"] = "invert" 97 a["mag"] = "magenta" 98 a["grey"] = "gray" 99 a["inverse"] = "invert" 100 a["inverted"] = "invert" 101 a["hilite"] = "invert" 102 a["hilited"] = "invert" 103 a["highlight"] = "invert" 104 a["highlighted"] = "invert" 105 a["underlined"] = "underline" 106 a["bb"] = "blueback" 107 a["gb"] = "greenback" 108 a["mb"] = "magentaback" 109 a["ob"] = "orangeback" 110 a["pb"] = "purpleback" 111 a["rb"] = "redback" 112 a["bg"] = "greenback" 113 a["bm"] = "magentaback" 114 a["bo"] = "orangeback" 115 a["bp"] = "purpleback" 116 a["br"] = "redback" 117 a["bluebg"] = "blueback" 118 a["graybg"] = "grayback" 119 a["greenbg"] = "greenback" 120 a["greyback"] = "grayback" 121 a["greybg"] = "grayback" 122 a["magentabg"] = "magentaback" 123 a["orangebg"] = "orangeback" 124 a["purplebg"] = "purpleback" 125 a["redbg"] = "redback" 126 a["magback"] = "magentaback" 127 a["magbg"] = "magentaback" 128 a["orback"] = "orangeback" 129 a["orbg"] = "orangeback" 130 a["purback"] = "purpleback" 131 a["purbg"] = "purpleback" 132 133 # style-lookup table 134 s["red"] = "\x1b[38;2;204;0;0m" 135 s["green"] = "\x1b[38;2;0;135;95m" 136 s["blue"] = "\x1b[38;2;0;95;215m" 137 s["orange"] = "\x1b[38;2;215;95;0m" 138 s["purple"] = "\x1b[38;2;135;95;255m" 139 s["magenta"] = "\x1b[38;2;215;0;255m" 140 s["gray"] = "\x1b[38;2;168;168;168m" 141 s["bold"] = "\x1b[1m" 142 s["invert"] = "\x1b[7m" 143 s["italic"] = "\x1b[3m" 144 s["strike"] = "\x1b[9m" 145 s["underline"] = "\x1b[4m" 146 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 147 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 148 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 149 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 150 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 151 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 152 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 153 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 style = s[name] 166 resetstyle = "\x1b[0m" style 167 } 168 169 { low = lower = tolower($0) } 170 171 '"${cond}"' { 172 gsub(/\x1b\[0m/, resetstyle) 173 printf "%s%s\x1b[0m\n", style, $0 174 next 175 } 176 177 1 178 ' "$@"