From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 3CF98159C9B for ; Fri, 2 Aug 2024 23:14:19 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 80769E2A5C; Fri, 2 Aug 2024 23:14:16 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 5268EE2A5C for ; Fri, 2 Aug 2024 23:14:16 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 1047934300E for ; Fri, 2 Aug 2024 23:14:15 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 714A81EA4 for ; Fri, 2 Aug 2024 23:14:12 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1722615674.8a83cf36a847fbd32990d3590bfd22a1516af898.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh test-functions X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 8a83cf36a847fbd32990d3590bfd22a1516af898 X-VCS-Branch: master Date: Fri, 2 Aug 2024 23:14:12 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 38537e45-104d-4ad0-b70f-ae31a9df2ef3 X-Archives-Hash: d252ecf8dad5a842e48e80911717cf1a commit: 8a83cf36a847fbd32990d3590bfd22a1516af898 Author: Kerin Millar plushkava net> AuthorDate: Fri Aug 2 09:28:58 2024 +0000 Commit: Sam James gentoo 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 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 . 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 . 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