File: style.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 # 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 # show help message, extracting the info-comment at the start 51 # of this file, and quit 52 awk '/^# +style/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 53 54 # show error after the help message, so it's noticed 55 printf "\x1b[31mmissing style/color name\x1b[0m\n" > /dev/stderr 56 exit 1 57 fi 58 59 # handle help options 60 case "$1" in 61 -h|--h|-help|--help) 62 # show help message, extracting the info-comment at the start 63 # of this file, and quit 64 awk '/^# +style/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 65 exit 0 66 ;; 67 esac 68 69 name="${1}" 70 shift 71 72 # handle special style-names 73 case "${name}" in 74 plain) 75 # the general case can't handle simply removing all styles 76 awk '{ 77 gsub(/\x1b\[([0-9]*[A-HJKST]|[0-9;]*m)/, ""); print; fflush() 78 }' "$@" 79 exit $? 80 ;; 81 82 keep) 83 # the general case handles empty style-strings as errors 84 awk '{ printf "%s\x1b[0m\n", $0; fflush() }' "$@" 85 exit $? 86 ;; 87 esac 88 89 # general case to handle actual styles 90 awk -v name="${name}" ' 91 BEGIN { 92 # alias-lookup table 93 a["r"] = "red" 94 a["g"] = "green" 95 a["b"] = "blue" 96 a["o"] = "orange" 97 a["p"] = "purple" 98 a["m"] = "magenta" 99 a["h"] = "invert" 100 a["i"] = "invert" 101 a["u"] = "underline" 102 a["or"] = "orange" 103 a["ma"] = "magenta" 104 a["hi"] = "invert" 105 a["in"] = "invert" 106 a["un"] = "underline" 107 a["inv"] = "invert" 108 a["mag"] = "magenta" 109 a["grey"] = "gray" 110 a["inverse"] = "invert" 111 a["inverted"] = "invert" 112 a["hilite"] = "invert" 113 a["hilited"] = "invert" 114 a["highlight"] = "invert" 115 a["highlighted"] = "invert" 116 a["underlined"] = "underline" 117 118 # style-lookup table 119 s["red"] = "\x1b[38;5;1m" 120 s["green"] = "\x1b[38;5;29m" 121 s["blue"] = "\x1b[38;5;26m" 122 s["orange"] = "\x1b[38;5;166m" 123 s["purple"] = "\x1b[38;5;99m" 124 s["magenta"] = "\x1b[38;5;165m" 125 s["gray"] = "\x1b[38;5;249m" 126 s["bold"] = "\x1b[1m" 127 s["invert"] = "\x1b[7m" 128 s["italic"] = "\x1b[3m" 129 s["underline"] = "\x1b[4m" 130 131 # resolve aliases 132 orig = name 133 if (a[name] != "") { 134 name = a[name] 135 } 136 137 # handle unsupported style-names with an error 138 if (s[name] == "") { 139 fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n" 140 printf fmt, orig > "/dev/stderr" 141 exit 1 142 } 143 144 # match ANSI-code to the name 145 style = s[name] 146 147 # make a style-reset replacement for already-styled lines 148 rep = "\x1b[0m" style 149 } 150 151 # (re)style lines 152 { 153 gsub(/\x1b\[0m/, rep) 154 printf "%s%s\x1b[0m\n", style, $0 155 fflush() 156 } 157 ' "$@"