#!/bin/zsh
zparseopts -D -E p:=padding -padding:=padding
lines=("$@")
if [[ -z $lines ]]; then
	lines=()
	while read line; do
		lines+=($line)
	done
fi

width=0
for line in $lines; do
	if [[ ${#line} -gt $width ]]; then
		width=${#line}
	fi
done

lpad_width=$((width + ${padding[2]:-0}))
rpad_width=$((lpad_width * 2))

pagga=( $(
for i in {1..$#lines}; do
	line=$lines[$i]
	padded="${(r:$(( rpad_width - ${#line})):: :)${(l:${lpad_width}:: :)line}}"
	spaces=$(echo $line | tr -cd ' ' | wc -c)
	if [[ $spaces -gt 0 ]]; then
		padded="$padded${(l:spaces:: :)}"
	fi
	echo $padded
done | figlet -fpagga -t
) )

left_space=$(( (COLUMNS - ${#pagga[1]}) / 2 ))
for line in ${pagga[*]}; do
	echo "${(l:left_space:: :)}$line"
done
