0

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...

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
mike rodent
  • 14,126
  • 11
  • 103
  • 157

1 Answers1

2

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.

René Scheibe
  • 1,970
  • 2
  • 14
  • 20
  • Thanks, that looks great! – mike rodent Feb 26 '17 at 20:55
  • (Later) Jansi is apparently the way to go... but the documentation (even Javadoc commenting) leaves quite a lot to be desired. I'm currently not using it, but instead "rolling my own", because my needs are currently very very simple (i.e. a bit of colour, and bolding). Any more and I'd have to examine the Jansi source code to find out how to use it, faute de mieux. – mike rodent Mar 03 '17 at 08:25