在屏幕中间打印代码输出


10

下面的代码将file在屏幕上逐字输出任何内容。例如:

Hello 将显示1秒钟并消失。然后,句子中的下一个单词将出现一秒钟,然后消失,依此类推。

如何输出屏幕中间显示的内容?

awk '{i=1; while(i<=NF){ print $((i++)); system("sleep 1; clear") }}' file

您到底想达到什么目的?
muru

该命令在屏幕的左上角以文件形式显示每个单词。我需要知道如何在屏幕中间进行输出。
Nebelz Cheez

4
是的,但是您要达到什么目的?这听起来像是XY问题
大师

什么是“屏幕中间”?码头中间?实际画面中间?如果您调整终端的大小,您需要它来将文本动态地放置在中间,无论终端具有什么大小吗?
terdon 2015年

是。终端的中间。
Nebelz Cheez

Answers:


7

这是一个非常强大的bash脚本:

#!/bin/bash

## When the program is interrupted, call the cleanup function
trap "cleanup; exit" SIGHUP SIGINT SIGTERM

## Check if file exists
[ -f "$1" ] || { echo "File not found!"; exit; }

function cleanup() {
    ## Restores the screen content
    tput rmcup

    ## Makes the cursor visible again
    tput cvvis
}

## Saves the screen contents
tput smcup

## Loop over all words
while read line
do
    ## Gets terminal width and height
    height=$(tput lines)
    width=$(tput cols)

    ## Gets the length of the current word
    line_length=${#line}

    ## Clears the screen
    clear

    ## Puts the cursor on the middle of the terminal (a bit more to the left, to center the word)
    tput cup "$((height/2))" "$((($width-$line_length)/2))"

    ## Hides the cursor
    tput civis

    ## Prints the word
    printf "$line"

    ## Sleeps one second
    sleep 1

## Passes the words separated by a newline to the loop
done < <(tr ' ' '\n' < "$1")

## When the program ends, call the cleanup function
cleanup

8

试试下面的脚本。它会检测每个输入字的终端大小,因此,如果您在运行终端时调整其大小,它甚至会动态更新。

#!/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

3

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"

1
这似乎与我的答案完全相同,除了它显示一件事,而不是按照OP的要求从文件中单独读取句子的每个单词。
terdon

1

这是类似于@Helio bash解决方案的Python脚本:

#!/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)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.