feat: add center and hr

This commit is contained in:
2024-08-05 02:07:01 +03:00
parent 01ea8f6381
commit 51cf97e368

View File

@@ -754,6 +754,44 @@ get-gh-latest-tag() {
fi
}
center() {
if [[ $# -eq 0 ]]; then
echo_red "Usage: center <text>"
return 1
fi
print_centered "$@"
}
hr() {
print_centered "-" "-"
}
# from [How to center text in Bash](https://gist.github.com/TrinityCoder/911059c83e5f7a351b785921cf7ecdaa)
function print_centered {
[[ $# == 0 ]] && return 1
declare -i TERM_COLS="$(tput cols)"
declare -i str_len="${#1}"
[[ $str_len -ge $TERM_COLS ]] && {
echo "$1";
return 0;
}
declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
[[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
filler=""
for (( i = 0; i < filler_len; i++ )); do
filler="${filler}${ch}"
done
printf "%s%s%s" "$filler" "$1" "$filler"
[[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
printf "\n"
return 0
}
# select random element from arguments
# NOTE always keep this function last, breaks syntax highlighting
randarg() {