File: style.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 # style [style-name] [filenames...] 27 # 28 # Add ANSI-style codes to all input lines, using the leading argument 29 # as the style/color name to use. 30 # 31 # Supported style names include 32 # 33 # - red - orange - bold - underline 34 # - green - magenta - purple - invert 35 # - blue - gray - italic 36 # 37 # Supported aliases for style names include 38 # 39 # b (blue) 40 # g (green) 41 # h (hilight/invert) 42 # m (magenta) 43 # o (orange) 44 # p (purple) 45 # r (red) 46 # u (underline) 47 48 49 if [ $# -eq 0 ]; then 50 awk '/^# +style /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 51 52 # show error after the help message, so it's noticed 53 printf "\x1b[31mmissing style/color name\x1b[0m\n" > /dev/stderr 54 exit 1 55 fi 56 57 case "$1" in 58 -h|--h|-help|--help) 59 awk '/^# +style /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 60 exit 0 61 ;; 62 esac 63 64 name="${1}" 65 [ $# -gt 0 ] && shift 66 67 # handle special style-names 68 case "${name}" in 69 plain) 70 # the general case can't handle simply removing all styles 71 awk '{ 72 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 73 print; fflush() 74 }' "$@" 75 exit $? 76 ;; 77 78 keep) 79 # the general case handles empty style-strings as errors 80 awk '{ printf "%s\x1b[0m\n", $0; fflush() }' "$@" 81 exit $? 82 ;; 83 esac 84 85 # general case to handle actual styles 86 awk -v name="${name}" ' 87 BEGIN { 88 orig = name 89 gsub(/^-{1,2}/, "", name) 90 91 # alias-lookup table 92 a["r"] = "red" 93 a["g"] = "green" 94 a["b"] = "blue" 95 a["o"] = "orange" 96 a["p"] = "purple" 97 a["m"] = "magenta" 98 a["h"] = "invert" 99 a["i"] = "invert" 100 a["u"] = "underline" 101 a["or"] = "orange" 102 a["ma"] = "magenta" 103 a["hi"] = "invert" 104 a["in"] = "invert" 105 a["un"] = "underline" 106 a["inv"] = "invert" 107 a["mag"] = "magenta" 108 a["grey"] = "gray" 109 a["inverse"] = "invert" 110 a["inverted"] = "invert" 111 a["hilite"] = "invert" 112 a["hilited"] = "invert" 113 a["highlight"] = "invert" 114 a["highlighted"] = "invert" 115 a["underlined"] = "underline" 116 a["bb"] = "blueback" 117 a["gb"] = "greenback" 118 a["mb"] = "magentaback" 119 a["ob"] = "orangeback" 120 a["pb"] = "purpleback" 121 a["rb"] = "redback" 122 a["bg"] = "greenback" 123 a["bm"] = "magentaback" 124 a["bo"] = "orangeback" 125 a["bp"] = "purpleback" 126 a["br"] = "redback" 127 a["bluebg"] = "blueback" 128 a["graybg"] = "grayback" 129 a["greenbg"] = "greenback" 130 a["greyback"] = "grayback" 131 a["greybg"] = "grayback" 132 a["magentabg"] = "magentaback" 133 a["orangebg"] = "orangeback" 134 a["purplebg"] = "purpleback" 135 a["redbg"] = "redback" 136 a["magback"] = "magentaback" 137 a["magbg"] = "magentaback" 138 a["orback"] = "orangeback" 139 a["orbg"] = "orangeback" 140 a["purback"] = "purpleback" 141 a["purbg"] = "purpleback" 142 143 # style-lookup table 144 s["red"] = "\x1b[38;2;204;0;0m" 145 s["green"] = "\x1b[38;2;0;135;95m" 146 s["blue"] = "\x1b[38;2;0;95;215m" 147 s["orange"] = "\x1b[38;2;215;95;0m" 148 s["purple"] = "\x1b[38;2;135;95;255m" 149 s["magenta"] = "\x1b[38;2;215;0;255m" 150 s["gray"] = "\x1b[38;2;168;168;168m" 151 s["bold"] = "\x1b[1m" 152 s["invert"] = "\x1b[7m" 153 s["italic"] = "\x1b[3m" 154 s["underline"] = "\x1b[4m" 155 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 156 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 157 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 158 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 159 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 160 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 161 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 162 163 # resolve aliases 164 if (a[name] != "") name = a[name] 165 166 # handle unsupported style-names with an error 167 if (s[name] == "") { 168 fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n" 169 printf fmt, orig > "/dev/stderr" 170 exit 1 171 } 172 173 # match ANSI-code to the name 174 style = s[name] 175 } 176 177 # (re)style lines 178 { 179 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 180 printf "%s%s\x1b[0m\n", style, $0; fflush() 181 } 182 ' "$@"