Skip to content

Commit d6fb038

Browse files
committed
[O] PBar: Better access
1 parent 24e2b87 commit d6fb038

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

src/main/java/org/hydev/mcpm/client/interaction/ProgressBar.java

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,41 +47,18 @@ public ProgressBar(ProgressBarTheme theme)
4747
public ProgressRow appendBar(ProgressRow bar)
4848
{
4949
this.activeBars.add(bar);
50-
out.println("");
50+
bar.setPb(this);
51+
52+
out.println();
5153
update();
5254
return bar;
5355
}
5456

55-
private void update()
57+
protected void update()
5658
{
5759
// Roll back to the first line
5860
cu.curUp(activeBars.size());
5961
activeBars.forEach(bar -> out.println(bar.fmt(theme, cols)));
60-
activeBars.stream().toList().stream().filter(it -> it.completed >= it.total).forEach(this::finishBar);
61-
}
62-
63-
/**
64-
* Increase progress
65-
*
66-
* @param bar Progress row
67-
* @param incr Increase amount
68-
*/
69-
public void increase(ProgressRow bar, long incr)
70-
{
71-
bar.completed += incr;
72-
update();
73-
}
74-
75-
/**
76-
* Set progress
77-
*
78-
* @param bar Progres bar
79-
* @param completed Completed amount
80-
*/
81-
public void set(ProgressRow bar, long completed)
82-
{
83-
bar.completed = completed;
84-
update();
8562
}
8663

8764
/**
@@ -116,15 +93,15 @@ public static void main(String[] args)
11693
//var r = b.appendBar(new ProgressRow(1000, "it"));
11794
//for (int i = 0; i < 1000; i++)
11895
//{
119-
// b.increase(r, 1);
96+
// r.increase(1);
12097
// safeSleep(5);
12198
//}
12299

123100
var all = new ArrayList<ProgressRow>();
124101
for (int i = 0; i < 1300; i++)
125102
{
126103
if (i < 1000 && i % 100 == 0) all.add(b.appendBar(new ProgressRow(300, "it")));
127-
all.forEach(a -> b.increase(a, 1));
104+
all.forEach(a -> a.increase(1));
128105
safeSleep(3);
129106
}
130107

src/main/java/org/hydev/mcpm/client/interaction/ProgressRow.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@
1212
*/
1313
public class ProgressRow
1414
{
15-
protected long total;
16-
protected long completed;
17-
protected String unit;
15+
private final long total;
16+
private long completed;
17+
private final String unit;
18+
19+
private long startTime;
20+
private ProgressBar pb;
1821

1922
public ProgressRow(long total, String unit)
2023
{
2124
this.total = total;
2225
this.completed = 0;
23-
this.unit = unit;
2426

2527
// Add leading space
26-
if (!unit.isBlank() && !unit.startsWith(" ")) this.unit = " " + unit;
28+
this.unit = (!unit.isBlank() && !unit.startsWith(" ")) ? " " + unit : unit;
29+
30+
// Record start time for speed estimation
31+
this.startTime = System.nanoTime();
2732
}
2833

2934
public ProgressRow(long total)
@@ -42,7 +47,7 @@ public String fmt(ProgressBarTheme theme, int cols)
4247
{
4348
double p = 100d * completed / total;
4449
var placeholder = "PLACEHOLDER_BAR";
45-
var t = format("%s%s%s %.0f%% %5d/%-5d%s", theme.prefix(), placeholder, theme.suffix(), p, completed, total, unit);
50+
var t = format("%s%s%s %3.0f%% %5d/%-5d%s", theme.prefix(), placeholder, theme.suffix(), p, completed, total, unit);
4651

4752
// Add progress bar length
4853
var len = cols - t.length() + placeholder.length();
@@ -53,4 +58,33 @@ public String fmt(ProgressBarTheme theme, int cols)
5358

5459
return t.replaceFirst(placeholder, bar);
5560
}
61+
62+
public void setPb(ProgressBar pb)
63+
{
64+
this.pb = pb;
65+
}
66+
67+
/**
68+
* Increase progress
69+
*
70+
* @param incr Increase amount
71+
*/
72+
public void increase(long incr)
73+
{
74+
this.completed += incr;
75+
pb.update();
76+
if (completed >= total) pb.finishBar(this);
77+
}
78+
79+
/**
80+
* Set progress
81+
*
82+
* @param completed Completed amount
83+
*/
84+
public void set(long completed)
85+
{
86+
this.completed = completed;
87+
pb.update();
88+
if (completed >= total) pb.finishBar(this);
89+
}
5690
}

0 commit comments

Comments
 (0)