centercat (ccat)

Gibt den Inhalt einer Datei zentriert auf stdout aus; kann gepiped werden.

#!/usr/bin/env bash

centercat() {
    local file="$1"

    [[ -f "$file" ]] || return 1

    local term_width term_height
    term_width=$(tput cols)
    term_height=$(tput lines)

    mapfile -t lines < "$file"

    local content_height="${#lines[@]}"
    local content_width=0
    local len

    for line in "${lines[@]}"; do
        len=$(printf '%s' "$line" | wc -L)
        (( len > content_width )) && content_width=$len
    done

    local pad_top=$(( (term_height - content_height) / 2 ))
    local pad_left=$(( (term_width - content_width) / 2 ))

    (( pad_top < 0 )) && pad_top=0
    (( pad_left < 0 )) && pad_left=0

    # Cursor nach oben links
    tput cup 0 0

    # Gesamten Bildschirm löschen
    tput ed

    for ((i=0; i<pad_top; i++)); do
        echo
    done

    for line in "${lines[@]}"; do
        printf "%*s%s\n" "$pad_left" "" "$line"
    done
}

# Cursor verstecken
tput civis

trap 'tput cnorm; clear; exit' INT TERM

while true; do
    centercat "$1"
    sleep 1
done

Usage

centercat [/path/to/file.txt] | lolcat

Man könnte es auch als „ccat“ speichern, oder ein alias setzen…

Beispieldatei

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡟⠁⠀⠙⢿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡟⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⣶⣶⣶⣿⡇⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣼⡟⠁⠀⠈⢻⡇⠀⠀⠀⠀⢸⣿⣶⣤⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⣼⡿⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⢸⡏⠀⠀⠉⢿⡆⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⣠⣾⣿⡇⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⢸⡇⠀⠀⠀⠘⣿⠦⣤⣀⠀⠀⠀⠀
⠀⢀⣾⠟⠁⣸⠁⠀⠀⠀⠀⣾⡆⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⢻⡀⠀⠹⣆⠀⠀⠀
⣰⡟⠁⠀⠀⣿⠀⠀⠀⠀⠀⠙⠁⠀⠀⠀⠀⠈⠃⠀⠀⠀⠀⢸⣇⠀⠀⠹⣦⠀⠀
⡿⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⠀⠀⠀⢹⡆⠀
⣿⡄⠀⠀⠈⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⡀
⠙⢿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇
⠀⠈⠻⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⠁
⠀⠀⠀⠈⠻⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠃⠀
⠀⠀⠀⠀⠀⠈⢻⣷⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⠛⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡟⠁⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢸⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⠁⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢸⣷⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣠⣾⠇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠁