fusesource / jansi

Jansi is a small java library that allows you to use ANSI escape sequences to format your console output which works even on windows.
http://fusesource.github.io/jansi/
Apache License 2.0
1.11k stars 140 forks source link

ANSI output stripping does not work if TERM is xterm #83

Closed michael-o closed 7 years ago

michael-o commented 7 years ago

Just debugged the following:

$ ~/apache-maven-3.5.0/bin/mvnDebug  | less

I still see colors eventhough isatty(fileno) says 0 for stdout.

I have:

$ echo $TERM
xterm

So the following line

if (!isXterm() && !forceColored && rc == 0)

kills this check on non-Cygwin.

There must be a better way for Cygwin since Unix is broken.

Any thoughts?

gnodet commented 7 years ago

Can you explain what your test is and which line you're pointing at ?

michael-o commented 7 years ago

Yes, I wanted to test your libjansi.so for FreeBSD along with Maven 3.5.0. Started mvnDebug on that machine and wanted to see how Jansi behaves when I redirect stdout to a file. It should remove ANSI escapes. isatty does return 0, so dection is working, but the xterm check is killing it.

The shared library is loaded successfully.

The spurious line is https://github.com/fusesource/jansi/blob/master/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java#L122

Using $ TERM=ansi ~/apache-maven-3.5.0/bin/mvnDebug | less works though.

I can create a sample project if you want, isolated from Maven usecase.

gnodet commented 7 years ago

I'm not sure why there is the XTERM check, but imho the behavior should be similar to commands such as ls, i.e. a flag which can have the values: always, auto, never. I think the goal was to let color go through when it's supported. For example, on osx, the less commands handles colors nicely and less -r works well on FreeBSD too.

gnodet commented 7 years ago

We already have the jansi.strip and jansi.force properties, so I think we're covered. We should now be able to use

MAVEN_OPTS=-Djansi.force=true ~/apache-maven-3.5.0/bin/mvn | less -r
michael-o commented 7 years ago

I am aware of that and this actually what we want to achieve in Maven. See here. We wan't to mimic ls(1) or grep(1) behavior. As for the force. We wan't actually abstract from Jansi. People shouldn't really know that this is a Jansi task but they simply rely Maven to do the right thing.