移动插入符号 BASH
Walking caret BASH
我一直在开发一个小型打字游戏,它可以创建一个随机字符串,分解该字符串,并观察输入是否与显示的字符相匹配。
感谢我在这里收到的所有帮助,所以我很接近......但是。我完全被难住了。我的问题:
在每个字符下实现行走“^”的最佳方法使得
3 x P ! 0 D 3 D 5 T r ! n G
^
首先设置为我可以检查的变量,然后使用正确的输入移动
3 x P ! 0 D 3 D 5 T r ! n G
^
我不想在这里粘贴大量代码,所以
查看我的 github https://github.com/archae0pteryx/chuppy
以供参考。谢谢!
这是我的快速尝试
#!/bin/bash
len=14
pos=0
str="$(tr -Cd 'A-Za-z0-9' < /dev/urandom | head -c "$len" | sed -e 's/./& /g')"
show_caret() {
tput el1
head -c $((2*pos)) /dev/zero | tr '[=10=]' ' '
echo '^'
}
echo "${str}"
show_caret
while (( $pos < $len )); do
read -rsN1 ch
if [[ $ch == "${str:$((2*pos)):1}" ]]; then
((pos++))
tput cuu1
show_caret
fi
done
希望你能从中有所收获。
一种天真的方法是结合使用 read -n1
和 printf
。 \r
将光标移动到行首,read -n1
将读取一个字符作为输入:
$ cat walk.bash
#!/bin/bash
echo "3 x P ! 0 D 3 D 5 T r ! n G"
i=1
answer=""
while [ "${#answer}" -lt 14 ]; do
printf "\r%$((i * 2 - 1))s" "^"
read -rn1 -s ans
answer+="$ans"
((i++))
done
printf '\n'
echo "$answer"
$ ./walk.bash
3 x P ! 0 D 3 D 5 T r ! n G
^
我一直在开发一个小型打字游戏,它可以创建一个随机字符串,分解该字符串,并观察输入是否与显示的字符相匹配。 感谢我在这里收到的所有帮助,所以我很接近......但是。我完全被难住了。我的问题: 在每个字符下实现行走“^”的最佳方法使得
3 x P ! 0 D 3 D 5 T r ! n G
^
首先设置为我可以检查的变量,然后使用正确的输入移动
3 x P ! 0 D 3 D 5 T r ! n G
^
我不想在这里粘贴大量代码,所以 查看我的 github https://github.com/archae0pteryx/chuppy 以供参考。谢谢!
这是我的快速尝试
#!/bin/bash
len=14
pos=0
str="$(tr -Cd 'A-Za-z0-9' < /dev/urandom | head -c "$len" | sed -e 's/./& /g')"
show_caret() {
tput el1
head -c $((2*pos)) /dev/zero | tr '[=10=]' ' '
echo '^'
}
echo "${str}"
show_caret
while (( $pos < $len )); do
read -rsN1 ch
if [[ $ch == "${str:$((2*pos)):1}" ]]; then
((pos++))
tput cuu1
show_caret
fi
done
希望你能从中有所收获。
一种天真的方法是结合使用 read -n1
和 printf
。 \r
将光标移动到行首,read -n1
将读取一个字符作为输入:
$ cat walk.bash
#!/bin/bash
echo "3 x P ! 0 D 3 D 5 T r ! n G"
i=1
answer=""
while [ "${#answer}" -lt 14 ]; do
printf "\r%$((i * 2 - 1))s" "^"
read -rn1 -s ans
answer+="$ans"
((i++))
done
printf '\n'
echo "$answer"
$ ./walk.bash
3 x P ! 0 D 3 D 5 T r ! n G
^