Add a maps API, and a progress bar creation API

This commit is contained in:
Zontreck 2024-02-11 16:50:27 -07:00
parent 8814702e6b
commit 49eab07801
2 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,28 @@
package dev.zontreck.ariaslib.util;
import java.util.HashMap;
import java.util.Map;
public class Maps
{
public static <A,B> Map<A,B> of(Entry<A,B>... entries) {
Map<A,B> map = new HashMap<>();
for(Entry<A,B> E : entries)
{
map.put(E.key, E.value);
}
return map;
}
public static class Entry<A,B> {
public A key;
public B value;
public Entry(A a, B b)
{
this.key=a;
this.value=b;
}
}
}

View file

@ -1,5 +1,7 @@
package dev.zontreck.ariaslib.util;
import java.io.PrintStream;
public class Percent
{
int current;
@ -16,4 +18,42 @@ public class Percent
{
return ((current * 100) / maximum);
}
private static final int DEFAULT_BAR_WIDTH = 50;
private static int getConsoleWidth() {
return 80; // Default console width, can be adjusted for different consoles
}
public static void printProgressBar(int progress, int maxProgress, String beforeText, String afterText) {
PrintStream out = System.out;
int consoleWidth = getConsoleWidth();
int barWidth = Math.min(consoleWidth - beforeText.length() - afterText.length() - 4, DEFAULT_BAR_WIDTH);
// Calculate progress
int progressBarLength = (int) ((double) progress / maxProgress * barWidth);
// Print before text
out.print(beforeText);
// Print progress bar
out.print("[");
for (int i = 0; i < barWidth; i++) {
if (i < progressBarLength) {
out.print("=");
} else {
out.print(" ");
}
}
out.print("]");
// Print percentage
out.print(" " + (progress * 100 / maxProgress) + "%");
// Print after text
out.print(afterText);
// Move cursor to next line
out.println();
}
}