diff options
author | Kerin Millar <kfm@plushkava.net> | 2024-05-16 07:20:02 +0100 |
---|---|---|
committer | Kerin Millar <kfm@plushkava.net> | 2024-05-17 02:40:19 +0100 |
commit | 1bd941e3de386fd04e23b23407d213868920dbd3 (patch) | |
tree | 0ccaba2acf1b48e8ece8b108da454e520b96aaeb | |
parent | Don't call is_int() from _esetdent() (diff) | |
download | gentoo-functions-1bd941e3de386fd04e23b23407d213868920dbd3.tar.gz gentoo-functions-1bd941e3de386fd04e23b23407d213868920dbd3.tar.bz2 gentoo-functions-1bd941e3de386fd04e23b23407d213868920dbd3.zip |
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@plushkava.net>
-rw-r--r-- | functions.sh | 28 |
1 files 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/. |