Bash - 文字列を美しくスクロール

Bash - 文字列を美しくスクロール

だから私はbashを使ってコンソールから美しい水平スクロールテキストを作成しようとしています。私は何をすべきかわかりません。例は次のとおりです。

Original string: Hi! How are you doing on this fine evening?

1st loop:
Hi! H
2nd:
i! Ho
3rd:
! How
4th:
 How 

など。どうすればいいですか?最後に印刷されたループを削除して、滑らかなスクロール文字列になることをお勧めします。どんな助けでも大変感謝します!ありがとうございます! (質問も自由に聞いてみてください。説明が少し不便ですね:P)

ベストアンサー1

これは少しハッキング的ですが、これがあなたが説明する効果だと思います。出力では、各行は前の項目を上書きするため、<marquee> 前のタグと同様の内容が端末に生成されます。

Hello There!
ello There! He
o There! Hello
There! Hello
ere! Hello The
e! Hello There
Hello There!
ello There! He
lo There! Hell
There! Hello
here! Hello Th
re! Hello Ther
! Hello There!
Hello There! H
llo There! Hel
o There! Hello
There! Hello T
here! Hello Th
e! Hello There
Hello There!
#!/bin/bash

function slice_loop () { ## grab a slice of a string, and if you go past the end loop back around
    local str="$1"
    local start=$2
    local how_many=$3
    local len=${#str};

    local result="";

    for ((i=0; i < how_many; i++))
    do
        local index=$(((start+i) % len)) ## Grab the index of the needed char (wrapping around if need be)
        local char="${str:index:1}" ## Select the character at that index
        local result="$result$char" ## Append to result
    done

    echo -n $result
}

msg="Hello There! ";
begin=0

while :
do
    slice=$(slice_loop "$msg" $begin 14);
    echo -ne "\r";
    echo -n $slice;
    echo -ne "              \r";
    sleep 0.08;
    ((begin=begin+1));
done

ファイルに保存して実行すると、文字列の"Hello There!"スクロールが通過するのがわかります。少しタイトですが、調整してよりきれいにすることができます。

おすすめ記事