Skip to content

Commit 81e540f

Browse files
committed
[+] PBar: Simple progress bar
1 parent ccc8365 commit 81e540f

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed
Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
package org.hydev.mcpm.client.interaction;
22

3+
import org.fusesource.jansi.AnsiConsole;
4+
import org.hydev.mcpm.utils.ConsoleUtils;
5+
6+
import java.io.PrintStream;
7+
import java.util.*;
8+
9+
import static java.lang.String.format;
10+
import static org.hydev.mcpm.utils.GeneralUtils.safeSleep;
11+
312
/**
4-
* TODO: Write a description for this class!
13+
* Terminal progress bar based on Xterm escape codes
514
*
615
* @author Azalea (https://github.com/hykilpikonna)
716
* @since 2022-09-27
817
*/
918
public class ProgressBar implements AutoCloseable
1019
{
20+
private final ConsoleUtils cu;
1121
private final ProgressBarTheme theme;
22+
private final PrintStream out;
23+
private final int cols;
24+
25+
private final List<ProgressRow> activeBars;
1226

1327
/**
1428
* Create and initialize a progress bar
@@ -18,50 +32,44 @@ public class ProgressBar implements AutoCloseable
1832
public ProgressBar(ProgressBarTheme theme)
1933
{
2034
this.theme = theme;
21-
this.init();
22-
}
23-
24-
/**
25-
* Initialize the progress bar (print the first line)
26-
*/
27-
public void init()
28-
{
29-
// TODO: Implement this
30-
throw new UnsupportedOperationException("TODO");
35+
this.out = System.out;
36+
this.cu = new ConsoleUtils(this.out);
37+
this.activeBars = new ArrayList<>();
38+
this.cols = AnsiConsole.getTerminalWidth();
39+
System.out.println(cols);
3140
}
3241

3342
/**
3443
* Append a progress bar at the end
3544
*
36-
* @return Unique identifier of the progress bar
45+
* @param bar Row of the progress bar
46+
* @return bar for fluent access
3747
*/
38-
public String appendBar()
48+
public ProgressRow appendBar(ProgressRow bar)
3949
{
40-
// TODO: Implement this
41-
throw new UnsupportedOperationException("TODO");
50+
this.activeBars.add(bar);
51+
out.println("");
52+
update();
53+
return bar;
4254
}
4355

44-
/**
45-
* Set progress for a bar
46-
*
47-
* @param id Unique identifier
48-
* @param progress Progress as a ratio in range 0-1
49-
*/
50-
public void setBar(String id, float progress)
56+
private void update()
5157
{
52-
// TODO: Implement this
53-
throw new UnsupportedOperationException("TODO");
58+
// Roll back to the first line
59+
cu.curUp(activeBars.size());
60+
activeBars.forEach(bar -> out.println(bar.fmt(theme, cols)));
5461
}
5562

5663
/**
5764
* Finish a progress bar
5865
*
59-
* @param id Unique identifier of the progress bar
66+
* @param bar Progress bar
6067
*/
61-
public void finishBar(String id)
68+
public void finishBar(ProgressRow bar)
6269
{
63-
// TODO: Implement this
64-
throw new UnsupportedOperationException("TODO");
70+
this.activeBars.remove(bar);
71+
out.println();
72+
update();
6573
}
6674

6775
/**
@@ -70,9 +78,32 @@ public void finishBar(String id)
7078
* @throws Exception e
7179
*/
7280
@Override
73-
public void close() throws Exception
81+
public void close()
82+
{
83+
System.out.println("");
84+
}
85+
86+
public static void main(String[] args)
7487
{
75-
// TODO: Implement this
76-
throw new UnsupportedOperationException("TODO");
88+
try (var b = new ProgressBar(ProgressBarTheme.ASCII_THEME))
89+
{
90+
var r = b.appendBar(new ProgressRow(1000, "it"));
91+
for (int i = 0; i < 1000; i++)
92+
{
93+
r.completed ++;
94+
b.update();
95+
safeSleep(10);
96+
}
97+
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+
//}
107+
}
77108
}
78109
}

0 commit comments

Comments
 (0)