В приведенном ниже коде будет выводиться любое слово file по слову на экране. Например:
Hello будет отображаться в течение 1 секунды и исчезнет. Затем следующее слово в предложении появится на секунду и исчезнет и т. Д.
Как я могу выводить все, что отображается в середине экрана?
awk '{i=1; while(i<=NF){ print $((i++)); system("sleep 1; clear") }}' file
Попробуйте сценарий ниже. Он будет определять размер терминала для каждого входного слова, поэтому будет даже динамически обновляться, если вы измените размер терминала во время его работы.
#!/usr/bin/env bash
## Change the input file to have one word per line
tr ' ' '\n' < "$1" |
## Read each word
while read word
do
## Get the terminal's dimensions
height=$(tput lines)
width=$(tput cols)
## Clear the terminal
clear
## Set the cursor to the middle of the terminal
tput cup "$((height/2))" "$((width/2))"
## Print the word. I add a newline just to avoid the blinking cursor
printf "%s\n" "$word"
sleep 1
done
Сохраните его как ~/bin/foo.sh, сделайте его исполняемым (chmod a+x ~/bin/foo.sh) и передайте ему свой входной файл в качестве его первого аргумента:
foo.sh file
bash, чтобы сделать то же самое
mpt() {
clear ;
w=$(( `tput cols ` / 2 ));
h=$(( `tput lines` / 2 ));
tput cup $h;
printf "%${w}s \n" "$1"; tput cup $h;
sleep 1;
clear;
}
, а затем
mpt "Text to show"
Вот скрипт Python, похожий на решение @ Helio's bash:
#!/usr/bin/env python
import fileinput
import signal
import sys
import time
from blessings import Terminal # $ pip install blessings
def signal_handler(*args):
raise SystemExit
for signal_name in "SIGHUP SIGINT SIGTERM".split():
signal.signal(getattr(signal, signal_name), signal_handler)
term = Terminal()
with term.hidden_cursor(), term.fullscreen():
for line in fileinput.input(): # read from files on the command-line and/or stdin
for word in line.split(): # whitespace-separated words
# use up to date width/height (SIGWINCH support)
with term.location((term.width - len(word)) // 2, term.height // 2):
print(term.bold_white_on_black(word))
time.sleep(1)
print(term.clear)