BASH Shell交互式会话-如何修复ASCII艺术动画输出? [英] BASH Shell Interactive Session - How to fix ASCII art animation output?
问题描述
我正在尝试为以下ASCII艺术制作动画。 (我现在有两个文件,以后可能会添加更多文件以获得更细腻的动画。)
I'm trying to animate the following ASCII art. (I have two files right now and may add more later for more fine grained animation.)
$ cat ~/aks1.txt
\ RTX /
\ /
\ WAZUH LAB /
] [ ,'|
] [ / |
]___ ___[ ,' |
] ]\ /[ [ |: |
] ] \ / [ [ |: |
] ] ] [ [ [ |: |
] ] ]__ __[ [ [ |: |
] ] ] ]\ _ /[ [ [ [ |: |
] ] ] ] (A) [ [ [ [ :===='
] ] ]_].nRn.[_[ [ [
] ] ] HHUHH. [ [ [
] ] / `HN("N \ [ [
]__]/ HNH " \[__[
] NNN [
] / N/" \ [
] / N H \ [
/ N \
/ q, \
/ \
$ cat ~/aks2.txt
\ RTX /
\ /
\ WAZUH LAB /
] [ ,'|
] [ / |
]___ ___[ ,' |
] ]\ /[ [ |: |
] ] \ / [ [ |: |
] ] ] [ [ [ |: |
] ] ]__ __[ [ [ |: |
] ] ] ]\ _ /[ [ [ [ |: |
] ] ] ] (A) [ [ [ [ :===='
] ] ]_].nRn.[_[ [ [
] ] ] .HHUHH [ [ [
] ] / #")NH` \ [ [
]__]/ " HNH \[__[
] NNN [
] / "\N [
] H N \ [
/ / N \ \
/ / ,p \ \
/ \
到目前为止,这是我的代码:
Here's my code so far:
if [[ -t 0 ]]; then
old_tty=$(stty --save)
stty raw -echo min 0
fi
while IFS= read -r REPLY; [[ -z "$REPLY" ]]; do
clear
cat ~/aks1.txt
usleep 500000
clear
cat ~/aks2.txt
((c++)) && usleep 500000
done
if [[ -t 0 ]]; then
stty "$old_tty"
fi
echo
优点:
- 简单的方法/解决方案
- 不需要复杂的变量替换(在tput杯屏幕位置)
- 效果很好,直到按下ANY键为止,因此不需要使用硬编码的COUNTER变量即可运行动画的寿命。
- 动画是可以工作的(动画正在发生,但是输出不能完美呈现)。
- Simple approach/solution
- No complex variable substitution required (at tput cup screen locations)
- Works well until ANY key is pressed so no hard-coded COUNTER variable is required for running the life of the animation.
- Animation is kind of working (animation is happening, but the output is not rendering perfectly).
缺点:
- 动画输出乱码/肮脏。
如何解决输出问题?
推荐答案
当您切换到原始模式换行符( \n
)不再将光标移回第一列。他们只将其向下移动。您必须打印回车符( \r
)才能重置列。
When you switch into raw mode newlines (\n
) no longer move the cursor back to the first column. They only move it down a line. You have to print carriage returns (\r
) to reset the column.
您可以通过禁用来实现将它们添加到带有sed的每一行的末尾:
You could do that by disabling adding them to the end of every line with sed:
sed 's/$/\r/g' ~/aks1.txt
或者,您可以跳过切换到原始模式并使终端保持默认状态。为防止 read
命令阻塞,请使用 read -t 0
添加0秒超时。如果用户没有按任何键,它会立即返回,而不是等待他们按某些键。
Alternatively, you could skip switching into raw mode and leave the terminal in its default state. To prevent the read
command from blocking use read -t 0
to add a 0-second timeout. If the user hasn't pressed a key it'll return immediately instead of waiting for them to press something.
until read -t 0; do
...
done
这篇关于BASH Shell交互式会话-如何修复ASCII艺术动画输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!