Search code examples
bashtput

How to display text at the bottom of the terminal window, and make it stay there, in Bash?


I am using a while loop to perform an ffmpeg operation that makes a bunch of files, and I want an indicator at the bottom of the screen that says what file I am on, that stays at the bottom of the screen, while ffmpeg is giving output. Sort of similar to how in the apt package manager, there is a progress bar that is always at the bottom, while it gives output information above it. I don't need a progress bar, just a string of text containing the file number to always be at the bottom.

A very simplified version of my code:

# Initialize file number
file_number=1

while IFS=, read -r starttime name endtime; do
    # ffmpeg command
    ffmpeg -ss $starttime_seconds -i "$1" -t $duration -codec libopus "$safename" < /dev/null

    # Display progress, this is what I want at the bottom of the screen
    echo -en "\r--- FILE $file_number ---"
    file_number=$((file_number+1))
done < "$2"

Solution

  • With tput. Replace in your code

    echo -en "\r--- FILE $file_number ---"
    

    with

    print_status
    

    and put this before your code:

    LINES=$(tput lines)
    
    set_window ()
    {
        # Create a virtual window that is two lines smaller at the bottom.
        tput csr 0 $(($LINES-2))
    }
    
    print_status ()
    {
        # Move cursor to last line in your screen
        tput cup $LINES 0;
    
        echo -n "--- FILE $file_number ---"
    
        # Move cursor to home position, back in virtual window
        tput cup 0 0
    }
    
    set_window
    

    See: man tput and man 5 terminfo