commit:     9b3c0592552ecba127fd872b8ba168efea0fdaaf
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Fri Feb 10 23:46:10 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Feb 11 01:29:27 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9b3c0592

Ensure that COLUMNS is set in the case that /bin/sh is bash

To reasonably ensure that the COLUMNS variable is set requires for
three conditions to be fulfilled. Firstly, the version of bash must be
at least 4.3. Secondly, the checkwinsize shopt must be enabled (since
5.0, it is enabled by default). Thirdly, an external command must be
executed.

Better fulfil these conditions by checking whether the shell appears to
be bash and, should it appear to be, enabling checkwinsize before
executing /bin/true. Determining the number of columns in this way is
preferable to falling back to the method of attempting to use stty(1).

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 functions.sh | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/functions.sh b/functions.sh
index 9ab8d0c..241f238 100644
--- a/functions.sh
+++ b/functions.sh
@@ -453,8 +453,15 @@ for arg in "$@" ; do
 done
 
 # Define COLS and ENDCOL so that eend can line up the [ ok ].
+if [ -n "${BASH}" ] && shopt -s checkwinsize 2>/dev/null; then
+       # As is documented, running an external command will cause bash to set
+       # the COLUMNS variable. This technique is effective for >=4.3, though
+       # it requires for the checkwinsize shopt to be enabled. By default, it
+       # is only enabled for >=5.0.
+       /bin/true
+fi
 if is_int "${COLUMNS}" && [ "${COLUMNS}" -gt 0 ]; then
-       # The value of COLUMNS was likely set by a shell such as bash. Trust it.
+       # The value of COLUMNS was likely set by a shell such as bash or mksh.
        COLS=${COLUMNS}
 else
        # Try to use stty(1) to determine the number of columns. The use of the

Reply via email to