File: leak.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 # leak [style-name...]
  27 #
  28 # Emit copies of stdin lines both to stdout and to stderr, thus `leaking`
  29 # the contents going through a pipe of commands, using the leading argument
  30 # as the style/color name to use to decorate the stderr output.
  31 #
  32 # If no style name is given, a default one is chosen.
  33 #
  34 # As its name suggests, its main use is to inspect/debug the intermediate
  35 # stages of a `pipelined` shell command.
  36 #
  37 # Supported style names include
  38 #
  39 # - red     - orange    - bold     - underline
  40 # - green   - magenta   - purple   - invert
  41 # - blue    - gray      - italic
  42 #
  43 # Supported aliases for style names include
  44 #
  45 #   b    (blue)
  46 #   g    (green)
  47 #   h    (hilight/invert)
  48 #   m    (magenta)
  49 #   o    (orange)
  50 #   p    (purple)
  51 #   r    (red)
  52 #   u    (underline)
  53 
  54 
  55 # handle help options
  56 case "$1" in
  57     -h|--h|-help|--help)
  58         awk '/^# +leak/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  59         exit 0
  60     ;;
  61 esac
  62 
  63 name="$(echo "${1:-gray}" | sed 's/^--?//')"
  64 [ $# -gt 0 ] && shift
  65 
  66 # handle special style-names
  67 case "${name}" in
  68     plain)
  69         # the general case can't handle simply removing all styles
  70         awk '{
  71             print
  72 
  73             gsub(/\x1b\[([0-9]*[A-HJKST]|[0-9;]*m)/, "")
  74             print > "/dev/stderr"
  75         }'
  76         exit $?
  77     ;;
  78 
  79     keep)
  80         # the general case handles empty style-strings as errors
  81         awk '{
  82             printf "%s\x1b[0m\n", $0
  83             printf "%s\x1b[0m\n", $0 > "/dev/stderr"
  84         }'
  85         exit $?
  86     ;;
  87 esac
  88 
  89 # general case to handle actual styles
  90 awk -v name="${name}" '
  91 BEGIN {
  92     # pick a default style, when no style is given
  93     if (name == "") name = "gray"
  94     orig = name
  95     gsub(/^-{1,2}/, "", name)
  96 
  97     # alias-lookup table
  98     a["r"] = "red"
  99     a["g"] = "green"
 100     a["b"] = "blue"
 101     a["o"] = "orange"
 102     a["p"] = "purple"
 103     a["m"] = "magenta"
 104     a["h"] = "invert"
 105     a["i"] = "invert"
 106     a["u"] = "underline"
 107     a["or"] = "orange"
 108     a["ma"] = "magenta"
 109     a["hi"] = "invert"
 110     a["in"] = "invert"
 111     a["un"] = "underline"
 112     a["inv"] = "invert"
 113     a["mag"] = "magenta"
 114     a["grey"] = "gray"
 115     a["inverse"] = "invert"
 116     a["inverted"] = "invert"
 117     a["hilite"] = "invert"
 118     a["hilited"] = "invert"
 119     a["highlight"] = "invert"
 120     a["highlighted"] = "invert"
 121     a["underlined"] = "underline"
 122     a["bluebg"] = "blueback"
 123     a["graybg"] = "grayback"
 124     a["greenbg"] = "greenback"
 125     a["magentabg"] = "magentaback"
 126     a["orangebg"] = "orangeback"
 127     a["purplebg"] = "purpleback"
 128     a["redbg"] = "redback"
 129     a["magback"] = "magentaback"
 130     a["magbg"] = "magentaback"
 131     a["orback"] = "orangeback"
 132     a["orbg"] = "orangeback"
 133     a["purback"] = "purpleback"
 134     a["purbg"] = "purpleback"
 135 
 136     # style-lookup table
 137     s["red"] = "\x1b[38;5;1m"
 138     s["green"] = "\x1b[38;5;29m"
 139     s["blue"] = "\x1b[38;5;26m"
 140     s["orange"] = "\x1b[38;5;166m"
 141     s["purple"] = "\x1b[38;5;99m"
 142     s["magenta"] = "\x1b[38;5;165m"
 143     s["gray"] = "\x1b[38;5;248m"
 144     s["bold"] = "\x1b[1m"
 145     s["invert"] = "\x1b[7m"
 146     s["italic"] = "\x1b[3m"
 147     s["underline"] = "\x1b[4m"
 148     s["blueback"] = "\x1b[48;5;26m\x1b[38;5;15m"
 149     s["grayback"] = "\x1b[48;5;248m\x1b[38;5;15m"
 150     s["greenback"] = "\x1b[48;5;29m\x1b[38;5;15m"
 151     s["magentaback"] = "\x1b[48;5;165m\x1b[38;5;15m"
 152     s["orangeback"] = "\x1b[48;5;166m\x1b[38;5;15m"
 153     s["purpleback"] = "\x1b[48;5;99m\x1b[38;5;15m"
 154     s["redback"] = "\x1b[41m\x1b[38;5;15m"
 155 
 156     # resolve aliases
 157     if (a[name] != "") {
 158         name = a[name]
 159     }
 160 
 161     # handle unsupported style-names with an error
 162     if (s[name] == "") {
 163         fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n"
 164         printf fmt, orig > "/dev/stderr"
 165         exit 1
 166     }
 167 
 168     # match ANSI-code to the name
 169     style = s[name]
 170 
 171     # make a style-reset replacement for already-styled lines
 172     rep = "\x1b[0m" style
 173 }
 174 
 175 # (re)style lines
 176 {
 177     print
 178     fflush()
 179     gsub(/\x1b\[0m/, rep)
 180     printf "%s%s\x1b[0m\n", style, $0 > "/dev/stderr"
 181 }
 182 ' "$@"