是否可以使用bash脚本在终端中打印旋转光标? [英] Is it possible to print a spinning cursor in a terminal using bash scripting?
本文介绍了是否可以使用bash脚本在终端中打印旋转光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我见过使用 c
, python
, ruby
但使用bash的解决方案.我发现最接近的设计是使用此ruby控制台解决方案
I have seen solutions which use c
,python
,ruby
but bash. The closest possible design I found was using this ruby console solution
10.times {
print "/"
sleep(0.1)
print "\b"
print "-"
sleep(0.1)
print "\b"
print "\\"
sleep(0.1)
print "\b"
print "|"
sleep(0.1)
print "\b"
}
我想要一种类似的效果.
I want an effect something simillar to that.
推荐答案
旋转器很好,但是如果您真的想要一个可控制的进度表,该进度表知道您要处理的IO,请查看 pv .
Spinners are nice, but if you really want a controllable progress meter that is aware of the IO you're dealing with, take a look at pv.
这是一个快速而且肮脏的微调器.( sleep
的许多非标准实现会让您睡一秒钟.)
Here's a quick-and-dirty spinner. (Many nonstandard implementations of sleep
will let you sleep for fractions of a second.)
spin() {
local -a marks=( '/' '-' '\' '|' )
while [[ 1 ]]; do
printf '%s\r' "${marks[i++ % ${#marks[@]}]}"
sleep 1
done
}
POSIX Sh
spin() {
i=0
marks='/ - \ |'
while true; do
if [ $# -lt 4 ]; then
set -- "$@" $marks
fi
shift $(( (i+1) % $# ))
printf '%s\r' "$1"
sleep 1
done
}
这篇关于是否可以使用bash脚本在终端中打印旋转光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文