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 [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 flush=0 68 if [ -p /dev/stdout ] || [ -t 1 ]; then 69 flush=1 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}" >&2 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 awk -v flush="${flush}" ' 93 { 94 print 95 if (flush) fflush() 96 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 97 print > "/dev/stderr" 98 } 99 ' "$@" 100 exit $? 101 ;; 102 103 keep|same) 104 awk -v flush="${flush}" ' 105 { 106 print 107 if (flush) fflush() 108 print > "/dev/stderr" 109 } 110 ' "$@" 111 exit $? 112 ;; 113 esac 114 115 # general case to handle actual styles 116 awk -v flush="${flush}" -v name="${name}" ' 117 BEGIN { 118 # pick a default style, when no style is given 119 if (name == "") name = "gray" 120 orig = name 121 gsub(/^-{1,2}/, "", name) 122 123 # alias-lookup table 124 a["r"] = "red" 125 a["g"] = "green" 126 a["b"] = "blue" 127 a["o"] = "orange" 128 a["p"] = "purple" 129 a["m"] = "magenta" 130 a["h"] = "invert" 131 a["i"] = "invert" 132 a["u"] = "underline" 133 a["or"] = "orange" 134 a["ma"] = "magenta" 135 a["hi"] = "invert" 136 a["in"] = "invert" 137 a["un"] = "underline" 138 a["inv"] = "invert" 139 a["mag"] = "magenta" 140 a["grey"] = "gray" 141 a["inverse"] = "invert" 142 a["inverted"] = "invert" 143 a["hilite"] = "invert" 144 a["hilited"] = "invert" 145 a["highlight"] = "invert" 146 a["highlighted"] = "invert" 147 a["underlined"] = "underline" 148 a["bb"] = "blueback" 149 a["gb"] = "greenback" 150 a["mb"] = "magentaback" 151 a["ob"] = "orangeback" 152 a["pb"] = "purpleback" 153 a["rb"] = "redback" 154 a["bg"] = "greenback" 155 a["bm"] = "magentaback" 156 a["bo"] = "orangeback" 157 a["bp"] = "purpleback" 158 a["br"] = "redback" 159 a["bluebg"] = "blueback" 160 a["graybg"] = "grayback" 161 a["greenbg"] = "greenback" 162 a["greyback"] = "grayback" 163 a["greybg"] = "grayback" 164 a["magentabg"] = "magentaback" 165 a["orangebg"] = "orangeback" 166 a["purplebg"] = "purpleback" 167 a["redbg"] = "redback" 168 a["magback"] = "magentaback" 169 a["magbg"] = "magentaback" 170 a["orback"] = "orangeback" 171 a["orbg"] = "orangeback" 172 a["purback"] = "purpleback" 173 a["purbg"] = "purpleback" 174 175 # style-lookup table 176 s["red"] = "\x1b[38;2;204;0;0m" 177 s["green"] = "\x1b[38;2;0;135;95m" 178 s["blue"] = "\x1b[38;2;0;95;215m" 179 s["orange"] = "\x1b[38;2;215;95;0m" 180 s["purple"] = "\x1b[38;2;135;95;255m" 181 s["magenta"] = "\x1b[38;2;215;0;255m" 182 s["gray"] = "\x1b[38;2;168;168;168m" 183 s["bold"] = "\x1b[1m" 184 s["invert"] = "\x1b[7m" 185 s["italic"] = "\x1b[3m" 186 s["underline"] = "\x1b[4m" 187 s["blueback"] = "\x1b[48;2;0;95;215m\x1b[38;2;238;238;238m" 188 s["grayback"] = "\x1b[48;2;168;168;168m\x1b[38;2;238;238;238m" 189 s["greenback"] = "\x1b[48;2;0;135;95m\x1b[38;2;238;238;238m" 190 s["magentaback"] = "\x1b[48;2;215;0;255m\x1b[38;2;238;238;238m" 191 s["orangeback"] = "\x1b[48;2;215;95;0m\x1b[38;2;238;238;238m" 192 s["purpleback"] = "\x1b[48;2;135;95;255m\x1b[38;2;238;238;238m" 193 s["redback"] = "\x1b[48;2;204;0;0m\x1b[38;2;238;238;238m" 194 195 # resolve aliases 196 if (a[name] != "") name = a[name] 197 198 # handle unsupported style-names with an error 199 if (s[name] == "") { 200 fmt = "unsupported style/color name `%s`\n" 201 printf fmt, orig > "/dev/stderr" 202 exit 1 203 } 204 205 # match ANSI-code to the name 206 style = s[name] 207 } 208 209 # leak (re)styled lines to stderr 210 { 211 print 212 if (flush) fflush() 213 gsub(/\x1b\[[0-9;]*[A-Za-z]/, "") 214 printf "%s%s\x1b[0m\n", style, $0 > "/dev/stderr" 215 } 216 ' "$@"