From 02620a07969b078b86cb6a034ef574fee37104a7 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 10 Feb 2025 10:25:13 -0800 Subject: Don't interrogate terminal too often in basta.check_cursor. - We take advantage of the $EPOCHSECONDS variable available in newer Bash to get the current time. We don't perform the cursor position or resize check in basta.check_cursor if it has been done in the last 5 seconds, in addition to continuing to avoid it if there is buffered TTY input. --- basta.sh | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/basta.sh b/basta.sh index 174dbd0..93e943a 100644 --- a/basta.sh +++ b/basta.sh @@ -5,6 +5,7 @@ basta_old_cmdno=${basta_old_cmdno-0} basta_old_lines=${basta_old_lines-0} basta_old_cols=${basta_old_cols-0} +basta_last_check_epoch=${basta_last_check_epoch-0} basta_scroll_lines=${basta_scroll_lines-0} basta_prev_reserved_rows=${basta_prev_reserved_rows-} @@ -58,6 +59,20 @@ basta.get_cur_line() printf "%s\n" "$2" } +if [ $EPOCHSECONDS ] ; then + basta.epoch() + { + local -n intovar=$1 + intovar=$EPOCHSECONDS + } +else + basta.epoch() + { + local -n intovar=$1 + intovar=$(date +%s) + } +fi + basta.update_status() { local pwd=$PWD @@ -97,18 +112,24 @@ basta.update_status() basta.check_cursor() { if ! read -t 0; then - local exit=$? - local curln=$(basta.get_cur_line) - local realrows + local now + basta.epoch now - if [ $curln ] && [ $curln -gt $basta_scroll_lines ]; then - printf $'\e[%s;1H' $basta_scroll_lines - fi + if [ $((now - 5)) -ge $basta_last_check_epoch ] ; then + local exit=$? + local curln=$(basta.get_cur_line) + local realrows - basta.query_terminal_lines realrows + if [ $curln ] && [ $curln -gt $basta_scroll_lines ]; then + printf $'\e[%s;1H' $basta_scroll_lines + fi - [ $LINES -eq $((realrows - $basta_prev_reserved_rows - 1)) ] - return $? + basta_last_check_epoch=$now + + basta.query_terminal_lines realrows + [ $LINES -eq $((realrows - $basta_prev_reserved_rows - 1)) ] + return $? + fi fi return 0 -- cgit v1.2.3