diff options
author | Kerin Millar <kfm@plushkava.net> | 2023-02-25 00:55:20 +0000 |
---|---|---|
committer | Sam James <sam@gentoo.org> | 2023-06-07 12:12:18 +0100 |
commit | b7245f936e1cd16aad6d9c7481fd0b52b2371703 (patch) | |
tree | 8d4b27294dd037a3ed82047587c8c7dfbac09c04 | |
parent | Drop support for the checkwinsize feature of bash (diff) | |
download | gentoo-functions-b7245f936e1cd16aad6d9c7481fd0b52b2371703.tar.gz gentoo-functions-b7245f936e1cd16aad6d9c7481fd0b52b2371703.tar.bz2 gentoo-functions-b7245f936e1cd16aad6d9c7481fd0b52b2371703.zip |
Add and integrate the _update_winsize() function
This new function assumes responsibility for updating the genfun_cols
variable. Additionally, it assumes responsibility for updating the newly
introduced genfun_rows variable.
As before, stty(1) is used to determine the dimensions of the terminal.
The approach has been slightly altered so as to incur just one subshell
in the course of doing so. The reason for this is that changes will soon
be made to the _eprint() and _eend() functions that require for the
function to be called repeatedly, not merely at the time of sourcing
functions.sh.
Should stty(1) fail - or produce nonsensical output - it will no longer
be assumed that there are 80 available columns. The requirement for the
"[ ok ]" and "[ !! ]" indicators to always be indented was recently
eliminated, so there is no longer any need to falsify a value for
genfun_cols.
Signed-off-by: Kerin Millar <kfm@plushkava.net>
Signed-off-by: Sam James <sam@gentoo.org>
-rw-r--r-- | functions.sh | 42 |
1 files changed, 18 insertions, 24 deletions
diff --git a/functions.sh b/functions.sh index 44a6dce..c326af4 100644 --- a/functions.sh +++ b/functions.sh @@ -477,6 +477,23 @@ _ends_with_newline() { ! case $1 in *"${genfun_newline}") false ;; esac } +_update_winsize() { + # The following use of stty(1) is portable as of POSIX Issue 8. It would + # be beneficial to leverage the checkwinsize option in bash but the + # implementation is buggy. Given that Chet has agreed to investigate, + # it may eventually become possible to support it. + # shellcheck disable=2046 + set -- $(stty size 2>/dev/null) + if is_int "$1" && is_int "$2" && [ "$1" -gt 0 ] && [ "$2" -gt 0 ]; then + genfun_rows=$1 + genfun_cols=$2 + else + genfun_rows= + genfun_cols= + false + fi +} + # This is the main script, please add all functions above this point! # shellcheck disable=2034 RC_GOT_FUNCTIONS="yes" @@ -511,30 +528,7 @@ else done fi -# Try to determine the number of available columns in the terminal. -for _ in 1 2; do - case $_ in - 1) - # This use of stty(1) is portable as of POSIX Issue 8. - genfun_cols=$( - stty size 2>/dev/null | { - if IFS=' ' read -r _ cols; then - printf '%s\n' "${cols}" - fi - } - ) - ;; - 2) - # Give up and assume 80 available columns. - genfun_cols=80 - break - esac - if is_int "${genfun_cols}" && [ "${genfun_cols}" -gt 0 ]; then - break - fi -done - -if _has_dumb_terminal; then +if _has_dumb_terminal || ! _update_winsize; then unset -v genfun_endcol else # Set some ECMA-48 CSI sequences (CUU and CUF) for cursor positioning. |