You can pipe to cat -v
to see all the characters of this file stream. column
calculates the width using them:
^[[1mfoo^[(B^[[m ^[[1mbar^[(B^[[m
bar foo
The right way is to separate any content creation from any formatting (like the MVC model). Note that column
is adding spaces to produce a tab-like output, so it modifies the content. While tput
adds instructions to the terminal. By separating these tasks, we are able to save the output to a file, or pass it from our formatting function to send it to console, or format it for other destination.
But this is also a good exercise for awk
, to implement column -t
excluding some characters from the width calculations. The regular expression I use below catches a lot of tput
stuff, but I am almost sure not all of them.
$ cat tst.awk
BEGIN {
OFS = " "
}
{
nr = NR
for (i=1;i<=NF;i++) {
f[NR,i] = $i
gsub(/\x1B[^[:alpha:]]+[[:alpha:]]/,"",$i)
if (length($i) > col[i]) col[i] = length($i)
}
}
END {
for (i=1;i<=nr;i++) {
for (j=1;j<=length(col);j++) {
printf "%-"col[j]"s%s", f[i,j], (j==length(col)? ORS : OFS)
}
}
}
and you pipe the first output to awk -f tst.awk
column from util-linux 2.36
. What implementation ofcolumn
are you using?column
from util-linux?util-linux
code doesn't handle escape sequences for colors, making "works well for me" dubious.