summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-08-02 10:28:58 +0100
committerKerin Millar <kfm@plushkava.net>2024-08-02 17:21:14 +0100
commit8a83cf36a847fbd32990d3590bfd22a1516af898 (patch)
treef9111b8f7812353a075c76550acc5acdcc60d331 /functions.sh
parentRender contains_all() and contains_any() faster (diff)
downloadgentoo-functions-8a83cf36a847fbd32990d3590bfd22a1516af898.tar.gz
gentoo-functions-8a83cf36a847fbd32990d3590bfd22a1516af898.tar.bz2
gentoo-functions-8a83cf36a847fbd32990d3590bfd22a1516af898.zip
Render hr() faster
Render hr() faster by eliminating the requirement to fork and execute any external utilities after having established the intended length of the rule. Also, use printf -v and string-replacing parameter expansion where the shell is found to be bash. Doing so helps considerably because bash is very slow at looping. Signed-off-by: Kerin Millar <kfm@plushkava.net>
Diffstat (limited to 'functions.sh')
-rw-r--r--functions.sh34
1 files changed, 20 insertions, 14 deletions
diff --git a/functions.sh b/functions.sh
index f120564..b40e17b 100644
--- a/functions.sh
+++ b/functions.sh
@@ -156,30 +156,36 @@ has_systemd()
#
# Prints a horizontal rule. If specified, the first parameter shall be taken as
-# a string to be repeated in the course of composing the rule. Otherwise, it
-# shall default to the <hyphen-minus>. If specified, the second parameter shall
-# define the length of the rule in characters. Otherwise, it shall default to
-# the width of the terminal if such can be determined, or 80 if it cannot be.
+# a string whose first character is to be repeated in the course of composing
+# the rule. Otherwise, or if specified as the empty string, it shall default to
+# the <hyphen-minus>. If specified, the second parameter shall define the length
+# of the rule in characters. Otherwise, it shall default to the width of the
+# terminal if such can be determined, or 80 if it cannot be.
#
hr()
{
- local length
+ local char hr i length
- if is_int "$2"; then
+ if [ "$#" -ge 2 ] && is_int "$2"; then
length=$2
elif _update_tty_level <&1; [ "${genfun_tty}" -eq 2 ]; then
length=${genfun_cols}
else
length=80
fi
- PATTERN=${1:--} awk -v "width=${length}" -f - <<-'EOF'
- BEGIN {
- while (length(rule) < width) {
- rule = rule substr(ENVIRON["PATTERN"], 1, width - length(rule))
- }
- print rule
- }
- EOF
+ char=${1--}
+ char=${char%"${char#?}"}
+ if [ "${BASH}" ]; then
+ # shellcheck disable=3045
+ printf -v hr '%*s' "${length}" ''
+ eval 'printf %s\\n "${hr//?/"$char"}"'
+ else
+ i=0
+ while [ "$(( i += 1 ))" -le "${length}" ]; do
+ hr=${hr}${char}
+ done
+ printf '%s\n' "${hr}"
+ fi
}
#