commit:     1bd941e3de386fd04e23b23407d213868920dbd3
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu May 16 06:20:02 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri May 17 01:40:19 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1bd941e3

Make _eend() much faster in bash

This is accomplished by instead invoking the true binary and taking a
reading of the COLUMNS variable. However, this is only done in
situations where it can be expected to be reliable. Even as of
bash 5.3-alpha, the checkwinsize feature does not work reliably in a
subshell.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>

 functions.sh | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/functions.sh b/functions.sh
index 57f6aa2..1f547d0 100644
--- a/functions.sh
+++ b/functions.sh
@@ -469,14 +469,20 @@ _is_visible()
 #
 _update_columns()
 {
-       local ifs
-
-       # The following use of stty(1) is portable as of POSIX Issue 8.
-       ifs=$IFS
-       IFS=' '
-       # shellcheck disable=2046
-       set -- $(stty size 2>/dev/null)
-       IFS=$ifs
+       # Command substitutions are rather slow in bash. Using the COLUMNS
+       # variable helps but checkwinsize won't work properly in subshells.
+       # shellcheck disable=3028,3044
+       if [ "$$" = "${BASHPID}" ] && shopt -q checkwinsize; then
+               "${genfun_bin_true}"
+               set -- 0 "${COLUMNS}"
+       else
+               # The following use of stty(1) is portable as of POSIX Issue 8.
+               genfun_ifs=${IFS}
+               IFS=' '
+               # shellcheck disable=2046
+               set -- $(stty size 2>/dev/null)
+               IFS=${genfun_ifs}
+       fi
        [ "$#" -eq 2 ] && is_int "$2" && [ "$2" -gt 0 ] && genfun_cols=$2
 }
 
@@ -515,6 +521,12 @@ else
        genfun_offset=0
 fi
 
+# Store the path to the true binary. It is potentially used by _update_columns.
+if [ "${BASH}" ]; then
+       # shellcheck disable=3045
+       genfun_bin_true=$(type -P true)
+fi
+
 # Determine whether the use of color is to be wilfully avoided.
 if [ -n "${NO_COLOR}" ]; then
        # See https://no-color.org/.

Reply via email to