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