commit:     cc75239fd896bb528e4faf9e6b54a900fb657f2e
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Fri Feb 24 06:15:57 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> 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 <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> 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

Reply via email to