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