Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No colors if stdout is redirected #124

Closed
klenium opened this issue Apr 20, 2018 · 9 comments
Closed

No colors if stdout is redirected #124

klenium opened this issue Apr 20, 2018 · 9 comments
Assignees

Comments

@klenium
Copy link

klenium commented Apr 20, 2018

I've write a program that uses both stdout (for "graphical" view) and stderr (for logging), they both have colorized texts. I'm using Win10.
Without any redirect, or if I redirect only stderr, JAnsi works fine.
But if I redirect stdout, the log won't be printed with colors.
This is strange... I think JAnsi uses only stdout for detecting the redirection, but what about stderr?

@klenium
Copy link
Author

klenium commented Apr 20, 2018

Sorry, I see that it has already been reported: #18
But why is it closed? I'm using the latest (1.17) JAnsi version.

@klenium
Copy link
Author

klenium commented Apr 20, 2018

Even stranger: if stdout is redirected, then everything that's printed using JAnsi after calling AnsiConsole.systemUninstall, it will have colors! If stdout isn't redirected, I see random caracters after the uninstall, just as how it should work.

@hboutemy
Copy link
Collaborator

System.out and System.err are wrapped independently: see https://github.com/fusesource/jansi/blob/master/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java#L61
I'm surprised that redirecting stdout affects stderr
You can use {{java -jar jansi*.jar}} to have a diagnostic: please check

@klenium
Copy link
Author

klenium commented Apr 22, 2018

It works fine on Ubuntu.

For windows 10:
powershell-diagnostics-normal
powershell-diagnostics-redirect
cmd-diagnostics-normal
cmd-diagnostics-redirect
custom-test

public static void main(String[] args) {
    AnsiConsole.systemInstall();
    print(System.out, "Lorem ipsum");
    print(System.err, "dolor sit amet");
    AnsiConsole.systemUninstall();
    print(System.out, "consectetur adipiscing elit");
    print(System.err, "Donec lectus lectus");
}
private static void print(PrintStream stream, String text) {
    int half = text.length() / 2;
    stream.print(text.substring(0, half));
    stream.println(Ansi.ansi().fg(Ansi.Color.GREEN).a(text.substring(half)).reset());
}

@hboutemy
Copy link
Collaborator

ok, I can reproduce your case with Jansi diagnostic: strange
I had a deeper look: I suppose I should add a new "debug" flag to dump exceptions instead of silently going to fallback, since it's not easy to diagnose
reading comment on https://github.com/fusesource/jansi/blob/master/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java#L127 and https://github.com/fusesource/jansi/blob/master/jansi/src/main/java/org/fusesource/jansi/WindowsAnsiPrintStream.java#L57 I fear that currently the native code is looking for stdout only, not stderr
we should probably pass some arguments to adapt WindowsAnsiPrintStream to the effective handle (out or err).

need to make some test: help welcome if you can try in parallel

@hboutemy
Copy link
Collaborator

I finally managed to fix the issue: please test and confirm

@klenium
Copy link
Author

klenium commented May 1, 2018

The first is resolved so stderr has colors if stdout is redirected.

But the uninstall-thing is still wrong. See the last image.

@hboutemy
Copy link
Collaborator

hboutemy commented May 1, 2018

ok, uninstall is something else: please open another issue

@hboutemy
Copy link
Collaborator

closing this issue: please open another one for the uninstall case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants