me.legrange.console.ProgressBar Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of console-animation Show documentation
Show all versions of console-animation Show documentation
A small console animation library
The newest version!
package me.legrange.console;
import static java.lang.Math.floor;
import static java.lang.Math.round;
final class ProgressBar extends Animator {
private final int size;
private final BarStyle style;
private final double base;
ProgressBar(int size, BarStyle style) {
this.size = size;
this.style = style;
base = 1D/style.chars().length;
}
@Override
public String draw(N min, N max, N value) {
return generateProgressBar(value.doubleValue(), min.doubleValue(), max.doubleValue(), size);
}
private String generateProgressBar(double value, double vmin, double vmax, int length) {
// normalize value
value = Math.min(Math.max(value, vmin), vmax);
value = (value - vmin) / (vmax - vmin);
double v = value * length;
int x = (int) floor(v); // integer part
double y = v - x; // fractional part
// round(base*math.floor(float(y)/base),prec)/base
int i = (int) ((round((base * floor(y / base)) * 1000D) / base) / 1000D);
String bar = style.chars()[style.chars().length-1].repeat(x) + style.chars()[i];
int n = length - bar.length();
return bar + style.chars()[0].repeat(n);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy