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