Skip to content

Commit

Permalink
fixes #91: only update when progress is made
Browse files Browse the repository at this point in the history
  • Loading branch information
ctongfei committed Feb 26, 2021
1 parent b41637b commit f1bc9c0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions docs/builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ProgressBarBuilder pbb = new ProgressBarBuilder()
.setTaskName(<taskName name>)
.setUnit(<unit name>, <unit size>)
.setUpdateIntervalMillis(<update interval>)
.setMaxRenderedLength(<max rendered length in terminal>)
.showSpeed();
// or .showSpeed(new DecimalFormat("#.##")) to customize speed display

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ProgressUpdateAction implements Runnable {
ProgressState progress;
private ProgressBarRenderer renderer;
private ProgressBarConsumer consumer;
private long last;

ProgressUpdateAction(
ProgressState progress,
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/me/tongfei/progressbar/Slf4jTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f1bc9c0

Please sign in to comment.