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 82AAB15806E for ; Wed, 7 Jun 2023 11:13:40 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 94EDEE0884; Wed, 7 Jun 2023 11:13:39 +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) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 74EDBE0884 for ; Wed, 7 Jun 2023 11:13:39 +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) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 1408B340C77 for ; Wed, 7 Jun 2023 11:13:38 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 56C2FA82 for ; Wed, 7 Jun 2023 11:13:36 +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: <1686136338.cc75239fd896bb528e4faf9e6b54a900fb657f2e.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: cc75239fd896bb528e4faf9e6b54a900fb657f2e X-VCS-Branch: master Date: Wed, 7 Jun 2023 11:13:36 +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: da4bbf86-5899-4efc-be4b-f7449217ebf2 X-Archives-Hash: 1d5c774f7c9b7909b3d05bb5901a23f4 commit: cc75239fd896bb528e4faf9e6b54a900fb657f2e Author: Kerin Millar plushkava net> AuthorDate: Fri Feb 24 06:15:57 2023 +0000 Commit: Sam James gentoo org> CommitDate: Wed Jun 7 11:12:18 2023 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=cc75239f Jettison the genfun_lastbegun_strlen and genfun_lastcall variables The genfun_lastbegun_strlen variable was previously used by _eend() to indent the "[ ok ]" and "[ !! ]" indicators in the case that STDOUT is found not to be a tty. Dispense with this variable. Instead, refrain from indenting the indicator at all. After all, the width of the controlling terminal is immaterial unless one is actually intending to print to it. $ { ebegin "Testing"; eend 0; } | cat * Testing ... [ ok ] Apart from simplifying the code, this change brings the overall behaviour of the printing functions closer to that of their counterparts in OpenRC. The genfun_lastcall variable was previously used by the _eprint() function for the sole purpose of printing a LF (newline) character in the case that the last message was printed as a consequence of calling the ebegin() function. It would do so in anticipation of the _eend() function later emitting the CUU (ECMA-48 CSI) sequence to move the cursor up by one line, just prior to printing the "[ ok ]" and "[ !! ]" indicators. Additionally, it was used by the ebegin() function to determine whether a terminating LF character should follow the printed message. Specifically, it would elect to print a LF character in the case that ECMA-48 CSI sequences had been disabled at the time of functions.sh being sourced. Finally, the value of genfun_lastcall would influence the (now defunct) method by which the _eend() function would calculate the degree of indentation required for the indicator in the case of STDOUT not being a tty. This variable has been dispensed with and replaced by a variable named genfun_is_pending_lf. As its name suggests, its purpose is to track whether the last printed message happened to contain a terminating LF character. Now, whenever the _eprint() function is called, it consults the variable so as to determine whether a LF character should be printed for the purpose of terminating the last message, just before proceeding to print the next one. Once the next one has been printed, the value of the variable is updated accordingly. Similarly, the _eend() function consults the variable so as to determine whether a LF character should be printed to a terminal, just prior to the CUU (ECMA-48 CSI) sequence. Ultimately, ebegin() will no longer be sloppily treated as a special case. Rather, any printing function that inhibits the addition of a terminating LF character (ebegin, einfon, ewarn, errorn etc) will have its message be terminated upon calling any printing function thereafter. In addition to cleaning up the code a little, these changes should render the impending overhaul of the _eprint() and _eend() functions easier to digest, once it lands. Signed-off-by: Kerin Millar plushkava.net> Signed-off-by: Sam James gentoo.org> functions.sh | 94 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 44 insertions(+), 50 deletions(-) diff --git a/functions.sh b/functions.sh index 31d1b43..97b0e6b 100644 --- a/functions.sh +++ b/functions.sh @@ -13,17 +13,21 @@ # Called by ebegin, eerrorn, einfon, and ewarnn. # _eprint() { - local color + local color msg color=$1 shift - if [ -z "${genfun_endcol}" ] && [ "${genfun_lastcall}" = "ebegin" ]; then - printf '\n' - fi + msg=$* if [ -t 1 ]; then - printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" "$*" + printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" "${msg}" else - printf ' * %s%s' "${genfun_indent}" "$*" + printf ' * %s%s' "${genfun_indent}" "${msg}" + fi + + if _ends_with_newline "${msg}"; then + genfun_is_pending_lf=0 + else + genfun_is_pending_lf=1 fi } @@ -117,7 +121,6 @@ einfon() { if ! yesno "${EINFO_QUIET}"; then _eprint "${GOOD}" "$@" - genfun_lastcall="einfon" fi } @@ -126,9 +129,7 @@ einfon() # einfo() { - einfon "$* -" - genfun_lastcall="einfo" + einfon "${*}${genfun_newline}" } # @@ -139,7 +140,6 @@ ewarnn() if ! yesno "${EINFO_QUIET}"; then _eprint "${WARN}" "$@" >&2 esyslog "daemon.warning" "${0##*/}" "$@" - genfun_lastcall="ewarnn" fi } @@ -148,9 +148,7 @@ ewarnn() # ewarn() { - ewarnn "$* -" - genfun_lastcall="ewarn" + ewarnn "${*}${genfun_newline}" } # @@ -161,7 +159,6 @@ eerrorn() if ! yesno "${EERROR_QUIET}"; then _eprint "${BAD}" "$@" >&2 esyslog "daemon.err" "${0##*/}" "$@" - genfun_lastcall="eerrorn" fi return 1 } @@ -171,10 +168,7 @@ eerrorn() # eerror() { - eerrorn "$* -" - genfun_lastcall="eerror" - return 1 + eerrorn "${*}${genfun_newline}" } # @@ -185,13 +179,8 @@ ebegin() local msg if ! yesno "${EINFO_QUIET}"; then - msg="$* ..." - _eprint "${GOOD}" "${msg}" - if [ -n "${genfun_endcol}" ]; then - printf '\n' - fi - genfun_lastbegun_strlen="$(( 3 + ${#genfun_indent} + ${#msg} ))" - genfun_lastcall="ebegin" + msg="$* ...${genfun_newline}" + GENFUN_CALLER=ebegin _eprint "${GOOD}" "${msg}" fi } @@ -201,14 +190,14 @@ ebegin() # _eend() { - local cols efunc is_tty msg retval + local efunc is_tty msg retval efunc=$1 shift if [ "$#" -eq 0 ]; then retval=0 elif ! is_int "$1" || [ "$1" -lt 0 ]; then - ewarn "Invalid argument given to ${CALLER} (the exit status code must be an integer >= 0)" + ewarn "Invalid argument given to ${GENFUN_CALLER} (the exit status code must be an integer >= 0)" retval=0 shift else @@ -218,13 +207,8 @@ _eend() if [ -t 1 ]; then is_tty=1 - cols=${genfun_cols} else - # STDOUT is not currently a TTY. Therefore, the width of the - # controlling terminal, if any, is irrelevant. For this call, - # consider the number of columns as being 80. is_tty=0 - cols=80 fi if [ "${retval}" -ne 0 ]; then @@ -253,12 +237,20 @@ _eend() fi if [ "${is_tty}" -eq 1 ] && [ -n "${genfun_endcol}" ]; then + # Should a LF character be pending then print one. The CUU + # (ECMA-48 CSI) sequence will move the cursor up by one line + # prior to printing the indicator, right-justified. + if [ "${genfun_is_pending_lf}" -eq 1 ]; then + printf '\n' + fi printf '%b %s\n' "${genfun_endcol}" "${msg}" else - [ "${genfun_lastcall}" = ebegin ] || genfun_lastbegun_strlen=0 - printf "%$(( cols - genfun_lastbegun_strlen - 7 ))s %s\n" '' "${msg}" + printf ' %s\n' "${msg}" fi + # Record the fact that a LF character is no longer pending. + genfun_is_pending_lf=0 + return "${retval}" } @@ -268,12 +260,7 @@ _eend() # eend() { - local retval - - CALLER=${CALLER:-eend} _eend eerror "$@" - retval=$? - genfun_lastcall="eend" - return "${retval}" + GENFUN_CALLER=${GENFUN_CALLER:-eend} _eend eerror "$@" } # @@ -282,12 +269,7 @@ eend() # ewend() { - local retval - - CALLER=${CALLER:-ewend} _eend ewarn "$@" - retval=$? - genfun_lastcall="ewend" - return "${retval}" + GENFUN_CALLER=${GENFUN_CALLER:-ewend} _eend ewarn "$@" } # v-e-commands honor EINFO_VERBOSE which defaults to no. @@ -329,7 +311,7 @@ vebegin() veend() { if yesno "${EINFO_VERBOSE}"; then - CALLER=veend eend "$@" + GENFUN_CALLER=veend eend "$@" elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then ewarn "Invalid argument given to veend (the exit status code must be an integer >= 0)" else @@ -340,7 +322,7 @@ veend() vewend() { if yesno "${EINFO_VERBOSE}"; then - CALLER=vewend ewend "$@" + GENFUN_CALLER=vewend ewend "$@" elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then ewarn "Invalid argument given to vewend (the exit status code must be an integer >= 0)" else @@ -490,6 +472,10 @@ _has_monochrome_terminal() { fi } +_ends_with_newline() { + ! case $1 in *"${genfun_newline}") false ;; esac +} + # This is the main script, please add all functions above this point! # shellcheck disable=2034 RC_GOT_FUNCTIONS="yes" @@ -501,6 +487,14 @@ EINFO_VERBOSE="${EINFO_VERBOSE:-no}" # Set the initial value for e-message indentation. genfun_indent= +# Assign the LF ('\n') character for later expansion. POSIX Issue 8 permits +# $'\n' but it may take years for it to be commonly implemented. +genfun_newline=' +' + +# Whether the last printed message is pending a concluding LF character. +genfun_is_pending_lf=0 + # Should we use color? if [ -n "${NO_COLOR}" ]; then # See https://no-color.org/. @@ -552,7 +546,7 @@ done if _has_dumb_terminal; then unset -v genfun_endcol else - # Set some ECMA-48 CSI sequences (CUU1 and CUF) for cursor positioning. + # Set some ECMA-48 CSI sequences (CUU and CUF) for cursor positioning. # These are standard and, conveniently, documented by console_codes(4). genfun_endcol="\\033[A\\033[$(( genfun_cols - 7 ))C" fi