Skip to content

Commit

Permalink
A Few Contributions
Browse files Browse the repository at this point in the history
- Fixing issue where `isSpeedShown` is true, but a null `speedFormat` is passed.
- Adding the option to continuously update according to the `updateIntervalMillis` instead of waiting for changes. This is good for long-running processes.
  • Loading branch information
bgriner committed Dec 1, 2021
1 parent e252b4a commit 7c704d8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

* `0.9.3`:
- New functionalities:
- Adding `continuousUpdate` boolean parameter to various constructors and the `ProgressUpdateAction` so that long-running processes don't take forever to print something.
- Bugfixes:
- Using a default `DecimalFormat` object if `isSpeedShown` is true as it will otherwise throw a `NullPointerException` during rendering.
* `0.9.2`:
- New functionalities:
- Supports for wrapping around `java.io.Reader`s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected DefaultProgressBarRenderer(
this.unitName = unitName;
this.unitSize = unitSize;
this.isSpeedShown = isSpeedShown;
this.speedFormat = speedFormat;
this.speedFormat = isSpeedShown && speedFormat == null ? new DecimalFormat() : speedFormat;
this.speedUnit = speedUnit;
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/me/tongfei/progressbar/ProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ProgressBar implements AutoCloseable {
* @param initialMax Initial maximum value
*/
public ProgressBar(String task, long initialMax) {
this(task, initialMax, 1000, System.err, ProgressBarStyle.COLORFUL_UNICODE_BLOCK, "", 1, false, null, ChronoUnit.SECONDS, 0L, Duration.ZERO);
this(task, initialMax, 1000, false, System.err, ProgressBarStyle.COLORFUL_UNICODE_BLOCK, "", 1, false, null, ChronoUnit.SECONDS, 0L, Duration.ZERO);
}

/**
Expand All @@ -45,6 +45,7 @@ public ProgressBar(String task, long initialMax) {
* @param task Task name
* @param initialMax Initial maximum value
* @param updateIntervalMillis Update interval (default value 1000 ms)
* @param continuousUpdate Rerender every time the update interval happens regardless of progress count.
* @param style Output style (default value ProgressBarStyle.UNICODE_BLOCK)
* @param showSpeed Should the calculated speed be displayed
* @param speedFormat Speed number format
Expand All @@ -53,6 +54,7 @@ public ProgressBar(
String task,
long initialMax,
int updateIntervalMillis,
boolean continuousUpdate,
PrintStream os,
ProgressBarStyle style,
String unitName,
Expand All @@ -63,7 +65,7 @@ public ProgressBar(
long processed,
Duration elapsed
) {
this(task, initialMax, updateIntervalMillis, processed, elapsed,
this(task, initialMax, updateIntervalMillis, continuousUpdate, processed, elapsed,
new DefaultProgressBarRenderer(style, unitName, unitSize, showSpeed, speedFormat, speedUnit),
createConsoleConsumer(os)
);
Expand All @@ -75,6 +77,7 @@ public ProgressBar(
* @param task Task name
* @param initialMax Initial maximum value
* @param updateIntervalMillis Update time interval (default value 1000ms)
* @param continuousUpdate Rerender every time the update interval happens regardless of progress count.
* @param processed Initial completed process value
* @param elapsed Initial elapsedBeforeStart second before
* @param renderer Progress bar renderer
Expand All @@ -84,13 +87,14 @@ public ProgressBar(
String task,
long initialMax,
int updateIntervalMillis,
boolean continuousUpdate,
long processed,
Duration elapsed,
ProgressBarRenderer renderer,
ProgressBarConsumer consumer
) {
this.progress = new ProgressState(task, initialMax, processed, elapsed);
this.action = new ProgressUpdateAction(progress, renderer, consumer);
this.action = new ProgressUpdateAction(progress, renderer, consumer, continuousUpdate);
scheduledTask = Util.executor.scheduleAtFixedRate(
action, 0, updateIntervalMillis, TimeUnit.MILLISECONDS
);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/tongfei/progressbar/ProgressBarBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ProgressBarBuilder {
private String task = "";
private long initialMax = -1;
private int updateIntervalMillis = 1000;
private boolean continuousUpdate = false;
private ProgressBarStyle style = ProgressBarStyle.COLORFUL_UNICODE_BLOCK;
private ProgressBarConsumer consumer = null;
private String unitName = "";
Expand Down Expand Up @@ -51,6 +52,11 @@ public ProgressBarBuilder setUpdateIntervalMillis(int updateIntervalMillis) {
return this;
}

public ProgressBarBuilder continuousUpdate(boolean continuousUpdate) {
this.continuousUpdate = continuousUpdate;
return this;
}

public ProgressBarBuilder setConsumer(ProgressBarConsumer consumer) {
this.consumer = consumer;
return this;
Expand Down Expand Up @@ -98,6 +104,7 @@ public ProgressBar build() {
task,
initialMax,
updateIntervalMillis,
continuousUpdate,
processed,
elapsed,
new DefaultProgressBarRenderer(style, unitName, unitSize, showSpeed, speedFormat, speedUnit),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ class ProgressUpdateAction implements Runnable {
private ProgressBarRenderer renderer;
private ProgressBarConsumer consumer;
private long last;
private boolean continuousUpdate;

ProgressUpdateAction(
ProgressState progress,
ProgressBarRenderer renderer,
ProgressBarConsumer consumer
ProgressBarConsumer consumer,
boolean continuousUpdate
) {
this.progress = progress;
this.renderer = renderer;
this.consumer = consumer;
this.continuousUpdate = continuousUpdate;
this.last = progress.start;
}

private void refresh() {
if (progress.current > last) {
if (progress.current > last || continuousUpdate) {
String rendered = renderer.render(progress, consumer.getMaxRenderedLength());
consumer.accept(rendered);
last = progress.current;
Expand Down

0 comments on commit 7c704d8

Please sign in to comment.