#!ruby -n a=[] scan(/#{'(\w+|\(([^)]+).)/'*2}?|./){a<<($1?(w=[x=$2||$1,y=$4||$3].map(&:size).max;[x.center(w),"-"*w,y.center(w)]):[" ",$&," "])} puts a.transpose.map(&:join),$/
Note that non-ascii characters in the above source code will be escaped (such as \x9f).