From e374ac4c782220282f7f78b72c0f72a9110d9ccd Mon Sep 17 00:00:00 2001 From: Zontreck Date: Sun, 11 Feb 2024 17:09:47 -0700 Subject: [PATCH] Revise progress bar to return as a String --- .../zontreck/ariaslib/util/ProgressBar.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java b/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java index db47c17..e56e94a 100644 --- a/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java +++ b/src/main/java/dev/zontreck/ariaslib/util/ProgressBar.java @@ -25,9 +25,10 @@ public class ProgressBar * @param percent The percentage * @param beforeText * @param afterText + * @return ProgressBar as a String */ - public static void printProgressBar(int percent, String beforeText, String afterText) { - PrintStream out = System.out; + public static String printProgressBar(int percent, String beforeText, String afterText) { + StringBuilder sb = new StringBuilder(); int consoleWidth = getConsoleWidth(); int barWidth = Math.min(consoleWidth - beforeText.length() - afterText.length() - 4, DEFAULT_BAR_WIDTH); @@ -35,26 +36,26 @@ public class ProgressBar int progressBarLength = (int) ((double) percent / 100 * barWidth); // Print before text - out.print(beforeText); + sb.append(beforeText); // Print progress bar - out.print("["); + sb.append("["); for (int i = 0; i < barWidth; i++) { if (i < progressBarLength) { - out.print("="); - } else { - out.print(" "); + sb.append("="); + }else if(i==progressBarLength) sb.append(">"); + else { + sb.append(" "); } } - out.print("]"); + sb.append("]"); // Print percentage - out.print(" " + percent + "%"); + sb.append(" " + percent + "%"); // Print after text - out.print(afterText); + sb.append(afterText); - // Move cursor to next line - out.println(); + return sb.toString(); } }