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 [ "$1" = "--" ] && shift 65 66 if [ -p 1 ] || [ -t 1 ]; then 67 buffering="stdbuf -oL" 68 else 69 buffering="" 70 fi 71 72 name="${1:-plain}" 73 [ $# -gt 0 ] && shift 74 75 # handle special style-names 76 case "${name}" in 77 plain) 78 # the long AWK script can't handle simply removing all styles 79 "${buffering}" awk ' 80 { 81 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 82 print 83 } 84 ' "$@" 85 exit $? 86 ;; 87 88 keep|same) 89 "${buffering}" awk '{ printf "%s\x1b[0m\n", $0 }' "$@" 90 exit $? 91 ;; 92 esac 93 94 # general case to handle actual styles 95 "${buffering}" awk -v name="${name}" ' 96 BEGIN { 97 orig = name 98 gsub(/^-{1,2}/, "", name) 99 100 # alias-lookup table 101 a["r"] = "red" 102 a["g"] = "green" 103 a["b"] = "blue" 104 a["o"] = "orange" 105 a["p"] = "purple" 106 a["m"] = "magenta" 107 a["h"] = "invert" 108 a["i"] = "invert" 109 a["u"] = "underline" 110 a["or"] = "orange" 111 a["ma"] = "magenta" 112 a["hi"] = "invert" 113 a["in"] = "invert" 114 a["un"] = "underline" 115 a["inv"] = "invert" 116 a["mag"] = "magenta" 117 a["grey"] = "gray" 118 a["inverse"] = "invert" 119 a["inverted"] = "invert" 120 a["hilite"] = "invert" 121 a["hilited"] = "invert" 122 a["highlight"] = "invert" 123 a["highlighted"] = "invert" 124 a["underlined"] = "underline" 125 a["bb"] = "blueback" 126 a["gb"] = "greenback" 127 a["mb"] = "magentaback" 128 a["ob"] = "orangeback" 129 a["pb"] = "purpleback" 130 a["rb"] = "redback" 131 a["bg"] = "greenback" 132 a["bm"] = "magentaback" 133 a["bo"] = "orangeback" 134 a["bp"] = "purpleback" 135 a["br"] = "redback" 136 a["bluebg"] = "blueback" 137 a["graybg"] = "grayback" 138 a["greenbg"] = "greenback" 139 a["greyback"] = "grayback" 140 a["greybg"] = "grayback" 141 a["magentabg"] = "magentaback" 142 a["orangebg"] = "orangeback" 143 a["purplebg"] = "purpleback" 144 a["redbg"] = "redback" 145 a["magback"] = "magentaback" 146 a["magbg"] = "magentaback" 147 a["orback"] = "orangeback" 148 a["orbg"] = "orangeback" 149 a["purback"] = "purpleback" 150 a["purbg"] = "purpleback" 151 152 # style-lookup table 153 s["red"] = "\x1b[38;2;204;0;0m" 154 s["green"] = "\x1b[38;2;0;135;95m" 155 s["blue"] = "\x1b[38;2;0;95;215m" 156 s["orange"] = "\x1b[38;2;215;95;0m" 157 s["purple"] = "\x1b[38;2;135;95;255m" 158 s["magenta"] = "\x1b[38;2;215;0;255m" 159 s["gray"] = "\x1b[38;2;168;168;168m" 160 s["bold"] = "\x1b[1m" 161 s["invert"] = "\x1b[7m" 162 s["italic"] = "\x1b[3m" 163 s["underline"] = "\x1b[4m" 164 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 165 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 166 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 167 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 168 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 169 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 170 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 171 172 # resolve aliases 173 if (a[name] != "") name = a[name] 174 175 # handle unsupported style-names with an error 176 if (s[name] == "") { 177 fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n" 178 printf fmt, orig > "/dev/stderr" 179 exit 1 180 } 181 182 # match ANSI-code to the name 183 style = s[name] 184 } 185 186 # (re)style lines 187 { 188 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 189 printf "%s%s\x1b[0m\n", style, $0 190 } 191 ' "$@"