JColor: ColoredPrinter.print does not behave like System.out.print

The Javadoc says ColoredPrinter.print is a

Usual System.out.print

This is not the case for me. Calling it does not immediately print my string; System.out.print correctly does in its place. Instead, my string appears with the next output made with ColoredPrinter.print or ColoredPrinter.println (or their equivalents from System.out).
For now I am using regular System.out in its place - I’d rather use ColoredPrinter as in the rest of my code though.

Is this a known and fixable error?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (13 by maintainers)

Most upvoted comments

I’ll try that when I get the chance. It likely won’t be for a day or two. Your assumption is correct. IntelliJ outputs its default white text and is not affected by the ANSI codes that should be included.

Hi @dialex, I’m having the same issue as @DesertCookie with a little twist (you’ll see why soon). I’m on Windows 10 and using version 2.0.3.1 of your library. This is awesome, by the way. I appreciate how much effort was put into this library and am grateful for its existence.

Right now, I have a method info(String message) that is defined below. Don’t worry about the if(enabled) checks. It’s for the very simple logger that I made.

public static void info(String message) {
		if (enabled) {
			cp.clear();
			cp.println(message, Ansi.Attribute.NONE, Ansi.FColor.CYAN, Ansi.BColor.BLACK);
			cp.clear();
		}
	}

This function works perfectly fine. However, if I want to print using this formatter but not have a newline at the end of the message, I have a method infoNoLine(String message) defined as follows:

public static void infoNoLine(String message) {
		if (enabled) {
			cp.clear();
			cp.print("message", Ansi.Attribute.NONE, Ansi.FColor.CYAN, Ansi.BColor.BLACK);
			cp.clear();
		}
	}

Here’s where things get interesting. This works perfectly fine for me in Windows’ cmd.exe. However, it does not work for me inside of IntelliJ’s console window. Instead, nothing is printed at all. I currently have a temporary fix that seems to be doing the job. It works the same as infoNoLine(String message) but it works both in IntelliJ and in cmd.exe.

public static void infoNoLineTempFix(String message) {
		if (enabled) {
			cp.clear();
			//set formatter but don't print anything
			cp.print("", Ansi.Attribute.NONE, Ansi.FColor.CYAN, Ansi.BColor.BLACK);
			//use System.out.print to print the actual message
			System.out.print(message);
			//clear the formatting for whatever might use System.out next
			cp.clear();
		}
	}

Let me know if I can provide any other details that might be helpful for fleshing out this issue.

No need to be sorry, it’s easy to forget, especially when I don’t provide a template for bug reports (but now I improved it, thanks to you 😉)

Regarding your issue, I’m having trouble understanding what your problem is. Here’s my current understanding:

You expected ColoredPrinter.print(msg) to use System.out, because that’s what the Javadoc says. But it didn’t. You’re right, the implementation doesn’t match the doc.

  • The UnixColoredPrinter uses System.out to print (code).
  • The WindowsColoredPrinter, your case, uses AnsiConsole from Jansi to print (code).

From Jansi’s official doc:

If the standard out natively supports ANSI escape codes, then this just returns System.out, otherwise it will provide an ANSI aware PrintStream which strips out the ANSI escape sequences or which implement the escape sequences.

Is that a problem to you? PrintStream should work just like System.out – check this SO question.

I can update the Javadoc, besides that I’m not sure what I can do to help you.