I basically want to manipulate the output of some program connected to a terminal so that the bottom section of text is always some arbitrary block of text (let's call it the footer), while the normal output of the program is displayed above that. If this footer was confined to a single terminal line, this would be very easy to do by simply clearing the current line, moving the cursor to the beginning of the line before each write
call, and then rewriting the footer. However, if my desired footer spans multiple terminal lines, either by including newline characters or by lines wrapping around the edge of the screen, things are complicated. I thought I might be able to work around that with the "save cursor" and "restore cursor" VT100 control codes, which would be emitted as such for every write:
write
callHowever these don't work when the output text reaches the bottom of the terminal because the saved cursor will always be at the bottom row.
Is there any way achieve this arbitrary terminal footer? Something with just VT100 codes would be ideal, but if the only way is to use curses then I suppose that's possible, too.
You could do this using a scrolling region. Something like this (keeping in mind that while you could hard-code escape sequences, this is more readable):
#!/bin/sh
rows=$(tput lines)
foot=$((rows - 4))
tput csr 1 $((foot - 1))
count=0
while true
do
date
count=$((count + 1))
tput sc
tput cup $foot 1
printf "Total cycles %d", $count
tput rc
sleep 1
done