Last active
May 13, 2022 23:54
-
-
Save jamestomasino/9b4e91aa38c73e6eb725713a3e120931 to your computer and use it in GitHub Desktop.
center a file of text visually in the terminal (for ascii art)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
BEGIN { | |
"tput cols" | getline c | |
while(getline < ARGV[1]) | |
{ | |
if(length>l){l=length} | |
} | |
w=(c-l)/2 | |
} | |
{ | |
printf "%*s%s\n",w,"",$0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Centers a text document as a whole, preserving indentation of | |
# each line, across the width of the terminal. | |
# get width of longest line in text file | |
w=$(awk '{if(length>L){L=length}}END{print L}' < "$1") | |
# get terminal width | |
c=$(tput cols) | |
# output file to stdout, offset to center all content | |
awk -v l="$(((c - w)/2))" '{printf "%*s%s\n",l,"",$0}' "$1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.. | |
. | |
. . . | |
. | |
....... | |
.. | |
. | |
. | |
. | |
. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage: