Column Command – Issue with Column Command and Color Escape Codes

colorscolumnsescape-characterstabletext formatting

I'm colorizing the header of a table formatted with column -ts $'\t'

Works well without color codes, but when I add color codes to the first line column doesn't properly align the output.

Without colored output it works as expected:
printf "1\t2\t3\nasdasdasdasdasdasdasd\tqwe\tqweqwe\n" | column -ts $'\t'

But when adding color on the first line column doesn't align the text of the colored row:
printf "\e[7m1\t2\t3\e[0m\nasdasdasdasdasdasdasd\tqwe\tqweqwe\n" | column -ts $'\t'

Observed this behaviour both on Ubuntu Linux and Mac OS X.

Best Answer

I imagine that column doesn't know that \e[7m is a v100 escape sequence that takes no space in the output. It seems to asssume character codes 0 to 037 octal take no space. You can get what you want by putting the initial escape sequence on a line of its own, then removing that newline from the output:

printf '\e[7m\n1\t2\t3\e[0m\nasdasdasdasdasdasdasd\tqwe\tqweqwe\n' | 
column -ts $'\t' |
sed '1{N;s/\n//}'