commit: 8a83cf36a847fbd32990d3590bfd22a1516af898
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Fri Aug 2 09:28:58 2024 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Aug 2 16:21:14 2024 +0000
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=8a83cf36
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 <AT> plushkava.net>
functions.sh | 34 ++++++++++++++++++++--------------
test-functions | 2 +-
2 files changed, 21 insertions(+), 15 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
}
#
diff --git a/test-functions b/test-functions
index f11234a..4b3107f 100755
--- a/test-functions
+++ b/test-functions
@@ -517,7 +517,7 @@ test_hr() {
eq 0 ----- - 5 \
eq 0 '' xyz 0 \
eq 0 x xyz 1 \
- eq 0 xyzxy xyz 5
+ eq 0 xxxxx xyz 5
callback() {
shift