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.12k stars 139 forks source link

flush() methods are not correctly transmitted to underlying terminal: 1 flush FOREACH char!!! BUT no autoFlush on newLine() #143

Closed Arnaud-Nauwynck closed 3 years ago

Arnaud-Nauwynck commented 5 years ago

Currently, there is 1 flush() system call FOREACH char !!! This cause ~10 times performance decrease

Notice that fixing these extra flushes would cause another bug, that there is a "missing" flush() after each newLine() ( such as after any println() method overload ) .. This is why "it works, but slooooowly"

The private parameter of "autoFlush" in PrintSteam implementation of System.out / System.err is not correctly re-transmitted to wrapper constructor of AnsiPrintStream

More-over, the semantic of java.io.PrintStream println() calling newLine() ... then calling flush() should be preserved

This should be something like

private void newLine() {
        write(NEWLINE);
        if (autoFlush) {
            try {
                out.flush();
                } catch (InterruptedIOException x) {
                        Thread.currentThread().interrupt();
                } catch(IOException ex) {
                // super.trouble = true; // ??
            }
        }
    }

instead of

private void newLine() {
        write(NEWLINE);
    }

to be similar to java.io.PrintSteam:

    private void newLine() {
        try {
            synchronized (this) {
                ensureOpen();
                textOut.newLine();
                textOut.flushBuffer();
                charOut.flushBuffer();
                if (autoFlush)
                    out.flush();
            }
        }
        catch (InterruptedIOException x) {
            Thread.currentThread().interrupt();
        }
        catch (IOException x) {
            trouble = true;
        }
    }

Maybe, the java.io.PrintStream class should not be extended at all, only delegating to a JAnsi Filtering java.io.OutputStream would be enough

gnodet commented 3 years ago

This has been fixed in 2.x.