Skip to content

Commit 5096f69

Browse files
author
Nathan Merrill
committed
Added remaining duration to TextUI
1 parent 36f98a4 commit 5096f69

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/main/java/com/nmerrill/kothcomm/ui/text/TextUI.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
import org.eclipse.collections.impl.factory.Lists;
77

88
import java.io.PrintStream;
9+
import java.time.Duration;
10+
import java.time.Instant;
911
import java.util.Arrays;
1012

13+
import static java.time.temporal.ChronoUnit.HOURS;
14+
import static java.time.temporal.ChronoUnit.MINUTES;
15+
1116
public class TextUI {
1217
public final PrintStream out;
1318
private boolean showScores;
19+
private Instant start;
1420
public TextUI(PrintStream out){
1521
this.out = out;
1622
showScores = true;
@@ -55,7 +61,7 @@ public <T> void printScoreboard(Scoreboard<T> scoreboard, TableBuilder builder)
5561
out.println(builder.display(table));
5662
}
5763

58-
public void printScoreboard(Scoreboard scoreboard){
64+
public void printScoreboard(Scoreboard<?> scoreboard){
5965
TableBuilder builder = new TableBuilder();
6066
builder.rightAlign(0);
6167
builder.rightAlign(2);
@@ -64,16 +70,33 @@ public void printScoreboard(Scoreboard scoreboard){
6470
printScoreboard(scoreboard, builder);
6571
}
6672

73+
private Duration timeRemaining(int current, int max){
74+
if (start == null){
75+
start = Instant.now();
76+
return Duration.ofMillis(0);
77+
}
78+
Instant currentTime = Instant.now();
79+
Duration duration = Duration.between(start, currentTime);
80+
return duration.dividedBy(current).multipliedBy(max-current);
81+
}
82+
6783
public void printProgress(int current, int max){
6884
int percent = current * 100/max;
85+
Duration remaining = timeRemaining(current, max);
86+
long hours = remaining.toHours();
87+
remaining = remaining.minus(hours, HOURS);
88+
long minutes = remaining.toMinutes();
89+
remaining = remaining.minus(minutes, MINUTES);
90+
long seconds = remaining.getSeconds();
6991
out.print(
7092
"\r" +
7193
String.format(" %d%% [", percent) +
7294
repeated('=', percent) +
7395
'>' +
7496
repeated(' ', 100 - percent) +
7597
"] " +
76-
current + '/' + max
98+
current + '/' + max +
99+
" " + hours + "h " + minutes + "m " + seconds + "s"
77100
);
78101
}
79102

0 commit comments

Comments
 (0)