Search code examples
javabashconsolecygwinansi-escape

BASH control sequences parser or regex?


I've stumbled across this topic because I'm developing a console app, currently on a Windows machine using Cygwin to run it (but it will have to run in Linux BASH too at some stage).

Initially all I want to do is apply colour, so I've found out about all these "(ESC)[31m... (ESC)[1m" type directives... for foreground and background colours.

But in the course of testing I also want to be able to strip such codes and just get to the "non-markup" text. With other forms of markup (HTML notably) there are tools available. Is there anything like this for these console codes? Googling around I found something in C, something in Python, but nothing in Java.

I think the codes involved here (for Cygwin and BASH) are the "ANSI" Control codes... but I'm not sure. Could someone maybe confirm this?

Failing a full-blown parser, a regex covering all cases would be helpful. If necessary I'll try to roll one myself from the documentation, but it'd be nice to get one off the shelf from the experts...


Solution

  • Have a look at the well known jansi library - especially AnsiString in there:

    AnsiString ansiString = new AnsiString("string with escape codes");
    String plainString = ansiString.getPlain();
    

    You can also have a look at the ansi-econsole plugin for Eclipse. It's an Eclipse plugin that understands ANSI escape sequences to color the Eclipse console output.