如何在控制台中打印 shell 管道并从左向右滑动单词?
How can I make a shell pipeline print and slide a word from left to right in the console?
我使用 toilet 编写了一个简单的管道来在每次打开控制台时打印出我的用户名,我希望能够让它始终如一地滑动,因为打印的字有几行高。
toilet -t -f ivrit 'rob93c' | lolcat
Script output
我试着用这个脚本让它移动,但我显然遗漏了一些东西,因为它没有移动
while true; do echo ' ' && toilet -t -f ivrit 'rob93c' | lolcat
sleep 1
done
slide() {
local -a content
local line prefixed_line cut_line
readarray -t content || return # read our stdin into an array
for ((prefix=0; prefix<=COLUMNS; prefix++)); do # loop increasing # of spaces
for line in "${content[@]}"; do # for lines in our input array...
printf -v prefixed_line "%${prefix}s%s" '' "$line" # first add spaces in front
cut_line=${prefixed_line:0:$COLUMNS} # then trim to fit on one line
printf '%s\n' "$cut_line" # finally, print our trimmed line
done
tput cuu "${#content[@]}" # move the cursor back up
done
}
用作:
toilet -t -f ivrit 'rob93c' | lolcat | slide
...或者,允许没有安装所有这些工具的人进行测试:
printf '%s\n' 'one' ' two' ' three' | slide
我使用 toilet 编写了一个简单的管道来在每次打开控制台时打印出我的用户名,我希望能够让它始终如一地滑动,因为打印的字有几行高。
toilet -t -f ivrit 'rob93c' | lolcat
Script output
我试着用这个脚本让它移动,但我显然遗漏了一些东西,因为它没有移动
while true; do echo ' ' && toilet -t -f ivrit 'rob93c' | lolcat
sleep 1
done
slide() {
local -a content
local line prefixed_line cut_line
readarray -t content || return # read our stdin into an array
for ((prefix=0; prefix<=COLUMNS; prefix++)); do # loop increasing # of spaces
for line in "${content[@]}"; do # for lines in our input array...
printf -v prefixed_line "%${prefix}s%s" '' "$line" # first add spaces in front
cut_line=${prefixed_line:0:$COLUMNS} # then trim to fit on one line
printf '%s\n' "$cut_line" # finally, print our trimmed line
done
tput cuu "${#content[@]}" # move the cursor back up
done
}
用作:
toilet -t -f ivrit 'rob93c' | lolcat | slide
...或者,允许没有安装所有这些工具的人进行测试:
printf '%s\n' 'one' ' two' ' three' | slide