Add javadoc, split progressbar to its own class

This commit is contained in:
Zontreck 2024-02-11 17:01:56 -07:00
parent 49eab07801
commit d4524bbccb
3 changed files with 83 additions and 38 deletions

View file

@ -3,8 +3,18 @@ package dev.zontreck.ariaslib.util;
import java.util.HashMap;
import java.util.Map;
/**
* Utility class to assist in creating a dictionary programmatically in one line of code.
*/
public class Maps
{
/**
* This takes a list of entries and returns a HashMap
* @param entries The entries you want in your hashmap
* @return The map itself
* @param <A> Any typed parameter
* @param <B> Any typed parameter
*/
public static <A,B> Map<A,B> of(Entry<A,B>... entries) {
Map<A,B> map = new HashMap<>();
for(Entry<A,B> E : entries)
@ -15,10 +25,21 @@ public class Maps
return map;
}
/**
* A virtual entry used only by the Maps#of function.
* @see Maps#of(Entry[])
* @param <A> Any typed parameter
* @param <B> Any typed parameter
*/
public static class Entry<A,B> {
public A key;
public B value;
public final A key;
public final B value;
/**
* Initializes the readonly entry
* @param a The dictionary key
* @param b The value
*/
public Entry(A a, B b)
{
this.key=a;

View file

@ -20,40 +20,4 @@ public class Percent
}
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();
}
}

View file

@ -0,0 +1,60 @@
package dev.zontreck.ariaslib.util;
import java.io.PrintStream;
/**
* Utility to create an ascii progress bar
*/
public class ProgressBar
{
private static final int DEFAULT_BAR_WIDTH = 50;
/**
* Always will return 80
* @return 80
*/
private static int getConsoleWidth() {
return 80; // Default console width, can be adjusted for different consoles
}
/**
* Print out a progress bar
* <br/><br/>
* your text here [========= ] 40% your text here
* @param percent The percentage
* @param beforeText
* @param afterText
*/
public static void printProgressBar(int percent, 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) percent / 100 * 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(" " + percent + "%");
// Print after text
out.print(afterText);
// Move cursor to next line
out.println();
}
}