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