Đầu ra in mã ở giữa màn hình


10

Mã dưới đây sẽ xuất ra bất cứ thứ gì filetừng chữ trên màn hình. Ví dụ:

Hello sẽ được hiển thị trong 1 giây và biến mất. Sau đó, từ tiếp theo trong câu sẽ xuất hiện trong một giây và biến mất và cứ thế.

Làm cách nào để xuất bất cứ thứ gì đang được hiển thị ở giữa màn hình?

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

Chính xác thì bạn đang cố gắng đạt được điều gì?
muru

lệnh đó hiển thị mỗi từ tạo thành một tệp ở góc trên cùng bên trái của màn hình. Tôi cần biết làm thế nào để tạo đầu ra ở giữa màn hình.
Nebelz Cheez

4
Vâng, nhưng bạn đang cố gắng đạt được điều gì? Điều này nghe có vẻ như là một vấn đề XY ,
muru

"Giữa màn hình" là gì? Giữa một thiết bị đầu cuối? Giữa màn hình thực tế? Điều gì sẽ xảy ra nếu bạn thay đổi kích thước thiết bị đầu cuối, bạn có cần điều này để tự động đặt văn bản ở giữa bất kỳ kích thước nào mà thiết bị đầu cuối của bạn có?
terdon

Đúng. Giữa nhà ga.
Nebelz Cheez

Câu trả lời:


7

Đây là một kịch bản bash rất mạnh mẽ:

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

Hãy thử kịch bản dưới đây. Nó sẽ phát hiện kích thước của thiết bị đầu cuối cho mỗi từ đầu vào, do đó thậm chí sẽ tự động cập nhật nếu bạn thay đổi kích thước thiết bị đầu cuối trong khi nó đang chạy.

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

Lưu nó dưới dạng ~/bin/foo.sh, làm cho nó có thể thực thi được ( chmod a+x ~/bin/foo.sh) và cung cấp cho nó tệp đầu vào của bạn làm đối số đầu tiên:

foo.sh file

3

hàm bash để làm tương tự

mpt() { 
   clear ; 
   w=$(( `tput cols ` / 2 ));  
   h=$(( `tput lines` / 2 )); 
   tput cup $h;
   printf "%${w}s \n"  "$1"; tput cup $h;
   sleep 1;
   clear;  
}

và sau đó

mpt "Text to show"

1
Điều này có vẻ giống hệt như câu trả lời của tôi ngoại trừ việc nó chỉ ra một điều và không phải mỗi từ trong câu được đọc từ một tệp riêng theo yêu cầu của OP.
terdon

1

Đây là tập lệnh Python tương tự như giải pháp của @ Heliobash :

#!/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)
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.