Revise progress bar to return as a String

This commit is contained in:
Zontreck 2024-02-11 17:09:47 -07:00
parent d4524bbccb
commit e374ac4c78

View file

@ -25,9 +25,10 @@ public class ProgressBar
* @param percent The percentage * @param percent The percentage
* @param beforeText * @param beforeText
* @param afterText * @param afterText
* @return ProgressBar as a String
*/ */
public static void printProgressBar(int percent, String beforeText, String afterText) { public static String printProgressBar(int percent, String beforeText, String afterText) {
PrintStream out = System.out; StringBuilder sb = new StringBuilder();
int consoleWidth = getConsoleWidth(); int consoleWidth = getConsoleWidth();
int barWidth = Math.min(consoleWidth - beforeText.length() - afterText.length() - 4, DEFAULT_BAR_WIDTH); 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); int progressBarLength = (int) ((double) percent / 100 * barWidth);
// Print before text // Print before text
out.print(beforeText); sb.append(beforeText);
// Print progress bar // Print progress bar
out.print("["); sb.append("[");
for (int i = 0; i < barWidth; i++) { for (int i = 0; i < barWidth; i++) {
if (i < progressBarLength) { if (i < progressBarLength) {
out.print("="); sb.append("=");
} else { }else if(i==progressBarLength) sb.append(">");
out.print(" "); else {
sb.append(" ");
} }
} }
out.print("]"); sb.append("]");
// Print percentage // Print percentage
out.print(" " + percent + "%"); sb.append(" " + percent + "%");
// Print after text // Print after text
out.print(afterText); sb.append(afterText);
// Move cursor to next line return sb.toString();
out.println();
} }
} }