-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBar.java
More file actions
44 lines (35 loc) · 1.4 KB
/
Copy pathProgressBar.java
File metadata and controls
44 lines (35 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class ProgressBar {
private int barLength;
private int maxValue;
private int decimals = 1;
private String endBuffer = "";
private String fillChar = "#";
private double lastValue = 0.0;
public ProgressBar(int barLength, int numMax, int decimals, String endBuffer, String fillChar) {
this.barLength = barLength;
this.maxValue = numMax;
this.decimals = decimals;
this.endBuffer = endBuffer;
this.fillChar = fillChar;
}
public String updateBar(int currentValue) {
//((int) (Math.pow(10, 2 + decimals) * currentValue / maxValue)) / Math.pow(10, 2 + decimals);
final double BAR_PERCENTAGE = ((int) (Math.pow(10, 2 + decimals) * (currentValue / maxValue))) / Math.pow(10, 2 + decimals);
if (BAR_PERCENTAGE <= lastValue) {
return null;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
final int BAR_PROPORTION = (int) (BAR_PERCENTAGE * barLength);
lastValue = BAR_PERCENTAGE;
final String BAR_STRING = fillChar.repeat(BAR_PROPORTION) + " ".repeat(barLength - BAR_PROPORTION);
clearScreen();
return BAR_PERCENTAGE * 100 + "%" + endBuffer + "[" + BAR_STRING + "]" +endBuffer;
}
private static void clearScreen() {
System.out.print("\033[H\033[2J");
}
}