1
1
package org .hydev .mcpm .client .interaction ;
2
2
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
+
3
12
/**
4
- * TODO: Write a description for this class!
13
+ * Terminal progress bar based on Xterm escape codes
5
14
*
6
15
* @author Azalea (https://github.com/hykilpikonna)
7
16
* @since 2022-09-27
8
17
*/
9
18
public class ProgressBar implements AutoCloseable
10
19
{
20
+ private final ConsoleUtils cu ;
11
21
private final ProgressBarTheme theme ;
22
+ private final PrintStream out ;
23
+ private final int cols ;
24
+
25
+ private final List <ProgressRow > activeBars ;
12
26
13
27
/**
14
28
* Create and initialize a progress bar
@@ -18,50 +32,44 @@ public class ProgressBar implements AutoCloseable
18
32
public ProgressBar (ProgressBarTheme theme )
19
33
{
20
34
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 );
31
40
}
32
41
33
42
/**
34
43
* Append a progress bar at the end
35
44
*
36
- * @return Unique identifier of the progress bar
45
+ * @param bar Row of the progress bar
46
+ * @return bar for fluent access
37
47
*/
38
- public String appendBar ()
48
+ public ProgressRow appendBar (ProgressRow bar )
39
49
{
40
- // TODO: Implement this
41
- throw new UnsupportedOperationException ("TODO" );
50
+ this .activeBars .add (bar );
51
+ out .println ("" );
52
+ update ();
53
+ return bar ;
42
54
}
43
55
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 ()
51
57
{
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 )));
54
61
}
55
62
56
63
/**
57
64
* Finish a progress bar
58
65
*
59
- * @param id Unique identifier of the progress bar
66
+ * @param bar Progress bar
60
67
*/
61
- public void finishBar (String id )
68
+ public void finishBar (ProgressRow bar )
62
69
{
63
- // TODO: Implement this
64
- throw new UnsupportedOperationException ("TODO" );
70
+ this .activeBars .remove (bar );
71
+ out .println ();
72
+ update ();
65
73
}
66
74
67
75
/**
@@ -70,9 +78,32 @@ public void finishBar(String id)
70
78
* @throws Exception e
71
79
*/
72
80
@ 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 )
74
87
{
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
+ }
77
108
}
78
109
}
0 commit comments