From 51cf97e368164aab41fba6433fbc25f36ce704b4 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Mon, 5 Aug 2024 02:07:01 +0300 Subject: [PATCH] feat: add center and hr --- plugins/functions.plugin.zsh | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/plugins/functions.plugin.zsh b/plugins/functions.plugin.zsh index 263eb4aa..d69b229f 100755 --- a/plugins/functions.plugin.zsh +++ b/plugins/functions.plugin.zsh @@ -754,6 +754,44 @@ get-gh-latest-tag() { fi } +center() { + if [[ $# -eq 0 ]]; then + echo_red "Usage: center " + 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() {