From 1998c2e12d19cc8cb739a5bdacf88386afdf5d0a Mon Sep 17 00:00:00 2001 From: Harald Pfeiffer Date: Sun, 19 Apr 2026 10:46:10 +0200 Subject: feat: introduce horizontal line drawers and terminal width calculator --- .bash/aliases.bash | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to '.bash/aliases.bash') diff --git a/.bash/aliases.bash b/.bash/aliases.bash index 8f29423..a09ba54 100644 --- a/.bash/aliases.bash +++ b/.bash/aliases.bash @@ -109,6 +109,44 @@ xstat() { printf "%s\t%s\n" "${crtime}" "${target}" done } +# ----- DECORATIVE STUFF ----- # +function coleval { + mycols="$COLUMNS" + if [ -z "$COLUMNS" ]; then mycols="$(tput cols 2>/dev/null)"; fi + if [ -z "$COLUMNS" ]; then mycols=20; fi + printf '%b' "$mycols" +} +function hsep { + mycols="$(coleval)" + nocalc=0 + declare mychar + while [ "$#" -gt 0 ]; do + case "$1" in + '-i') nocalc=1; shift ;; + *) mychar="$1"; shift ;; + esac + done + [ -z "$mychar" ] && return 1 + [ "$(printf '%b' "$mychar" | wc --chars)" -ne 1 ] && return 2 + if [ "$nocalc" -ne 1 ]; then + # if we encounter a one-char three-byte character, we may have hit a Unicode + # symbol that indeed is a little wider. Use bashes mathematical division- it rounds + # down, but this is what we want as we cannot split a character into parts :-) + # The result might be significantly narrower than the terminal's width, but for + # now we don't have any better math. + [ "$(printf '%b' "$mychar" | wc --bytes)" -eq 3 ] && mycols="$(( 4 * mycols / 9 ))" + fi + for (( i=0;i