Skip to content

Commit f89e7c9

Browse files
committed
making ProgressState public with get accessors; credit contributors
1 parent 7d192c0 commit f89e7c9

File tree

9 files changed

+125
-42
lines changed

9 files changed

+125
-42
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
* `0.9.5`:
3+
- Bugfixes:
4+
- Fixed the problem of `ProgressState` not being public, thus making `setEtaFunction` useless (#147, PR #146). Thanks @deejgregor, @natanfudge !
25
* `0.9.4`:
36
- New functionalities:
47
- In `ProgressBarBuilder`s, one can now switch whether to show the remaining time, or to provide a custom

docs/builder.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ProgressBarBuilder pbb = new ProgressBarBuilder()
1414
.setMaxRenderedLength(<max rendered length in terminal>)
1515
.showSpeed();
1616
// or .showSpeed(new DecimalFormat("#.##")) to customize speed display
17+
.setEtaFunction(state -> ...)
1718
for (T x : ProgressBar.wrap(collection, pbb)) {
1819
...
1920
}

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@
121121
<developer>
122122
<id>gaoagong</id>
123123
</developer>
124+
<developer>
125+
<id>deejgregor</id>
126+
<name>DJ Gregor</name>
127+
</developer>
124128
</developers>
125129

126130
<dependencies>

src/main/java/me/tongfei/progressbar/DefaultProgressBarRenderer.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.time.Instant;
66
import java.time.temporal.ChronoUnit;
77
import java.util.Optional;
8-
import java.util.function.BiFunction;
8+
import java.util.function.Function;
99

1010
import static me.tongfei.progressbar.StringDisplayUtils.*;
1111

@@ -23,8 +23,8 @@ public class DefaultProgressBarRenderer implements ProgressBarRenderer {
2323
private boolean isSpeedShown;
2424
private DecimalFormat speedFormat;
2525
private ChronoUnit speedUnit;
26-
private boolean isETAShown;
27-
private BiFunction<ProgressState, Duration, Optional<Duration>> eta;
26+
private boolean isEtaShown;
27+
private Function<ProgressState, Optional<Duration>> eta;
2828

2929
protected DefaultProgressBarRenderer(
3030
ProgressBarStyle style,
@@ -33,16 +33,16 @@ protected DefaultProgressBarRenderer(
3333
boolean isSpeedShown,
3434
DecimalFormat speedFormat,
3535
ChronoUnit speedUnit,
36-
boolean isETAShown,
37-
BiFunction<ProgressState, Duration, Optional<Duration>> eta
36+
boolean isEtaShown,
37+
Function<ProgressState, Optional<Duration>> eta
3838
) {
3939
this.style = style;
4040
this.unitName = unitName;
4141
this.unitSize = unitSize;
4242
this.isSpeedShown = isSpeedShown;
4343
this.speedFormat = isSpeedShown && speedFormat == null ? new DecimalFormat() : speedFormat;
4444
this.speedUnit = speedUnit;
45-
this.isETAShown = isETAShown;
45+
this.isEtaShown = isEtaShown;
4646
this.eta = eta;
4747
}
4848

@@ -57,8 +57,8 @@ protected int progressFractionalPart(ProgressState progress, int length) {
5757
return (int) Math.floor(fraction);
5858
}
5959

60-
protected String etaString(ProgressState progress, Duration elapsed) {
61-
Optional<Duration> eta = this.eta.apply(progress, elapsed);
60+
protected String etaString(ProgressState progress) {
61+
Optional<Duration> eta = this.eta.apply(progress);
6262
if (eta.isPresent()) {
6363
return Util.formatDuration(eta.get());
6464
}
@@ -80,9 +80,9 @@ protected String ratio(ProgressState progress) {
8080
return Util.repeat(' ', m.length() - c.length()) + c + "/" + m + unitName;
8181
}
8282

83-
protected String speed(ProgressState progress, Duration elapsed) {
83+
protected String speed(ProgressState progress) {
8484
String suffix = "/s";
85-
double elapsedSeconds = elapsed.getSeconds();
85+
double elapsedSeconds = progress.getElapsedAfterStart().getSeconds();
8686
double elapsedInUnit = elapsedSeconds;
8787
if (null != speedUnit)
8888
switch (speedUnit) {
@@ -112,10 +112,7 @@ public String render(ProgressState progress, int maxLength) {
112112
return "";
113113
}
114114

115-
Instant currTime = Instant.now();
116-
Duration elapsed = Duration.between(progress.startInstant, currTime);
117-
118-
String prefix = progress.taskName + " " + percentage(progress) + " " + style.leftBracket;
115+
String prefix = progress.getTaskName() + " " + percentage(progress) + " " + style.leftBracket;
119116
int prefixLength = getStringDisplayLength(prefix);
120117

121118
if (prefixLength > maxLength) {
@@ -126,9 +123,11 @@ public String render(ProgressState progress, int maxLength) {
126123
// length of progress should be at least 1
127124
int maxSuffixLength = Math.max(maxLength - prefixLength - 1, 0);
128125

129-
String speedString = isSpeedShown ? speed(progress, elapsed) : "";
126+
String speedString = isSpeedShown ? speed(progress) : "";
130127
String suffix = style.rightBracket + " " + ratio(progress) + " ("
131-
+ Util.formatDuration(elapsed) + (isETAShown ? " / " + etaString(progress, elapsed) : "") + ") "
128+
+ Util.formatDuration(progress.getTotalElapsed())
129+
+ (isEtaShown ? " / " + etaString(progress) : "")
130+
+ ") "
132131
+ speedString + progress.extraMessage;
133132
int suffixLength = getStringDisplayLength(suffix);
134133
// trim excessive suffix

src/main/java/me/tongfei/progressbar/ProgressBar.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.*;
88
import java.text.DecimalFormat;
99
import java.time.Duration;
10+
import java.time.Instant;
1011
import java.time.temporal.ChronoUnit;
1112
import java.util.Arrays;
1213
import java.util.Iterator;
@@ -74,7 +75,7 @@ public ProgressBar(
7475
new DefaultProgressBarRenderer(
7576
style, unitName, unitSize,
7677
showSpeed, speedFormat, speedUnit,
77-
true, Util::linearETA
78+
true, Util::linearEta
7879
),
7980
createConsoleConsumer(os)
8081
);
@@ -214,6 +215,49 @@ public long getMax() {
214215
return progress.getMax();
215216
}
216217

218+
public long getStart() {
219+
return progress.getStart();
220+
}
221+
222+
/**
223+
* Returns the progress normalized to the interval [0, 1].
224+
*/
225+
public double getNormalizedProgress() {
226+
return progress.getNormalizedProgress();
227+
}
228+
229+
/**
230+
* Returns the instant when the progress bar started.
231+
* If a progress bar is resumed after a pause, it returns the instant when the progress was resumed.
232+
*/
233+
public Instant getStartInstant() {
234+
return progress.startInstant;
235+
}
236+
237+
/**
238+
* Returns the duration that this progress bar has been running before it was resumed.
239+
* If a progress bar starts afresh, it should return zero.
240+
*/
241+
public Duration getElapsedBeforeStart() {
242+
return progress.getElapsedBeforeStart();
243+
}
244+
245+
/**
246+
* Returns the duration that this progress bar has been running after it was resumed.
247+
* If a progress bar has not been paused before, it should return the total duration starting from creation.
248+
*/
249+
public Duration getElapsedAfterStart() {
250+
return progress.getElapsedAfterStart();
251+
}
252+
253+
/**
254+
* Returns the total duration that this progress bar has been running from start,
255+
* excluding the period when it has been paused.
256+
*/
257+
public Duration getTotalElapsed() {
258+
return progress.getTotalElapsed();
259+
}
260+
217261
/**
218262
* Returns the name of this task.
219263
*/
@@ -229,7 +273,7 @@ public String getExtraMessage() {
229273
}
230274

231275
/** Checks if the progress bar is indefinite, i.e., its maximum value is unknown. */
232-
public boolean IsIndefinite() {
276+
public boolean isIndefinite() {
233277
return progress.indefinite;
234278
}
235279

src/main/java/me/tongfei/progressbar/ProgressBarBuilder.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.time.Duration;
55
import java.time.temporal.ChronoUnit;
66
import java.util.Optional;
7-
import java.util.function.BiFunction;
7+
import java.util.function.Function;
88

99
/**
1010
* Builder class for {@link ProgressBar}s.
@@ -23,8 +23,8 @@ public class ProgressBarBuilder {
2323
private String unitName = "";
2424
private long unitSize = 1;
2525
private boolean showSpeed = false;
26-
private boolean hideETA = false;
27-
private BiFunction<ProgressState, Duration, Optional<Duration>> eta = Util::linearETA;
26+
private boolean hideEta = false;
27+
private Function<ProgressState, Optional<Duration>> eta = Util::linearEta;
2828
private DecimalFormat speedFormat;
2929
private ChronoUnit speedUnit = ChronoUnit.SECONDS;
3030
private long processed = 0;
@@ -93,13 +93,13 @@ public ProgressBarBuilder showSpeed(DecimalFormat speedFormat) {
9393
return this;
9494
}
9595

96-
public ProgressBarBuilder hideETA() {
97-
this.hideETA = true;
96+
public ProgressBarBuilder hideEta() {
97+
this.hideEta = true;
9898
return this;
9999
}
100100

101-
public ProgressBarBuilder setETAFunction(BiFunction<ProgressState, Duration, Optional<Duration>> eta) {
102-
this.hideETA = false;
101+
public ProgressBarBuilder setEtaFunction(Function<ProgressState, Optional<Duration>> eta) {
102+
this.hideEta = false;
103103
this.eta = eta;
104104
return this;
105105
}
@@ -132,7 +132,7 @@ public ProgressBar build() {
132132
new DefaultProgressBarRenderer(
133133
style, unitName, unitSize,
134134
showSpeed, speedFormat, speedUnit,
135-
!hideETA, eta),
135+
!hideEta, eta),
136136
consumer == null ? Util.createConsoleConsumer(maxRenderedLength) : consumer
137137
);
138138
}

src/main/java/me/tongfei/progressbar/ProgressState.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,70 @@ public class ProgressState {
2929

3030
ProgressState(String taskName, long initialMax, long startFrom, Duration elapsedBeforeStart) {
3131
this.taskName = taskName;
32-
this.max = initialMax;
3332
if (initialMax < 0) indefinite = true;
3433
this.start = startFrom;
3534
this.current = startFrom;
36-
this.elapsedBeforeStart = elapsedBeforeStart;
35+
3736
this.startInstant = Instant.now();
37+
this.elapsedBeforeStart = elapsedBeforeStart;
3838
}
3939

40-
String getTaskName() {
40+
public String getTaskName() {
4141
return taskName;
4242
}
4343

44-
synchronized String getExtraMessage() {
44+
public synchronized String getExtraMessage() {
4545
return extraMessage;
4646
}
4747

48-
synchronized long getCurrent() {
48+
public synchronized long getStart() {
49+
return start;
50+
}
51+
52+
public synchronized long getCurrent() {
4953
return current;
5054
}
5155

52-
synchronized long getMax() {
56+
public synchronized long getMax() {
5357
return max;
5458
}
5559

56-
// The progress, normalized to range [0, 1].
57-
synchronized double getNormalizedProgress() {
60+
public synchronized double getNormalizedProgress() {
5861
if (max <= 0) return 0.0;
5962
else if (current > max) return 1.0;
6063
else return ((double)current) / max;
6164
}
6265

66+
public synchronized Instant getStartInstant() {
67+
return startInstant;
68+
}
69+
70+
public synchronized Duration getElapsedBeforeStart() {
71+
return elapsedBeforeStart;
72+
}
73+
74+
public synchronized Duration getElapsedAfterStart() {
75+
return (startInstant == null)
76+
? Duration.ZERO
77+
: Duration.between(startInstant, Instant.now());
78+
}
79+
80+
public synchronized Duration getTotalElapsed() {
81+
return getElapsedBeforeStart().plus(getElapsedAfterStart());
82+
}
83+
84+
public synchronized boolean isIndefinite() {
85+
return indefinite;
86+
}
87+
88+
public synchronized boolean isAlive() {
89+
return alive;
90+
}
91+
92+
public synchronized boolean isPaused() {
93+
return paused;
94+
}
95+
6396
synchronized void setAsDefinite() {
6497
indefinite = false;
6598
}

src/main/java/me/tongfei/progressbar/Util.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
import java.io.*;
44
import java.time.Duration;
55
import java.util.Optional;
6-
import java.util.Set;
7-
import java.util.HashSet;
86
import java.util.Spliterator;
97
import java.util.concurrent.Executors;
108
import java.util.concurrent.ScheduledThreadPoolExecutor;
11-
import java.util.stream.Collectors;
12-
import java.util.stream.Stream;
139

1410

1511
class Util {
@@ -48,11 +44,13 @@ static String formatDuration(Duration d) {
4844
return String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, s % 60);
4945
}
5046

51-
static Optional<Duration> linearETA(ProgressState progress, Duration elapsed) {
52-
if (progress.max <= 0 || progress.indefinite) return Optional.empty();
53-
else if (progress.current - progress.start == 0) return Optional.empty();
47+
static Optional<Duration> linearEta(ProgressState progress) {
48+
if (progress.getMax() <= 0 || progress.isIndefinite()) return Optional.empty();
49+
else if (progress.getCurrent() - progress.getStart() == 0) return Optional.empty();
5450
else return Optional.of(
55-
elapsed.dividedBy(progress.current - progress.start).multipliedBy(progress.max - progress.current)
51+
progress.getElapsedAfterStart()
52+
.dividedBy(progress.getCurrent() - progress.getStart())
53+
.multipliedBy(progress.getMax() - progress.getCurrent())
5654
);
5755
}
5856

src/test/java/me/tongfei/progressbar/StreamSetupExtension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void setup() {
2626
// autoFlush = true for stdout/stderr streams
2727
System.setOut(new PrintStream(System.out, true));
2828
System.setErr(new PrintStream(System.err, true));
29+
System.out.println();
2930
}
3031

3132
@Override

0 commit comments

Comments
 (0)