I'm working on a script that is meant to check a certain status, output the result, sleep for a short time and then start everything over again. The output consists of multiple lines and I want the script to update the output that means to print on the same lines again. It's supposed to work on Mac and Linux. This is what I have so far:
#! /usr/bin/perl
use strict;
print `tput sc`; # Store cursor position
my @lines;
while (1) {
@lines = ();
push(@lines, `tput rc`); # Restore cursor position
push(@lines, `tput ed`); # Clear from cursor to end of screen
push(@lines, `dd if=/dev/urandom bs=1 count=1`); # This is just an example
print @lines;
sleep 1;
}
It works fine as long as I'm not calling the script with my cursor at the end of the terminal window. In the latter case the cursor stays at the end of the window after printing so restoring it's original position does effectively nothing and the next output just goes to a new line.
How can I avoid that?
I don't want to call tput clear
or tput init
since that might loose any previous commands and output. I was thinking about scrolling the window so that the prompt is at the top line before I start outputting but that would require the current cursor row and I can't see how to get that with tput
.
Alternatively, I could remember the number of lines that were printed and then move the cursor back using tput rin
. However, that doesn't work when the output is too long and gets broken across two or more terminal lines.