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