Last active
September 22, 2022 18:16
-
-
Save AlexAtkinson/49078eb9a2dcff17b28371eb92964cca to your computer and use it in GitHub Desktop.
BASH: Fancy Autosizing Section Header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prints a section header. | |
# | |
# Features | |
# - Autosizes to terminal if $width -gt $COLUMNS | |
# - Specifying width as 4000 works... | |
# - Adjust first two sanities for stricter usage | |
# * Currently sets width to half terminal width | |
# * Currently rounds down odd to even width automatically | |
# - Customizable outside and inside fill characters | |
# - Nofill style option | |
# | |
# Usage: | |
# printSectionHeader <width> <message> | |
# | |
# Examples: | |
# printSectionHeader Hello World! | |
# printSectionHeader 40 Hello World! | |
# | |
# Output: | |
# # ------------------------------------ # | |
# # //////////////////////////////////// # | |
# # /////////// Hello World! /////////// # | |
# # //////////////////////////////////// # | |
# # ------------------------------------ # | |
# | |
function printSectionHeader() { | |
local width=$1 | |
local wrap_char='#' # Examples: '$', '=', '-', '\', '%', '\*' (Must escape some chars) | |
local outside_fill_char='=' | |
local inside_fill_char='-' | |
local fill_style='fill' # 'fill' or 'nofill' | |
local txt="${@:2}" | |
if [[ ! $width =~ ^[0-9]+$ ]]; then | |
#echo "ERROR: First argument must specify the width as an even number (int)." | |
#return 1 | |
width=$((COLUMNS/2)) | |
local txt="$*" | |
fi | |
if [[ $((width%2)) -eq 1 ]]; then | |
#echo "ERROR: The width must be an even number (int)." | |
#return 1 | |
width=$(($width-($width%2))) | |
fi | |
local fill=$(($width-4)) | |
local txt_length=${#txt} | |
local min_width=$((txt_length+8)) | |
if [[ $width -lt $min_width ]]; then | |
echo "ERROR: Minimum width for text length ($txt_length) is $min_width." | |
return 1 | |
fi | |
local padding=8 | |
local cols=$((COLUMNS - (COLUMNS % 2))) | |
[[ $fill -gt $((cols-padding)) ]] && fill=$((cols-$((padding/2)))) | |
local fill_width="$((cols-cols-fill))" | |
local half_fill_width=$(( ((((fill_width/2))+((padding/ 2))))+$((txt_length/2))-1 )) | |
local fill="$(printf '%*s' "${fill_width}" | tr ' ' $outside_fill_char)" | |
local inside_fill="$(printf '%*s' "$((fill_width))" | tr ' ' $inside_fill_char)" | |
[[ $fill_style == "nofill" ]] && local inside_fill="$(printf '%*s' "$((fill_width))")" | |
local inside_half_fill="$(printf '%*s' "$((half_fill_width -2))" | tr ' ' $inside_fill_char)" | |
[[ $fill_style == "nofill" ]] && local inside_half_fill="$(printf '%*s' "$((half_fill_width -2))")" | |
printf "$wrap_char "; printf '%s' "${fill}"; printf " $wrap_char\n" | |
printf "$wrap_char "; printf '%s' "${inside_fill}"; printf " $wrap_char\n" | |
printf "$wrap_char "; printf '%s' "${inside_half_fill}" | |
printf "\e[01;39m $txt \e[0m" | |
[[ $((txt_length%2)) -eq 1 ]] && local inside_half_fill="$(printf '%*s' "$((half_fill_width -1))" | tr ' ' $inside_fill_char)" | |
[[ $fill_style == "nofill" ]] && [[ $((txt_length%2)) -eq 1 ]] && local inside_half_fill="$(printf '%*s' "$((half_fill_width -1))")" | |
printf '%s' "${inside_half_fill}"; printf " $wrap_char\n" | |
printf "$wrap_char "; printf '%s' "${inside_fill}"; printf " $wrap_char\n" | |
printf "$wrap_char "; printf '%s' "${fill}"; printf " $wrap_char\n" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment