6
6
import org .eclipse .collections .impl .factory .Lists ;
7
7
8
8
import java .io .PrintStream ;
9
+ import java .time .Duration ;
10
+ import java .time .Instant ;
9
11
import java .util .Arrays ;
10
12
13
+ import static java .time .temporal .ChronoUnit .HOURS ;
14
+ import static java .time .temporal .ChronoUnit .MINUTES ;
15
+
11
16
public class TextUI {
12
17
public final PrintStream out ;
13
18
private boolean showScores ;
19
+ private Instant start ;
14
20
public TextUI (PrintStream out ){
15
21
this .out = out ;
16
22
showScores = true ;
@@ -55,7 +61,7 @@ public <T> void printScoreboard(Scoreboard<T> scoreboard, TableBuilder builder)
55
61
out .println (builder .display (table ));
56
62
}
57
63
58
- public void printScoreboard (Scoreboard scoreboard ){
64
+ public void printScoreboard (Scoreboard <?> scoreboard ){
59
65
TableBuilder builder = new TableBuilder ();
60
66
builder .rightAlign (0 );
61
67
builder .rightAlign (2 );
@@ -64,16 +70,33 @@ public void printScoreboard(Scoreboard scoreboard){
64
70
printScoreboard (scoreboard , builder );
65
71
}
66
72
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
+
67
83
public void printProgress (int current , int max ){
68
84
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 ();
69
91
out .print (
70
92
"\r " +
71
93
String .format (" %d%% [" , percent ) +
72
94
repeated ('=' , percent ) +
73
95
'>' +
74
96
repeated (' ' , 100 - percent ) +
75
97
"] " +
76
- current + '/' + max
98
+ current + '/' + max +
99
+ " " + hours + "h " + minutes + "m " + seconds + "s"
77
100
);
78
101
}
79
102
0 commit comments