Skip to content

Commit 2ccc95d

Browse files
committed
[+] Progress bar row
1 parent 6f189c7 commit 2ccc95d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.hydev.mcpm.client.interaction;
2+
3+
import static java.lang.String.format;
4+
5+
/**
6+
* Row of a progress bar
7+
*
8+
* @author Azalea (https://github.com/hykilpikonna)
9+
* @since 2022-10-30
10+
*/
11+
public class ProgressRow
12+
{
13+
protected long total;
14+
protected long completed;
15+
protected String unit;
16+
17+
public ProgressRow(long total, String unit)
18+
{
19+
this.total = total;
20+
this.completed = 0;
21+
this.unit = unit;
22+
}
23+
24+
public ProgressRow(long total)
25+
{
26+
this(total, "it");
27+
}
28+
29+
/**
30+
* Get formatted string of the current progress bar
31+
*
32+
* @param theme Progress bar theme
33+
* @return Formatted string
34+
*/
35+
public String fmt(ProgressBarTheme theme)
36+
{
37+
double p = 100d * completed / total;
38+
var placeholder = "PLACEHOLDER_BAR";
39+
var t = format("%s%s%s %.0f %d/%d%s", theme.prefix(), placeholder, theme.suffix(), p, completed, total, unit);
40+
41+
// Add progress bar length
42+
var len = t.length() - placeholder.length();
43+
44+
// Calculate progress length
45+
int pLen = (int) (completed / total * len);
46+
var bar = theme.done().repeat(pLen / theme.doneLen()) + theme.ipr().repeat(pLen / theme.iprLen());
47+
48+
return t.replaceFirst(placeholder, bar);
49+
}
50+
}

0 commit comments

Comments
 (0)