File: leak.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 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 [options...] [style name...] [files...] 27 # 28 # Emit copies of input 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 # This tool's main use-case is to inspect/debug the intermediate stages of a 33 # `pipelined` shell command. 34 # 35 # When variable NO_COLOR is declared and set to 1, an invert/highlight style 36 # is used instead of colors. 37 # 38 # The only option available is to show this help message, using any of 39 # `-h`, `--h`, `-help`, or `--help`, without the quotes. 40 # 41 # Supported style names include 42 # 43 # - red - orange - bold - underline 44 # - green - magenta - purple - invert 45 # - blue - gray - italic 46 # 47 # Supported aliases for style names include 48 # 49 # b (blue) bb (blue background) 50 # g (green) gb (green background) 51 # h (hilight/invert) 52 # m (magenta) mb (magenta background) 53 # o (orange) ob (orange background) 54 # p (purple) pb (purple background) 55 # r (red) rb (red background) 56 # u (underline) 57 58 59 buffered=0 60 61 case "$1" in 62 -b|--b|-buffered|--buffered) 63 buffered=1 64 shift 65 ;; 66 67 -h|--h|-help|--help) 68 awk '/^# +leak /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 69 exit 0 70 ;; 71 esac 72 73 name="$(echo "${1:-gray}" | sed 's/^--?//')" 74 [ $# -gt 0 ] && shift 75 76 [ "$1" = '--' ] && shift 77 78 # show all non-existing files given 79 failed=0 80 for arg in "$@"; do 81 if [ "${arg}" = "-" ]; then 82 continue 83 fi 84 if [ ! -e "${arg}" ]; then 85 printf "no file named \"%s\"\n" "${arg}" >&2 86 failed=1 87 fi 88 done 89 90 if [ "${failed}" -gt 0 ]; then 91 exit 2 92 fi 93 94 flush=0 95 if [ "${buffered}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then 96 flush=1 97 fi 98 99 if [ "${NO_COLOR}" = 1 ]; then 100 name='inverse' 101 fi 102 103 # handle special style-names 104 case "${name}" in 105 plain) 106 # the long AWK script can't handle simply removing all styles 107 awk -v flush="${flush}" ' 108 { 109 print 110 if (flush) fflush() 111 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 112 print > "/dev/stderr" 113 } 114 ' "$@" 115 exit $? 116 ;; 117 118 keep|same) 119 awk -v flush="${flush}" ' 120 { 121 print 122 if (flush) fflush() 123 print > "/dev/stderr" 124 } 125 ' "$@" 126 exit $? 127 ;; 128 esac 129 130 # general case to handle actual styles 131 awk -v flush="${flush}" -v name="${name}" ' 132 BEGIN { 133 # pick a default style, when no style is given 134 if (name == "") name = "gray" 135 orig = name 136 gsub(/^-{1,2}/, "", name) 137 138 # alias-lookup table 139 a["r"] = "red" 140 a["g"] = "green" 141 a["b"] = "blue" 142 a["o"] = "orange" 143 a["p"] = "purple" 144 a["m"] = "magenta" 145 a["h"] = "invert" 146 a["i"] = "invert" 147 a["u"] = "underline" 148 a["or"] = "orange" 149 a["ma"] = "magenta" 150 a["hi"] = "invert" 151 a["in"] = "invert" 152 a["un"] = "underline" 153 a["inv"] = "invert" 154 a["mag"] = "magenta" 155 a["grey"] = "gray" 156 a["inverse"] = "invert" 157 a["inverted"] = "invert" 158 a["hilite"] = "invert" 159 a["hilited"] = "invert" 160 a["highlight"] = "invert" 161 a["highlighted"] = "invert" 162 a["underlined"] = "underline" 163 a["bb"] = "blueback" 164 a["gb"] = "greenback" 165 a["mb"] = "magentaback" 166 a["ob"] = "orangeback" 167 a["pb"] = "purpleback" 168 a["rb"] = "redback" 169 a["bg"] = "greenback" 170 a["bm"] = "magentaback" 171 a["bo"] = "orangeback" 172 a["bp"] = "purpleback" 173 a["br"] = "redback" 174 a["bluebg"] = "blueback" 175 a["graybg"] = "grayback" 176 a["greenbg"] = "greenback" 177 a["greyback"] = "grayback" 178 a["greybg"] = "grayback" 179 a["magentabg"] = "magentaback" 180 a["orangebg"] = "orangeback" 181 a["purplebg"] = "purpleback" 182 a["redbg"] = "redback" 183 a["magback"] = "magentaback" 184 a["magbg"] = "magentaback" 185 a["orback"] = "orangeback" 186 a["orbg"] = "orangeback" 187 a["purback"] = "purpleback" 188 a["purbg"] = "purpleback" 189 190 # style-lookup table 191 s["red"] = "\x1b[38;2;204;0;0m" 192 s["green"] = "\x1b[38;2;0;135;95m" 193 s["blue"] = "\x1b[38;2;0;95;215m" 194 s["orange"] = "\x1b[38;2;215;95;0m" 195 s["purple"] = "\x1b[38;2;135;95;255m" 196 s["magenta"] = "\x1b[38;2;215;0;255m" 197 s["gray"] = "\x1b[38;2;168;168;168m" 198 s["bold"] = "\x1b[1m" 199 s["invert"] = "\x1b[7m" 200 s["italic"] = "\x1b[3m" 201 s["underline"] = "\x1b[4m" 202 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 203 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 204 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 205 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 206 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 207 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 208 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 209 210 # resolve aliases 211 if (a[name] != "") name = a[name] 212 213 # handle unsupported style-names with an error 214 if (s[name] == "") { 215 fmt = "unsupported style/color name `%s`\n" 216 printf fmt, orig > "/dev/stderr" 217 exit 1 218 } 219 220 # match ANSI-code to the name 221 style = s[name] 222 } 223 224 # leak (re)styled lines to stderr 225 { 226 print 227 if (flush) fflush() 228 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 229 printf "%s%s\x1b[0m\n", style, $0 > "/dev/stderr" 230 } 231 ' "$@"