Skip to content

Commit

Permalink
fix #117, force refresh on first run; dependency bump
Browse files Browse the repository at this point in the history
  • Loading branch information
ctongfei committed Jan 2, 2022
1 parent 1f0f690 commit 4ebf879
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
12 changes: 8 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<id>heroesleo65</id>
<name>Ilya Korobtsev</name>
</developer>
<developer>
<id>kmtong</id>
<name>KM Tong</name>
</developer>
</developers>

<dependencies>
Expand All @@ -122,28 +126,28 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<version>5.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<version>5.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.31</version>
<version>1.7.32</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.31</version>
<version>1.7.32</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/me/tongfei/progressbar/ProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public void close() {
scheduledTask.cancel(false);
progress.kill();
try {
action.setForceRefresh();
Util.executor.schedule(action, 0, TimeUnit.NANOSECONDS).get();
} catch (InterruptedException | ExecutionException e) { /* NOOP */ }
}
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/me/tongfei/progressbar/ProgressUpdateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ProgressUpdateAction implements Runnable {
ProgressState progress;
private ProgressBarRenderer renderer;
private ProgressBarConsumer consumer;
private long last;
private boolean forceRefresh = false;
volatile private long last;
volatile private boolean first;

ProgressUpdateAction(
ProgressState progress,
Expand All @@ -20,26 +20,32 @@ class ProgressUpdateAction implements Runnable {
this.renderer = renderer;
this.consumer = consumer;
this.last = progress.start;
this.first = true;
}

private void refresh() {
if (forceRefresh || progress.current > last) {
String rendered = renderer.render(progress, consumer.getMaxRenderedLength());
consumer.accept(rendered);
last = progress.current;
}
if (progress.current > last) forceRefresh();
// else do nothing: only print when actual progress is made (#91).
}

public void setForceRefresh() {
forceRefresh = true;
public void forceRefresh() {
String rendered = renderer.render(progress, consumer.getMaxRenderedLength());
consumer.accept(rendered);
last = progress.current;
}

public void run() {
if (!progress.paused) refresh();
if (!progress.alive) {
consumer.close();
TerminalUtils.closeTerminal();
if (first) {
forceRefresh();
first = false;
}
else {
if (!progress.paused) refresh();
if (!progress.alive) {
forceRefresh();
consumer.close();
TerminalUtils.closeTerminal();
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/me/tongfei/progressbar/Issue117Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.tongfei.progressbar;

import org.junit.jupiter.api.Test;

public class Issue117Test {

@Test
void testImmediateStart() throws InterruptedException {
try (ProgressBar pb = new ProgressBar("Test", 2)) {
for (int i = 0; i < 2; i++) {
Thread.sleep(1000);
pb.step();
}
}
}

}

0 comments on commit 4ebf879

Please sign in to comment.