พิมพ์เอาท์พุทของรหัสที่อยู่ตรงกลางของหน้าจอ


10

โค้ดด้านล่างจะแสดงผลอะไรก็ตามทีfileละคำบนหน้าจอ ตัวอย่างเช่น:

Hello จะปรากฏเป็นเวลา 1 วินาทีและหายไป จากนั้นคำถัดไปในประโยคจะปรากฏขึ้นเป็นวินาทีและหายไปเรื่อย ๆ

ฉันจะแสดงสิ่งที่แสดงอยู่กลางหน้าจอได้อย่างไร

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

อะไรคือสิ่งที่คุณพยายามทำให้สำเร็จ
muru

คำสั่งนั้นแสดงคำแต่ละคำในรูปแบบไฟล์ที่มุมซ้ายบนของหน้าจอ ฉันจำเป็นต้องรู้วิธีการส่งออกในช่วงกลางของหน้าจอ
Nebelz Cheez

4
ใช่ แต่คุณพยายามทำอะไรให้สำเร็จ เสียงนี้เช่นปัญหา XY ,
Muru

"กึ่งกลางของหน้าจอ" คืออะไร ตรงกลางของเทอร์มินัล ตรงกลางของหน้าจอจริง? ถ้าคุณปรับขนาดเทอร์มินัลคุณต้องการให้วางข้อความไว้ตรงกลางแบบใดก็ตามที่เทอร์มินัลของคุณมีขนาดหรือไม่
terdon

ใช่. ตรงกลางของเทอร์มินัล
Nebelz Cheez

คำตอบ:


7

ที่นี่คุณเป็นสคริปต์ทุบตีที่แข็งแกร่งมาก:

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

ฟังก์ชั่นทุบตีที่จะทำเช่นเดียวกัน

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

นี่คือสคริปต์ Python ที่คล้ายกับโซลูชันของ @ 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)
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.