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 2208F15800F for ; Fri, 17 Feb 2023 07:44:22 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 6E271E0858; Fri, 17 Feb 2023 07:44:21 +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 50AE2E0858 for ; Fri, 17 Feb 2023 07:44:21 +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 6ACF5340FE3 for ; Fri, 17 Feb 2023 07:44:20 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id E793C8A7 for ; Fri, 17 Feb 2023 07:44:18 +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: <1676619419.0af4e76511a1235990e10d0e616a261cb6a4714f.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 0af4e76511a1235990e10d0e616a261cb6a4714f X-VCS-Branch: master Date: Fri, 17 Feb 2023 07:44:18 +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: 5e79d0ea-7751-494e-b439-14cc80f607a8 X-Archives-Hash: 08693c941bebcd0430879964069e7712 commit: 0af4e76511a1235990e10d0e616a261cb6a4714f Author: Kerin Millar plushkava net> AuthorDate: Fri Feb 17 03:04:49 2023 +0000 Commit: Sam James gentoo org> CommitDate: Fri Feb 17 07:36:59 2023 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=0af4e765 Improve the legibility of the routine that detects the number of columns Also, mention that the use of stty size is portable as of POSIX Issue 8. Signed-off-by: Kerin Millar plushkava.net> Signed-off-by: Sam James gentoo.org> functions.sh | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/functions.sh b/functions.sh index df72d4a..02627d2 100644 --- a/functions.sh +++ b/functions.sh @@ -492,32 +492,36 @@ for arg in "$@" ; do done # Try to determine the number of available columns in the terminal. -# shellcheck disable=3044 -if [ -n "${BASH}" ] && shopt -s checkwinsize 2>/dev/null; then - # As is documented, running an external command will cause bash to set - # the COLUMNS variable. This technique is effective for >=4.3, though - # it requires for the checkwinsize shopt to be enabled. By default, it - # is only enabled for >=5.0. - /bin/true -fi -if is_int "${COLUMNS}" && [ "${COLUMNS}" -gt 0 ]; then - # The value of COLUMNS was likely set by a shell such as bash or mksh. - genfun_cols=${COLUMNS} -else - # Try to use stty(1) to determine the number of columns. The use of the - # size operand is not portable. - genfun_cols=$( - stty size 2>/dev/null | { - if IFS=' ' read -r _ cols _; then - printf '%s\n' "${cols}" +for _ in 1 2 3; do + case $_ in + 1) + # Running an external command causes bash >=4.3 to set + # the COLUMNS variable, provided that the checkwinsize + # shopt is enabled. As of 5.0, it's enabled by default. + # shellcheck disable=3044 + if [ -n "${BASH}" ] && shopt -s checkwinsize 2>/dev/null; then + /bin/true fi - } - ) - if ! is_int "${genfun_cols}" || [ "${genfun_cols}" -le 0 ]; then - # Give up and assume 80 available columns. - genfun_cols=80 + genfun_cols=${COLUMNS} + ;; + 2) + # This use of stty(1) is portable as of POSIX Issue 8. + genfun_cols=$( + stty size 2>/dev/null | { + if IFS=' ' read -r _ cols; then + printf '%s\n' "${cols}" + fi + } + ) + ;; + 3) + # Give up and assume 80 available columns. + genfun_cols=80 + esac + if is_int "${genfun_cols}" && [ "${genfun_cols}" -gt 0 ]; then + break fi -fi +done # Set an ECMA-48 CSI sequence, allowing for eend to line up the [ ok ] string. {