Internationalization Cookbook
This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.

Eclipse plugin – ANSI Escape in Console

This Eclipse plugin interprets the ANSI escape sequences to color the console output.
This can be pretty handy when using something like jansi

But is also works if you output escape sequences directly from Java. Or from Perl, C++, or Groovy, or any other Eclipse hosted language.

ANSI Console Sample

This plugin is not needed starting with Eclipse 2022-09 (4.25)

From Eclipse 2022-09 the official Eclipse Console supports ANSI escape sequences:
https://www.eclipse.org/eclipse/news/4.25/platform.php#debug-ansi-support.

I will keep this plugin up and running for older Eclipse versions, until the number of downloads trickles down to almost nothing.

Installation

To install the plugin in Eclipse select Help -> Install New Software… and click “Add…” to add the following URL:
http://www.mihai-nita.net/eclipse

You can then select the site in the “Work with” list and continue the installation like with any other plugin.

Features

Since this plugin hooks into LineStyleListener, it cannot do anything other than changing the style of the current output.
So no cursor movements, clear screen, etc.

OK, so what CAN it do, you will ask.

From the “SGR (Select Graphic Rendition) parameters” table at
http://en.wikipedia.org/wiki/ANSI_escape_code
it can handle the following subset:

  • 0 – Reset / Normal
  • 1 – Bold: treated as intensity under Windows console, user option in this plugin)
  • 2 – Intensity faint: “kind of” supported :-) It resets the intensity to normal.
  • 3 – Italic: on (treated as inverse under Windows console, user option in this plugin)
  • 4 – Underline
  • 7 – Negative
  • 8 – Conceal
  • 9 – Crossed-out
  • 21 – Double underline
  • 22 – Bold off (normal intensity)
  • 23 – Italic off
  • 24 – Underline off
  • 27 – Negative off
  • 28 – Conceal off
  • 29 – Crossed-out off
  • 30-37 – Set text color
  • 38 – Set xterm-256 text color
  • 39 – Default text color
  • 40-47 – Set background color
  • 48 – Set xterm-256 background color
  • 49 – Default background color
  • 51 – Framed
  • 54 – Framed off
  • 90-97 – Set foreground color, high intensity
  • 100-107 – Set background color, high intensity

The reasons for choosing these and not others are the limitations in the Eclipse console (for instance no blink :-), or limitation in the existing OS consoles (what’s the point in supporting something that can’t be used outside Eclipse).

But the final result is reasonable close to what you get in a real console in terms of colors (so no cursor movements, clear console, etc.)

For more functionality I might have to hook some more Eclipse events, or even to come up with my own console. But then I am not sure it can be used by applications in all kind of languages without code changes.
Considering that this started as a week-end toy project, it does not have such ambitions.
But if someone can point me to some simple methods that would allow me to implement that without tons of work, or even contribute code, I might take it there :-)

I have also tried to emulate Windows look (optional), with bold rendered as high intensity color, and italic as revere, and other attributes ignored.
It is debatable how close you can get to the right thing. Probably nobody knows what the ancient ANSI.SYS supported. And at the time the DOS console did not support 256 colors.
Nowdays the Windows options that I am aware of are jansi, ansicon, and conemu.
Between these three options, the support is uneven, and inconsistent.
In the non-Windows world there are also many terminals, also inconsistent, some of them with their own configurable options for how to treat various attributes.
So I don’t plan to try and emulate all the possible terminals.
But if you notice some glaring differences, let me know and I will try to do something about it.

Quick test

If something does not seem to work, try running this before anything else:

public class HelloColor {
    public static void main(String [] args) {
        System.out.println("Hello \u001b[1;31mred\u001b[0m world!");
    }
}

The result should be something like this:
Hello red world!

Note for jansi

If you use jansi, it might look like the plugin does not work.

That is because jansi detects when the standard output is redirected and does not output the escape sequences anymore.
So try adding this to your code, before using jansi: System.setProperty("jansi.passthrough", "true");

Sources

On GitHub, at https://github.com/mihnita/ansi-econsole.

License

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License.

You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

33 Comments to “Eclipse plugin – ANSI Escape in Console”

  1. […] I am successfully using this plugin for Eclipse – ANSIConsole […]

  2. […] Estoy usando con éxito este plugin para Eclipse – ANSIConsole […]

  3. alex says:

    Hi Mihai,

    I have installed it, enabled it, restarted eclipse multiple times and cannot get it to work colors in my console. Though if I run your hellocolor class, it does show the ‘Hello red world!’ with text red actually in bold red color, so it should work, but it does not.
    Any suggestions please?
    I am using Eclipse Version: 2022-03 (4.23.0)
    Build id: 20220310-1457

    • alex says:

      Earlier I had 2022-09 installed where the plugin comes default and does not need explicit installation – In 2022-09 also it was not working. And thats the reason I had degraded to 2022-03 version but that did not color the logs either. :(

      • Mihai says:

        I’ve created and issue in GitHub (https://github.com/mihnita/ansi-econsole/issues/85)

        Can we please continue the discussion there?

        This site was around for a while, and it ended up in the cross-hairs of some spambots networks.
        So it receives a lot of spam, and legit comments are very easy to miss.

        Unless you don’t have a GitHub account, you hate GitHub, or for some other reason you can’t do it there.
        In that case I will try to keep an eye on the comments here and make sure your comments don’t slip through the cracks and don’t end up in spam.

        Thank you,
        Mihai

  4. […] I am successfully using this plugin for Eclipse – ANSIConsole […]

  5. […] I am successfully using this plugin for Eclipse – ANSIConsole […]

  6. Lidia Popescu says:

    Hello Mihai,
    I can see there is an update for Ansi Console from v.1.3.5.201612301822 -> 1.4.2.202002090224. Do you also have some release notes? What new features it brings?
    Thank you

  7. […] I am successfully using this plugin for Eclipse – ANSIConsole […]

  8. Periata says:

    Worth noting that the Apache log4j framework version 2 has an inbuilt formatting option for using ansi colour to indicate the severity of its messages… just wrap your message format in “%highlight{}”. This alone makes installing this plugin worth doing. :)

  9. Srimu says:

    Hi,
    Is it possible to add text to the console line apart from changing the style of the text?

    • Mihai says:

      Unfortunately, no.
      I could not find any way to do anything outside the current line.
      And in fact the only thing I can do on the current line is to return some ranges with styles, not change the text.
      I am not even removing the escape sequences from the original text, I can only hide them (if you copy paste somewhere else you will still see them)

      I am listening on org.eclipse.swt.custom.LineStyleListener (http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fcustom%2FLineStyleListener.html)
      I have also tried TextChangeListener and ModifyListener and ExtendedModifyListener and ModifyEvent and ExtendedModifyEvent, and did some digging in the Eclipse code.
      They don’t seem to be called by the console…

      • decoder says:

        Really useful plugin Mihai. Congrats!
        Can you tell me if you see any solution filtering the ANSI codes when copying into other containers, e.g. sharing messages over email, chat etc?

  10. disznoperzselo says:

    It seems that ESC[1A is not supported. Why?

    • Mihai says:

      The plugin does not replace the standard Eclipse console, so it cannot completely “take over”
      The only thing I could do is ask Eclipse to call me for coloring, and that happens one line at the time.
      And can’t go back and change anything else.

      As a result it can’t support anything that makes changes outside the current line.
      So no cursor movements, no erase screen, scrolling, etc.

      ESC[1A means cursor up, so the anwer is “sorry, but no can do”

      I think that a way to support everything would be to have a full console replacement.
      But I think that would mean that applications would have to explicitely write to that console, instead of being agnosting and using standard output / error, which go to the standard Eclipse one.
      Or I would have to do some “big time Eclipse surgery” to force the use of my console for strout / sterr.

      Mihai

    • Periata says:

      There’s a VT100 terminal emulator plugin that might do what you’re looking for at https://marketplace.eclipse.org/content/tm-terminal, although unfortunately it doesn’t seem to be able to integrate well enough that you can easily run your applications using it…

  11. Luli says:

    I was looking for a solution to have some kind of colored console output for a personal project with a PiTFT (http://www.adafruit.com/product/1601). And to have the ability to test it in Eclipse on a regular pc (so I don’t have to copy all the time the code to the PI).
    Works great.
    Thanks.

  12. Greg Bishop says:

    Where is the test code you show at the top of this page?

    -G

  13. Greg Bishop says:

    Does work. Very nice. Maybe clear screen should scrunch the text since the last clear up into an expandable section. Very large console logs do cause an out of memory issue (w/ unlimited log length).

    • Mihai says:

      Maybe clear screen should scrunch the text since the last clear up into an expandable section.

      I am using the standard Eclipse console, I don’t replace it.
      And the stuff that I am hooking does not give me access to the whole console.
      Only the current line to be rendered, before rendering. All I can do is apply attributes.
      No text changes, and nothing beyond the current line.
      There might be a hook that gives me better control, but that is a bigger project :-)

      About performance: seems to be mostly determined by the complexity of the console rendering colored text.
      I could not detect any indication that my code is the trouble…
      Big logs are slow and eat a lot of memory with and without my plugin.
      But adding color might make it worse, true…

  14. kodinos says:

    Hi Mihai,

    Thanks for all the useful tips and plugins.

    I just installed the ANSI Console plugin on top of a fresh Eclipse installation. The only other modification I have made really is the Dark Eclipse you provided (+ the dark console tweak). No other console-related apps are installed.

    The thing is, I cannot make the ANSI Console plugin work. Any ideas what I need to look for?

    Thanks

    • kodinos says:

      Please disregard my question; the source of the problem was not the plugin after all!

  15. Andrew Eidsness says:

    This is great, I really like this plugin.

    If you’re looking for feature suggestions … :-) … Then I think it would be nice if the icon in the view toolbar re-evaluated the current content. I’m not even sure if the Console view allows that, but it sure would be handy.

    • Mihai says:

      Thank you for the suggestion.
      But I am not sure I understand what do you mean by “re-evaluated the current content”.

  16. Roy Paterson says:

    Just installed it – works a treat! Thank you!

Leave a comment to Mihai