Skip to content

Instantly share code, notes, and snippets.

@emctague
Created November 12, 2019 17:46
Show Gist options
  • Save emctague/b162b67befb809bc927b31fce49c7e65 to your computer and use it in GitHub Desktop.
Save emctague/b162b67befb809bc927b31fce49c7e65 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# boxit: Format text inside box-drawing sections.
# Reads from stdin, waits for EOF (^D), then prints output to stdout.
# Lines containing three dashes will be converted into separators!
# Usage: boxit [--round]
# Copyright 2019 Ethan McTague
# License to use, modify, and distribute this software is granted to
# any and all entities wherever permitted by law.
output=`cat`
maxwidth=`echo "$output" | wc -L`
corners="┌,┐,└,┘"
if [ "$1" == "--round" ]; then
corners="╭,╮,╰,╯"
fi
read -r -d '' awkscript <<'EOF'
function lines(a, b) { printf a; for(c=0; c < maxwidth+2; c++) { printf "─"; } printf "%s\n", b; }
BEGIN { split(corners, co, ","); lines(co[1], co[2]); }
{
if ($0=="---") lines("├", "┤");
else printf "│ %-*s │\n", maxwidth, $0
}
END { lines(co[3], co[4]); }
EOF
echo "$output" | awk -v maxwidth="$maxwidth" -v corners="$corners" "$awkscript"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment