Skip to content

Commit 24e2b87

Browse files
committed
[+] PBar: Multi-line progress bar
1 parent 81e540f commit 24e2b87

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

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

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public ProgressBar(ProgressBarTheme theme)
3636
this.cu = new ConsoleUtils(this.out);
3737
this.activeBars = new ArrayList<>();
3838
this.cols = AnsiConsole.getTerminalWidth();
39-
System.out.println(cols);
4039
}
4140

4241
/**
@@ -58,6 +57,31 @@ private void update()
5857
// Roll back to the first line
5958
cu.curUp(activeBars.size());
6059
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();
6185
}
6286

6387
/**
@@ -68,8 +92,6 @@ private void update()
6892
public void finishBar(ProgressRow bar)
6993
{
7094
this.activeBars.remove(bar);
71-
out.println();
72-
update();
7395
}
7496

7597
/**
@@ -80,30 +102,33 @@ public void finishBar(ProgressRow bar)
80102
@Override
81103
public void close()
82104
{
83-
System.out.println("");
105+
}
106+
107+
public List<ProgressRow> getActiveBars()
108+
{
109+
return activeBars;
84110
}
85111

86112
public static void main(String[] args)
87113
{
88114
try (var b = new ProgressBar(ProgressBarTheme.ASCII_THEME))
89115
{
90-
var r = b.appendBar(new ProgressRow(1000, "it"));
91-
for (int i = 0; i < 1000; i++)
116+
//var r = b.appendBar(new ProgressRow(1000, "it"));
117+
//for (int i = 0; i < 1000; i++)
118+
//{
119+
// b.increase(r, 1);
120+
// safeSleep(5);
121+
//}
122+
123+
var all = new ArrayList<ProgressRow>();
124+
for (int i = 0; i < 1300; i++)
92125
{
93-
r.completed ++;
94-
b.update();
95-
safeSleep(10);
126+
if (i < 1000 && i % 100 == 0) all.add(b.appendBar(new ProgressRow(300, "it")));
127+
all.forEach(a -> b.increase(a, 1));
128+
safeSleep(3);
96129
}
97130

98-
99-
//var all = new ArrayList<ProgressRow>();
100-
//for (int i = 0; i < 10; i++)
101-
//{
102-
// all.add(b.appendBar(new ProgressRow(3, "it")));
103-
// all.forEach(a -> a.completed++);
104-
// b.update();
105-
// safeSleep(1000);
106-
//}
131+
System.out.println("Done");
107132
}
108133
}
109134
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.hydev.mcpm.client.interaction;
22

3+
import java.util.Objects;
4+
35
import static java.lang.String.format;
46

57
/**
@@ -19,6 +21,9 @@ public ProgressRow(long total, String unit)
1921
this.total = total;
2022
this.completed = 0;
2123
this.unit = unit;
24+
25+
// Add leading space
26+
if (!unit.isBlank() && !unit.startsWith(" ")) this.unit = " " + unit;
2227
}
2328

2429
public ProgressRow(long total)
@@ -37,7 +42,7 @@ public String fmt(ProgressBarTheme theme, int cols)
3742
{
3843
double p = 100d * completed / total;
3944
var placeholder = "PLACEHOLDER_BAR";
40-
var t = format("%s%s%s %.0f %d/%d%s", theme.prefix(), placeholder, theme.suffix(), p, completed, total, unit);
45+
var t = format("%s%s%s %.0f%% %5d/%-5d%s", theme.prefix(), placeholder, theme.suffix(), p, completed, total, unit);
4146

4247
// Add progress bar length
4348
var len = cols - t.length() + placeholder.length();

0 commit comments

Comments
 (0)