#!/bin/sh # The MIT License (MIT) # # Copyright © 2024 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # leak [style-name...] # # Emit copies of stdin lines both to stdout and to stderr, thus `leaking` # the contents going through a pipe of commands, using the leading argument # as the style/color name to use to decorate the stderr output. # # If no style name is given, a default one is chosen. # # As its name suggests, its main use is to inspect/debug the intermediate # stages of a `pipelined` shell command. # # Supported style names include # # - red - orange - bold - underline # - green - magenta - purple - invert # - blue - gray - italic # # Supported aliases for style names include # # b (blue) # g (green) # h (hilight/invert) # m (magenta) # o (orange) # p (purple) # r (red) # u (underline) # handle help options case "$1" in -h|--h|-help|--help) awk '/^# +leak/, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac name="$(echo "${1:-gray}" | sed 's/^--?//')" [ $# -gt 0 ] && shift # handle special style-names case "${name}" in plain) # the general case can't handle simply removing all styles awk '{ print gsub(/\x1b\[([0-9]*[A-HJKST]|[0-9;]*m)/, "") print > "/dev/stderr" }' exit $? ;; keep) # the general case handles empty style-strings as errors awk '{ printf "%s\x1b[0m\n", $0 printf "%s\x1b[0m\n", $0 > "/dev/stderr" }' exit $? ;; esac # general case to handle actual styles awk -v name="${name}" ' BEGIN { # pick a default style, when no style is given if (name == "") name = "gray" orig = name gsub(/^-{1,2}/, "", name) # alias-lookup table a["r"] = "red" a["g"] = "green" a["b"] = "blue" a["o"] = "orange" a["p"] = "purple" a["m"] = "magenta" a["h"] = "invert" a["i"] = "invert" a["u"] = "underline" a["or"] = "orange" a["ma"] = "magenta" a["hi"] = "invert" a["in"] = "invert" a["un"] = "underline" a["inv"] = "invert" a["mag"] = "magenta" a["grey"] = "gray" a["inverse"] = "invert" a["inverted"] = "invert" a["hilite"] = "invert" a["hilited"] = "invert" a["highlight"] = "invert" a["highlighted"] = "invert" a["underlined"] = "underline" a["bluebg"] = "blueback" a["graybg"] = "grayback" a["greenbg"] = "greenback" a["magentabg"] = "magentaback" a["orangebg"] = "orangeback" a["purplebg"] = "purpleback" a["redbg"] = "redback" a["magback"] = "magentaback" a["magbg"] = "magentaback" a["orback"] = "orangeback" a["orbg"] = "orangeback" a["purback"] = "purpleback" a["purbg"] = "purpleback" # style-lookup table s["red"] = "\x1b[38;5;1m" s["green"] = "\x1b[38;5;29m" s["blue"] = "\x1b[38;5;26m" s["orange"] = "\x1b[38;5;166m" s["purple"] = "\x1b[38;5;99m" s["magenta"] = "\x1b[38;5;165m" s["gray"] = "\x1b[38;5;248m" s["bold"] = "\x1b[1m" s["invert"] = "\x1b[7m" s["italic"] = "\x1b[3m" s["underline"] = "\x1b[4m" s["blueback"] = "\x1b[48;5;26m\x1b[38;5;15m" s["grayback"] = "\x1b[48;5;248m\x1b[38;5;15m" s["greenback"] = "\x1b[48;5;29m\x1b[38;5;15m" s["magentaback"] = "\x1b[48;5;165m\x1b[38;5;15m" s["orangeback"] = "\x1b[48;5;166m\x1b[38;5;15m" s["purpleback"] = "\x1b[48;5;99m\x1b[38;5;15m" s["redback"] = "\x1b[41m\x1b[38;5;15m" # resolve aliases if (a[name] != "") { name = a[name] } # handle unsupported style-names with an error if (s[name] == "") { fmt = "\x1b[31munsupported style/color name `%s`\x1b[0m\n" printf fmt, orig > "/dev/stderr" exit 1 } # match ANSI-code to the name style = s[name] # make a style-reset replacement for already-styled lines rep = "\x1b[0m" style } # (re)style lines { print fflush() gsub(/\x1b\[0m/, rep) printf "%s%s\x1b[0m\n", style, $0 > "/dev/stderr" } ' "$@"