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