From f1bc9c07df167722f1bacbda1e95cae998cad86f Mon Sep 17 00:00:00 2001 From: Tongfei Chen Date: Thu, 25 Feb 2021 22:56:31 -0500 Subject: [PATCH] fixes #91: only update when progress is made --- CHANGELOG.md | 3 +++ docs/builder.md | 1 + .../me/tongfei/progressbar/ProgressUpdateAction.java | 10 ++++++++-- src/test/java/me/tongfei/progressbar/Slf4jTest.java | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b06fe15..1f0f130 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog * `0.9.1`: + - Improvements: + - Only renders the progress bar when progress is made (#91). This improves the performance of progress bars. Thanks @elanzini ! + - Added `setMaxRenderedLength` method to builders (#71). Thanks @koppor ! - Misc: - Updates JUnit 4 to JUnit 5 (PR #93). Thanks @michaelsiepmann ! - Dependency version bump. diff --git a/docs/builder.md b/docs/builder.md index 2958f98..37f4922 100644 --- a/docs/builder.md +++ b/docs/builder.md @@ -11,6 +11,7 @@ ProgressBarBuilder pbb = new ProgressBarBuilder() .setTaskName() .setUnit(, ) .setUpdateIntervalMillis() + .setMaxRenderedLength() .showSpeed(); // or .showSpeed(new DecimalFormat("#.##")) to customize speed display diff --git a/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java b/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java index 9748886..ec71d74 100644 --- a/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java +++ b/src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java @@ -8,6 +8,7 @@ class ProgressUpdateAction implements Runnable { ProgressState progress; private ProgressBarRenderer renderer; private ProgressBarConsumer consumer; + private long last; ProgressUpdateAction( ProgressState progress, @@ -17,11 +18,16 @@ class ProgressUpdateAction implements Runnable { this.progress = progress; this.renderer = renderer; this.consumer = consumer; + this.last = progress.start; } private void refresh() { - String rendered = renderer.render(progress, consumer.getMaxRenderedLength()); - consumer.accept(rendered); + if (progress.current > last) { + String rendered = renderer.render(progress, consumer.getMaxRenderedLength()); + consumer.accept(rendered); + last = progress.current; + } + // else do nothing: only print when actual progress is made (#91). } public void run() { diff --git a/src/test/java/me/tongfei/progressbar/Slf4jTest.java b/src/test/java/me/tongfei/progressbar/Slf4jTest.java index d9c6207..5e9fd89 100644 --- a/src/test/java/me/tongfei/progressbar/Slf4jTest.java +++ b/src/test/java/me/tongfei/progressbar/Slf4jTest.java @@ -17,7 +17,7 @@ void printLoggerTest() throws InterruptedException { .setInitialMax(100) .setTaskName("log.test") .setConsumer(new DelegatingProgressBarConsumer(logger::info)) - .setUpdateIntervalMillis(100) + .setUpdateIntervalMillis(1) .build()) { for (int i = 0; i < 100; i++) { pb.step();