me.tongfei.progressbar.ConsoleProgressBarConsumer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of progressbar Show documentation
Show all versions of progressbar Show documentation
A terminal-based progress bar for JVM
The newest version!
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) {
out.print(CARRIAGE_RETURN + StringDisplayUtils.trimDisplayLength(str, getMaxRenderedLength()));
}
@Override
public void close() {
out.println();
out.flush();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy