tput ed doesn't work once terminal starts scrolling

154 Views Asked by At

I'm writing an interactive bash script, which needs to keep editing its own output. For the sake of example let's say it looks like this:

tput sc     # Store the cursor position.

while true; do
    tput rc # Restore the cursor position.
    tput ed # Delete everything from cursor position to the end.
    seq 10  # Output whatever.
    sleep 1 # Repeat after some time.
done

I expect to only ever see the output of seq 10 once on my screen, constantly being erased and re-written. If I run this on a fresh terminal, it works. But if my terminal already has enough output in it from previous commands for the scrollbar to appear, it does not work. I just get the output of seq 10 multiple times on my screen, as if my loop was just:

while true; do
    seq 10
    sleep 1
done

This happens in both Windows Terminal and MinTTY. Anything I can do? Thanks.

EDIT: Here is the output of stty -a:

speed 38400 baud; rows 43; columns 171; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = ^Z; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff -iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -tostop echoctl echoke -flusho
1

There are 1 best solutions below

1
Philippe On

You can save screen with smcup and restore with rmcup :

#!/usr/bin/env bash

tput smcup
clear
tput sc     # Store the cursor position.

for c in {1..10} ; do
    tput rc # Restore the cursor position.
    tput ed # Delete everything from cursor position to the end.
    date
    seq 10  # Output whatever.
    sleep 1 # Repeat after some time.
done

tput rmcup