The problem is of course with column
not differentiating between printing and non-printing characters, bash
gets around this in its prompt (PS1
) with its \[ \]
escape feature, I don't know of anything else that does exactly that.
I tried a quick and nasty hack, given that your problematic field is at the start of the line, move it to the then end, but you run into similar padding/alignment problems that aren't easily solved using common utilities.
ls -l --color | rev | column -t | rev # not a useful solution
If you have the perl HTML::FromANSI
module install it has a useful ansi2html
script:
ls -l --color | ansi2html -f
That only gets you halfway there, now you have to align HTML output...
There's a straightforward (though slightly heavyweight) solution with HTML as an intermediate: layout of HTML tables is effectively the thing you are trying to do.
This uses Andre Simon's ansifilter, e.g.:
ls -l --color | ansifilter -fH
This converts ANSI sequences to HTML (<span style="..."></span>
), then this can be dumped using a text-mode ANSI capable browser like elinks.
If incomplete HTML is a problem, you can optionally run hxclean
or htmltidy
to clean up the HTML before passing it to a browser.
ls -l --color | ansifilter -fH | perl table.pl | elinks -dump -dump-color-mode 1
You should be able to use elinks or w3m for this
The table.pl
script splits on whitespace and adds the relevant HTML table tags to achieve the desired formatting:
print "<table>\n";
while (<>) {
print "<tr><td>" .
join("</td><td>", split(/((?!<[^>]+)\s+(?![^<]+>))/) ) .
"</td></tr>\n";
}
print "</table>\n";
It doesn't just split on any whitespace, it splits on whitespace that isn't inside '<' '>' angle-brackets so it doesn't break the <span>
tag. This is not a good way to parse HTML, but it should suffice for the type of constrained input here.
You may need (I did) to set or override the default colors, either add these to your ~/.elinks/elinks.conf
file:
set document.colors.use_document_colors = 1
set document.colors.text = "#000000"
set document.colors.background = "#ffffff"
Make sure to use the latest (0.12.x) elinks, earlier versions do not support ANSI colour output.