Process ANSI escape codes before piping

3

I'm trying to pipe the output of a script (Mocha) to another script. However there is one problem: Mocha generates quite a few ansi escape characters to update the screen on the fly. These characters are also send through the pipe.

Is there a way to process the ansi sequence such that the output is the same as the final output to the screen? I do want to keep color escape sequences, but not the curser movement escapes.

Edit: I have a partial solution now (for Mocha only): so far it seems that Mocha with the spec output (the one I use) only generates color ecape characters and the CSI 0G escape sequence. The CSI 0G escape character means that the cursor should move back to the beginning of the line. Mocha uses this to overwrite a line completely. Therefore you could simply create a sed regexp which will delete everything up to that escape sequence on a line: sed 's/^.*\x1b\[0G//g'. I am still looking for the complete solution though.

Tiddo

Posted 2012-10-17T20:45:35.880

Reputation: 262

The same question exists here http://stackoverflow.com/questions/6306728/remove-ansi-codes-when-storing-script-output

– dset0x – 2012-10-17T20:50:17.867

@zmode that's not the identical, that one is about removing all ansi codes, whereas here the OP wants to "keep color escape sequences" – eis – 2012-10-17T20:55:58.943

@Tiddo Do you know how the other script would handle the color escapes? it would sound peculiar that it could handle them... – eis – 2012-10-17T21:02:00.573

@eis - The other script is ansifilter. It generates colored HTML from ansi input. By design it is able to handle color escapes, but for some reason it can't handle the movement escape characters.

– Tiddo – 2012-10-17T21:36:01.653

@Tiddo ok, makes sense. – eis – 2012-10-18T08:24:33.350

Answers

0

sed -e 's/\x1b\[[0-9?]\+[^m0-9?]//g'

should remove all escape codes that are not "Select Graphic Rendition" codes — things like color, bold, italics.

Edit: You should also use your sed 's/^.*\x1b\[0G//g' filter before mine if you want the previous text to actually be overwritten. Otherwise both the previous text and the new text will be output. If you want all of the sequences to be carried out so that the output is the same as what would be displayed in a terminal emulator, you'd have to make some sort of background terminal emulator that could actually execute all the codes, and at that point, such a project would eclipse ansifilter.

NighttimeDriver50000

Posted 2012-10-17T20:45:35.880

Reputation: 136