我想在 Linux 终端内制作动画文本。
“动画”,我的意思是单个角色不断变化以使其形成某种动画。 我想要的是这样的:
| # First second
/ # One second after previous frame
- # One second after previous frame
| # One second after previous frame
- # One second after previous frame
\ # Last frame, skip back to top
...所以它创建了一条看起来在旋转的线。
我已经很清楚使用
clear
的方法,但这不符合我的需求。
clear
删除所有文本,包括之前的文本。我不想要这个。我在终端窗口中看到很多动画没有这样做(例如使用 apt install
显示的加载栏),但是,我无法重现。
如何使用 Linux 终端制作动画,例如加载栏?如果是一个库,我更喜欢它是 C++。
使用 tput 是这里的关键,它是 ncurses 的一部分。
这是我编写的示例脚本:-
#!/usr/bin/env bash
clear # Let clear the screen first
echo " Starting the spinning wheel.."
echo " Press 'q' or 'Q' to stop it.."
# to set the cursor position on terminal on row 2 column 1
tput cup 2 1
while true; do
echo '|'
sleep 1
tput cup 2 1 # reset cursor position
echo '/'
sleep 1
tput cup 2 1 # reset cursor position
echo '-'
sleep 1
tput cup 2 1 # reset cursor position
echo '\'
sleep 1
tput cup 2 1 # reset cursor position
echo '|'
tput cup 2 1 # reset cursor position
# -t for timeout, -N to except only 1 character
read -t 0.25 -N 1 ch
if [[ $ch = "q" ]] || [[ $ch = "Q" ]]; then
# The following line is for the prompt to appear on a new line.
echo
break
fi
done
这就像一个基本版本。您可以进一步完善它。
问候, 尤拉杰