bash - 如何在终端窗口的底部显示文本,并让它留在 Bash 中?

标签 bash tput

我正在使用 while 循环来执行生成一堆文件的 ffmpeg 操作,我希望屏幕底部有一个指示器,指示我正在处理的文件,它位于屏幕底部,同时ffmpeg 正在提供输出。有点类似于 apt 包管理器中的方式,有一个进度条始终位于底部,同时它在其上方提供输出信息。我不需要进度条,只需要一串包含文件编号的文本,它始终位于底部。

我的代码的一个非常简化的版本:

# 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"

最佳答案

使用tput。在你的代码中替换

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

print_status

并将其放在您的代码之前:

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

参见:man tputman 5 terminfo

关于bash - 如何在终端窗口的底部显示文本,并让它留在 Bash 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63661567/

相关文章:

linux - 参数列表过长时如何正确使用 "xargs"

bash - 如何删除 BASH 中只包含数字的行?

Bash:如何从数字中提取数字

gnu-make - 将 tput 与 make 一起使用

rust - 无法使用 std::io::process::Command 执行 `tput` 命令

c - 将 STDOUT 和 STDERR 重定向到 C 中的套接字?

linux - 在 Shell 中显示未找到的命令以及正确的退出状态。这怎么能解决?