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 2AC1E15817D for ; Fri, 21 Jun 2024 13:14:20 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 2F862E2A4C; Fri, 21 Jun 2024 13:14:19 +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 A5726E2A4F for ; Fri, 21 Jun 2024 13:14:18 +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 95AF9335DC3 for ; Fri, 21 Jun 2024 13:14:17 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id B77271D50 for ; Fri, 21 Jun 2024 13:14:14 +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: <1718176002.1124356ee9f4d6065953d8aecf6e06c617e930c1.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: 1124356ee9f4d6065953d8aecf6e06c617e930c1 X-VCS-Branch: master Date: Fri, 21 Jun 2024 13:14:14 +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: 23ab1037-6d83-4718-8e85-65696a740e4d X-Archives-Hash: 3e490dd77bf8afeb6b58c8aae95f628b commit: 1124356ee9f4d6065953d8aecf6e06c617e930c1 Author: Kerin Millar plushkava net> AuthorDate: Mon Jun 3 01:39:20 2024 +0000 Commit: Sam James gentoo org> CommitDate: Wed Jun 12 07:06:42 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1124356e Add the hr() function to print a horizontal rule As based on the implementation in Maarten Billemont's bashlib library. Signed-off-by: Kerin Millar plushkava.net> functions.sh | 28 ++++++++++++++++++++++++++++ test-functions | 24 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/functions.sh b/functions.sh index 30c2447..c077290 100644 --- a/functions.sh +++ b/functions.sh @@ -363,6 +363,34 @@ has_systemd() test -d /run/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. +# +hr() +{ + local length + + if 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 +} + # # Determines whether the first parameter is a valid identifier (variable name). # diff --git a/test-functions b/test-functions index bbb74f1..d90462e 100755 --- a/test-functions +++ b/test-functions @@ -484,6 +484,29 @@ test_trim() { iterate_tests 4 "$@" } +test_hr() { + # shellcheck disable=2183 + set -- \ + eq 0 "$(printf '%80s' | tr ' ' -)" N/A N/A \ + eq 0 "$(printf '%80s' | tr ' ' -)" - N/A \ + eq 0 '' - 0 \ + eq 0 - - 1 \ + eq 0 ----- - 5 \ + eq 0 '' xyz 0 \ + eq 0 x xyz 1 \ + eq 0 xyzxy xyz 5 + + callback() { + shift + expected=$1 + shift + test_description="hr $(_print_args "$@")" + test "$(hr "$@")" = "${expected}" + } + + iterate_tests 5 "$@" +} + iterate_tests() { slice_width=$1 shift @@ -547,6 +570,7 @@ test_edo || rc=1 test_srandom || rc=1 test_newest || rc=1 test_trim || rc=1 +test_hr || rc=1 cleanup_tmpdir