如何为终端制作动画 bash shell 提示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29448625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:44:04  来源:igfitidea点击:

How to make an animated bash shell prompt for the terminal?

bashanimationterminalcommand-promptubuntu-14.04

提问by Black Block

I want to make an animated curser prompt in the terminal(Ubuntu 14.04),
so i make this script:

我想在终端(Ubuntu 14.04)中制作一个动画光标提示,
所以我制作了这个脚本:

while [ : ]
do
    echo -ne '|\r'
    sleep 0.3
    echo -ne '/\r'
    sleep 0.3
    echo -ne 'һ\r'
    sleep 0.3
    echo -ne '\ \r'
    sleep 0.3
    echo -ne '|\r'
    sleep 0.3
    echo -ne '$\r'
    sleep 0.3
done

But when i put it in the .bashrc ps1 ~/animated-prompt.sh &it make a text distortion and every thing i write is overridden by the \rcharacter.
enter image description here
so Is there anyway to make this work ?
(IDK, like putting the script to another process thread other than the one that the terminal is working on)

但是当我把它放在 .bashrc ps1 中时,~/animated-prompt.sh &它会使文本失真,我写的每件事都被\r字符覆盖。
在此处输入图片说明
那么有没有办法让这项工作?
(IDK,就像将脚本放到终端正在处理的线程之外的另一个进程线程中一样)

回答by Abubaker Siddique

save and restore cursor position instead of \r. Move to required column location where you want animation between saving and restoring cursor position.

保存和恢复光标位置而不是 \r。移动到您想要在保存和恢复光标位置之间设置动画的所需列位置。

For cursor movement refer http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

有关光标移动,请参阅http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

modified script:

修改后的脚本:

s="3[s"
u="3[u"

# Position of column
# As per my command prompt, i want 15th column( so 14C)
pos="3[1000D3[14C"
while [ : ]
do
    eval echo -ne '$s$pos\|$u'
    sleep 0.3
    eval echo -ne '$s$pos/$u'
    sleep 0.3
    eval echo -ne '$s$posһ$u'
    sleep 0.3
    eval echo -ne '$s$pos\\$u'
    sleep 0.3
    eval echo -ne '$s$pos\|$u'
    sleep 0.3
    eval echo -ne '$s$pos$$u'
    sleep 0.3
done

[root@hello ~]|

As you were using, it works with last line in .bashrc

当您使用时,它适用于 .bashrc 中的最后一行

Thanks

谢谢

回答by Steven Penny

echo -ne 'һ\r'
#         ^
#         |
#         \--- problem

This character should be a hyphen, but is actually something called CJK Ideograph, First

这个字符应该是一个连字符,但实际上是一种叫做CJK Ideograph 的东西 ,首先