public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/gentoo-functions:master commit in: /
Date: Wed,  7 Jun 2023 11:13:36 +0000 (UTC)	[thread overview]
Message-ID: <1686136338.cc75239fd896bb528e4faf9e6b54a900fb657f2e.sam@gentoo> (raw)

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	other threads:[~2023-06-07 11:13 UTC|newest]

Thread overview: 281+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-07 11:13 Sam James [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-10-05  7:25 [gentoo-commits] proj/gentoo-functions:master commit in: / Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-10-05  4:15 Sam James
2024-08-11 10:23 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-05 20:39 Sam James
2024-08-05 20:39 Sam James
2024-08-05  2:03 Sam James
2024-08-05  2:02 Sam James
2024-08-05  2:02 Sam James
2024-08-05  2:02 Sam James
2024-08-05  2:02 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-07-08  3:00 Sam James
2024-07-08  2:31 Sam James
2024-07-08  2:31 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-07-07  5:55 Sam James
2024-06-25  4:06 Sam James
2024-06-25  4:06 Sam James
2024-06-25  4:06 Sam James
2024-06-25  4:06 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-05-24  6:05 Sam James
2024-05-24  1:18 Sam James
2024-05-24  1:18 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-22  1:12 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-18 16:07 Sam James
2024-05-18 16:06 Sam James
2024-05-18 16:06 Sam James
2024-05-18 15:34 Sam James
2024-05-18 15:32 Sam James
2024-05-18 15:32 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-17  4:03 Sam James
2024-05-15 10:28 Sam James
2024-05-15 10:28 Sam James
2024-05-14  0:18 Sam James
2024-05-14  0:15 Sam James
2024-05-14  0:12 Sam James
2024-05-14  0:12 Sam James
2024-05-14  0:08 Sam James
2024-05-14  0:08 Sam James
2024-05-14  0:05 Sam James
2024-05-14  0:05 Sam James
2024-05-14  0:05 Sam James
2024-05-14  0:05 Sam James
2024-05-14  0:05 Sam James
2024-02-16 21:35 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-10  7:23 Sam James
2023-06-10  7:23 Sam James
2023-06-10  6:04 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-10  4:22 Sam James
2023-06-09 11:17 Sam James
2023-06-09 11:11 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-17  7:44 Sam James
2023-02-17  7:44 Sam James
2023-02-17  7:44 Sam James
2023-02-17  1:33 Sam James
2023-02-17  1:33 Sam James
2023-02-17  1:33 Sam James
2023-02-15  8:18 Sam James
2023-02-15  7:48 Sam James
2023-02-15  7:46 Sam James
2023-02-15  7:46 Sam James
2023-02-15  7:46 Sam James
2023-02-15  7:46 Sam James
2023-02-15  7:46 Sam James
2023-02-15  7:46 Sam James
2023-02-15  2:24 Sam James
2023-02-15  2:24 Sam James
2023-02-15  2:24 Sam James
2023-02-14  3:40 Sam James
2023-02-14  3:40 Sam James
2023-02-14  3:40 Sam James
2023-02-14  3:40 Sam James
2023-02-14  0:09 Sam James
2023-02-14  0:09 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-12 18:53 Sam James
2023-02-12 18:53 Sam James
2023-02-12  6:53 Sam James
2023-02-12  6:53 Sam James
2023-02-12  6:53 Sam James
2023-02-11  1:43 Sam James
2023-02-11  1:43 Sam James
2023-02-10  6:09 Sam James
2023-02-10  6:09 Sam James
2023-02-10  6:09 Sam James
2023-02-09  3:54 Sam James
2023-02-09  3:54 Sam James
2023-02-08  3:37 Sam James
2023-02-08  1:06 Sam James
2023-02-08  0:03 Sam James
2023-02-08  0:03 Sam James
2023-02-07 23:47 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07  1:08 Sam James
2023-02-07  1:08 Sam James
2023-02-06 13:47 Sam James
2023-02-06  4:32 Sam James
2023-02-06  4:23 Sam James
2023-02-06  4:19 Sam James
2023-02-06  4:10 Sam James
2023-02-06  4:10 Sam James
2023-02-06  3:59 Sam James
2023-02-06  3:59 Sam James
2023-02-06  3:59 Sam James
2022-07-30  5:48 Sam James
2022-07-29  2:03 Sam James
2022-07-29  2:03 Sam James
2022-07-29  2:03 Sam James
2021-08-30 21:14 Mike Gilbert
2021-08-30 21:14 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-01-26 23:19 Mike Gilbert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1686136338.cc75239fd896bb528e4faf9e6b54a900fb657f2e.sam@gentoo \
    --to=sam@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox