Skip to content

Instantly share code, notes, and snippets.

@jamestomasino
Last active May 13, 2022 23:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamestomasino/9b4e91aa38c73e6eb725713a3e120931 to your computer and use it in GitHub Desktop.
Save jamestomasino/9b4e91aa38c73e6eb725713a3e120931 to your computer and use it in GitHub Desktop.
center a file of text visually in the terminal (for ascii art)
#!/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
}
#!/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"
..
.
. . .
.
.......
..
.
.
.
.
@jamestomasino
Copy link
Author

usage:

$ ./center.sh eridanus.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment