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