#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # style [style-name] [filenames...] # # Add ANSI-style codes to all input lines, using the leading argument # as the style/color name to use. # # Supported style names include # # - red - orange - bold - underline # - green - magenta - purple - invert # - blue - gray - italic # # Supported aliases for style names include # # b (blue) # g (green) # h (hilight/invert) # m (magenta) # o (orange) # p (purple) # r (red) # u (underline) if [ $# -eq 0 ]; then awk '/^# +style /, /^$/ { gsub(/^# ?/, ""); print }' "$0" # show error after the help message, so it's noticed printf "\x1b[31mmissing style/color name\x1b[0m\n" > /dev/stderr exit 1 fi # handle help options case "$1" in -h|--h|-help|--help) awk '/^# +style /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac name="${1}" [ $# -gt 0 ] && shift # handle special style-names case "${name}" in plain) # the general case can't handle simply removing all styles awk '{ gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") print; fflush() }' "$@" exit $? ;; keep) # the general case handles empty style-strings as errors awk '{ printf "%s\x1b[0m\n", $0; fflush() }' "$@" exit $? ;; esac # general case to handle actual styles awk -v name="${name}" ' BEGIN { orig = name gsub(/^-{1,2}/, "", name) # alias-lookup table a["r"] = "red" a["g"] = "green" a["b"] = "blue" a["o"] = "orange" a["p"] = "purple" a["m"] = "magenta" a["h"] = "invert" a["i"] = "invert" a["u"] = "underline" a["or"] = "orange" a["ma"] = "magenta" a["hi"] = "invert" a["in"] = "invert" a["un"] = "underline" a["inv"] = "invert" a["mag"] = "magenta" a["grey"] = "gray" a["inverse"] = "invert" a["inverted"] = "invert" a["hilite"] = "invert" a["hilited"] = "invert" a["highlight"] = "invert" a["highlighted"] = "invert" a["underlined"] = "underline" a["bb"] = "blueback" a["gb"] = "greenback" a["mb"] = "magentaback" a["ob"] = "orangeback" a["pb"] = "purpleback" a["rb"] = "redback" a["bg"] = "greenback" a["bm"] = "magentaback" a["bo"] = "orangeback" a["bp"] = "purpleback" a["br"] = "redback" a["bluebg"] = "blueback" a["graybg"] = "grayback" a["greenbg"] = "greenback" a["greyback"] = "grayback" a["greybg"] = "grayback" a["magentabg"] = "magentaback" a["orangebg"] = "orangeback" a["purplebg"] = "purpleback" a["redbg"] = "redback" a["magback"] = "magentaback" a["magbg"] = "magentaback" a["orback"] = "orangeback" a["orbg"] = "orangeback" a["purback"] = "purpleback" a["purbg"] = "purpleback" # style-lookup table s["red"] = "\x1b[38;2;204;0;0m" s["green"] = "\x1b[38;2;0;135;95m" s["blue"] = "\x1b[38;2;0;95;215m" s["orange"] = "\x1b[38;2;215;95;0m" s["purple"] = "\x1b[38;2;135;95;255m" s["magenta"] = "\x1b[38;2;215;0;255m" s["gray"] = "\x1b[38;2;168;168;168m" s["bold"] = "\x1b[1m" s["invert"] = "\x1b[7m" s["italic"] = "\x1b[3m" s["underline"] = "\x1b[4m" s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" # resolve aliases if (a[name] != "") name = a[name] # handle unsupported style-names with an error if (s[name] == "") { fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n" printf fmt, orig > "/dev/stderr" exit 1 } # match ANSI-code to the name style = s[name] } # (re)style lines { gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") printf "%s%s\x1b[0m\n", style, $0; fflush() } ' "$@"