package me.tongfei.progressbar;

import java.io.PrintStream;

import static me.tongfei.progressbar.TerminalUtils.CARRIAGE_RETURN;

/**
 * Progress bar consumer that prints the progress bar state to console.
 * By default {@link System#err} is used as {@link PrintStream}.
 *
 * @author Tongfei Chen
 * @author Alex Peelman
 */
public class ConsoleProgressBarConsumer implements ProgressBarConsumer {

    private static int consoleRightMargin = 1;
    int maxRenderedLength = -1;
    final PrintStream out;

    public ConsoleProgressBarConsumer(PrintStream out) {
        this.out = out;
    }

    public ConsoleProgressBarConsumer(PrintStream out, int maxRenderedLength) {
        this.maxRenderedLength = maxRenderedLength;
        this.out = out;
    }

    @Override
    public int getMaxRenderedLength() {
        if (maxRenderedLength <= 0)
            return TerminalUtils.getTerminalWidth() - consoleRightMargin;
        else return maxRenderedLength;
    }

    @Override
    public void accept(String str) {
        int displayLength = StringDisplayUtils.getStringDisplayLength(str);
        int acceptedLength = Math.min(displayLength, getMaxRenderedLength());
        out.print(CARRIAGE_RETURN + StringDisplayUtils.trimDisplayLength(str, acceptedLength));
    }

    @Override
    public void close() {
        out.println();
        out.flush();
    }
}

@@ -1017,162 +1017,8 @@
 ) %7B%0A
-        int displayLength = StringDisplayUtils.getStringDisplayLength(str);%0A        int acceptedLength = Math.min(displayLength, getMaxRenderedLength());%0A
     
@@ -1095,22 +1095,28 @@
 tr, 
-accept
+getMaxRender
 edLength
 ));%0A
@@ -1111,16 +1111,18 @@
 edLength
+()
 ));%0A    

Improve performance in ConsoleProgressBarConsumer.accept