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         # show help message, extracting the info-comment at the start
  59         # of this file, and quit
  60         awk '/^# +leak/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  61         exit 0
  62     ;;
  63 esac
  64 
  65 name="${1}"
  66 
  67 # handle special style-names
  68 case "${name}" in
  69     plain)
  70         # the general case can't handle simply removing all styles
  71         awk '{
  72             print
  73             fflush()
  74 
  75             gsub(/\x1b\[([0-9]*[A-HJKST]|[0-9;]*m)/, "")
  76             print > "/dev/stderr"
  77             fflush("/dev/stderr")
  78         }'
  79         exit $?
  80     ;;
  81 
  82     keep)
  83         # the general case handles empty style-strings as errors
  84         awk '{
  85             printf "%s\x1b[0m\n", $0
  86             fflush()
  87             printf "%s\x1b[0m\n", $0 > "/dev/stderr"
  88             fflush("/dev/stderr")
  89         }'
  90         exit $?
  91     ;;
  92 esac
  93 
  94 # general case to handle actual styles
  95 awk -v name="${name}" '
  96     BEGIN {
  97         # pick a default style, when no style is given
  98         if (name == "") name = "blue"
  99 
 100         # alias-lookup table
 101         a["r"] = "red"
 102         a["g"] = "green"
 103         a["b"] = "blue"
 104         a["o"] = "orange"
 105         a["p"] = "purple"
 106         a["m"] = "magenta"
 107         a["h"] = "invert"
 108         a["i"] = "invert"
 109         a["u"] = "underline"
 110         a["or"] = "orange"
 111         a["ma"] = "magenta"
 112         a["hi"] = "invert"
 113         a["in"] = "invert"
 114         a["un"] = "underline"
 115         a["inv"] = "invert"
 116         a["mag"] = "magenta"
 117         a["grey"] = "gray"
 118         a["inverse"] = "invert"
 119         a["inverted"] = "invert"
 120         a["hilite"] = "invert"
 121         a["hilited"] = "invert"
 122         a["highlight"] = "invert"
 123         a["highlighted"] = "invert"
 124         a["underlined"] = "underline"
 125 
 126         # style-lookup table
 127         s["red"] = "\x1b[38;5;1m"
 128         s["green"] = "\x1b[38;5;29m"
 129         s["blue"] = "\x1b[38;5;26m"
 130         s["orange"] = "\x1b[38;5;166m"
 131         s["purple"] = "\x1b[38;5;99m"
 132         s["magenta"] = "\x1b[38;5;165m"
 133         s["gray"] = "\x1b[38;5;249m"
 134         s["bold"] = "\x1b[1m"
 135         s["invert"] = "\x1b[7m"
 136         s["italic"] = "\x1b[3m"
 137         s["underline"] = "\x1b[4m"
 138 
 139         # resolve aliases
 140         orig = name
 141         if (a[name] != "") {
 142             name = a[name]
 143         }
 144 
 145         # handle unsupported style-names with an error
 146         if (s[name] == "") {
 147             fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n"
 148             printf fmt, orig > "/dev/stderr"
 149             exit 1
 150         }
 151 
 152         # match ANSI-code to the name
 153         style = s[name]
 154 
 155         # make a style-reset replacement for already-styled lines
 156         rep = "\x1b[0m" style
 157     }
 158 
 159     # (re)style lines
 160     {
 161         print
 162         fflush()
 163         gsub(/\x1b\[0m/, rep)
 164         printf "%s%s\x1b[0m\n", style, $0 > "/dev/stderr"
 165         fflush("/dev/stderr")
 166     }
 167 '