Better handling of terminal length
authorSteve McIntyre <steve@einval.com>
Mon, 5 Mar 2018 12:32:41 +0000 (12:32 +0000)
committerSteve McIntyre <steve@einval.com>
Mon, 5 Mar 2018 12:32:41 +0000 (12:32 +0000)
LINES *isn't* normally set inside a script, so try to use tput to get
the number of lines or fall back to the hard-coded 24

abcde

diff --git a/abcde b/abcde
index 351c46e..15f8b31 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -226,6 +226,19 @@ page ()
        PAGEFILE="$1"
        local NUM_LINES=$(wc -l < "$PAGEFILE")
 
+       # Check the terminal length every time we're called - the user
+       # might have resized since last time we were here
+
+       # First, try tput
+       if [ -z "$LINES" ]; then
+               LINES=$(tput lines 2>/dev/null)
+       fi
+
+       # If tput didn't work, 24 will do...
+       if [ -z "$LINES" ]; then
+               LINES=24
+       fi
+
        # Is the text long enough to need a pager?
        if [ "$NUM_LINES" -ge $LINES ]; then
                # Yes!
@@ -4045,12 +4058,6 @@ CDDBAVAIL=y
 GREP_OPTIONS=""
 GREP_COLOR=""
 
-# Length of the terminal *should* be in $LINES. If it's not, guess at
-# a reasonable number instead
-if [ -z "$LINES" ]; then
-       LINES=24
-fi
-
 if [ -z "$OUTPUTDIR" ]; then
        OUTPUTDIR=$(pwd)
 fi