mihnita / ansi-econsole

Eclipse plugin that understands ANSI escape sequences to color the Eclipse console output.
http://www.mihai-nita.net/java/
Other
91 stars 25 forks source link

No colors shown with CDT Makefile project that uses ANSI colors during it's build process #38

Closed TheChilly73 closed 5 years ago

TheChilly73 commented 5 years ago

The attached file contains a trivial Makefile project that when I build it doesn't show any coloring. It should show the word 'Green' in green color. It does this when I run 'make' in a Windows CMD window or a Git Bash, but not in the Eclipse Console (of course with this plugin installed).

If I create a new project in Eclipse (File -> New -> Makefile project with existing code), and then build it, I see the text ‘Green’, but it’s not colored at all… I hope you can reproduce this, and then maybe help with making it work 😉

Thanks,

TheChilly

Color.zip

mihnita commented 5 years ago

The very likely cause is that most Linux applications that output colors try to detect if the output is a real terminal, or a redirect (so that they don't output color escapes when you do something like foo > errors.log) And the Eclipse console is detected as a redirect (nothing to do with my plugin)

You can check if that is the case by disabling my plugin (the red A inside a blue C icon, top-right of the console) If you see things like ←[1;31m (different digits for different colors) then the output is colored, but my plugin is at fault. (the "arrow" is the escape, and might look different, like a rectangle, or rectangle with ?, depends on OS and installed fonts)

But if not, then you need to figure out how to force your tools to output colors even if the output is redirected.

For instance gcc can be "forced" to always output colors using -fdiagnostics-color=always https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Language-Independent-Options.html

For grep you do --color=always http://linuxcommand.org/lc3_man_pages/grep1.html

Unfortunately each tool will have its own flag, there is no standard :-(

But basically if you can do

   make >stdout.log 2>stderr.log
   cat stdout.log
   cat stderr.log

and see colors, then it means that you managed to override the tools to output color when redirected. At that point the Eclipse console would also show colors.

If you want to be able to redirect the output to files without colors, then things get messier. You would have to change your Makefile to force the color flags only when a special parameters is passed to it, or an environment variable is set. And then pass that parameter (or set that variable) when you run make in Eclipse.

mihnita commented 5 years ago

For the project attached, the problem is what I suspected: the python script uses colorama, and colorama does two things:

See here https://pypi.org/project/colorama/ And in the code here https://github.com/tartley/colorama/blob/master/colorama/ansitowin32.py you can see the checks for self.stream.isatty() (to see if this is a real terminal)

So the "trick" is to force colorama to always output escape sequences, not to interpret them (like it normally does on Windows), and not strip them (like it normally does in a non-tty)

And the "trick" is simple: call colorama.init(strip=False) instead of just colorama.init()

mihnita commented 5 years ago

Extras:

You might have some problems with strip=False in a real Windows console now. That's because Windows did not support ANSI escapes "since forever", It does in Windows 10, and there are some "tricks" to support them below 10 (ansicon, or alternate terminals)

But it it probably easier to just let colorama do its "magic". So you can do something like this:

if os.environ.get('FORCE_ANSI_ESCAPES'):
    print('FORCE_ANSI_ESCAPES')
    colorama.init(strip=False)
else:
    print('Regular color handling terminal')
    colorama.init()

And you define FORCE_ANSI_ESCAPES in the Eclipse runner (menu Run -- Run Configurations -- your project (Color?) -- Environment tab -- New button, add FORCE_ANSI_ESCAPES name and true value.

mihnita commented 5 years ago

Extras 2:

The color.py script uses colorama.Style.BRIGHT, but in reality colorama outputs \e[1m (using \e for escape)

That is often "bold", not "bright", depending on the terminal: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters

So \e[1m\e[32m (what colorama outputs) means either bright-green, or bold-green "Real" bright would be \e[92m

Solution 1: You can force the output of 92 with

print(colorama.Fore.LIGHTGREEN_EX + "Green" + colorama.Style.RESET_ALL)

Solution 2: You can tell my plugin to interpret things the way Windows does: Window menu -- Preferences -- Ansi Console -- Use Windows color mapping (bold => intense, italic => reverse)

mihnita commented 5 years ago

Please close if this solves your problem :-)

TheChilly73 commented 5 years ago

Thanks a lot for the extensive information! This clarifies everything. I'll close the issue.