JavaUtils/SimpleLoadingBar.java

147 lines
5.5 KiB
Java
Raw Permalink Normal View History

2024-08-05 14:33:49 +02:00
import java.text.DecimalFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
public class SimpleLoadingBar {
2024-08-05 14:33:49 +02:00
private static final String TIME_FORMAT = "HH:mm";
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_FORMAT);
private static final Pattern TIME_PATTERN = Pattern.compile("(?>[01]\\d|2[0-4]):[0-5]\\d");
2024-08-05 14:33:49 +02:00
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
private static final int MINS_PER_HOUR = 60;
private static final int LINE_LENGTH = 100;
2024-08-05 14:33:49 +02:00
public static void main(String[] args) {
if (args.length > 0 && Objects.equals(args[0], "--help")) {
printHelp();
return;
}
verifyMinimumNumberOfArgs(args);
String nextArg = args[0];
verifyTimeFormat(nextArg, "Erstes Argument"); // FSFIXME package
var firstTime = LocalTime.parse(nextArg, TIME_FORMATTER); // FSFIXME package
LocalTime startTime = null;
String title = "";
LocalTime endTime = null;
if (args.length > 1) {
startTime = firstTime;
nextArg = args[1];
if ("-msg".equals(nextArg)) {
2024-08-05 14:33:49 +02:00
title = args.length > 2 ? args[2] : title;
} else {
verifyTimeFormat(nextArg, "Zweites Argument");
endTime = LocalTime.parse(nextArg, TIME_FORMATTER);
}
}
if (endTime == null) {
2024-08-05 14:33:49 +02:00
startTime = LocalTime.now();
endTime = firstTime;
}
String fallbackTitle = "Ende! Endzeit " + TIME_FORMATTER.format(endTime) + " erreicht.";
if (args.length == 2 || !title.isBlank()) {
title = title.isBlank() ? fallbackTitle : title;
showLoadingBar(startTime, endTime, title);
return;
}
// if there are 3 arguments, the third will be discarded.
boolean hasTitleArg = args.length > 3 && "-msg".equals(args[2]);
2024-08-05 14:33:49 +02:00
title = hasTitleArg ? args[3] : title;
title = title.isBlank() ? fallbackTitle : title;
showLoadingBar(startTime, endTime, title);
}
private static void printHelp() {
System.out.println("Mögliche Argumente für LoadingBar:\n"
+ "Startzeit, Endzeit, Endnachricht (Optional)\n"
+ TIME_FORMAT + " " + TIME_FORMAT + " -msg <Nachricht>\n"
+ "Endzeit (Startzeit = jetzt), Endnachricht (Optional)\n"
+ TIME_FORMAT + " -msg <Nachricht>\n"
2024-08-05 14:33:49 +02:00
);
}
private static void verifyMinimumNumberOfArgs(String[] args) {
if (args.length >= 1) {
return;
}
System.out.println("Mindestens 1 Argument muss gegeben sein.");
printHelp();
System.exit(1);
}
private static void verifyTimeFormat(String param, String errMsgPrefix) {
if (TIME_PATTERN.matcher(param).matches()) {
return;
}
System.out.println(errMsgPrefix + " \"" + param + "\" muss Uhrzeitformat (" + TIME_FORMAT + ") entsprechen.");
2024-08-05 14:33:49 +02:00
System.exit(1);
}
public static void showLoadingBar(LocalTime startTime, LocalTime endTime, String title) {
long initialMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
System.out.print(minutesToTimeString(initialMinutes) + " gesamt; Endzeit: " + TIME_FORMATTER.format(endTime) + "\n");
long passedMinutes = startTime.until(LocalTime.now(), ChronoUnit.MINUTES);
if (passedMinutes > initialMinutes) {
passedMinutes = initialMinutes;
} else if (passedMinutes < 0) {
System.out.println(fillLoadingBar(initialMinutes, 0, false));
return;
}
while (passedMinutes < initialMinutes) {
System.out.print(fillLoadingBar(initialMinutes, passedMinutes, true));
try {
var now = LocalTime.now();
var oneMinuteLater = now.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
// +1 second to adjust for ignored milliseconds as it is better to switch between 00 and 01 as between 59 and 00
TimeUnit.SECONDS.sleep(now.until(oneMinuteLater, ChronoUnit.SECONDS) + 1);
// TimeUnit.SECONDS.sleep(1L); // DEBUG
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
passedMinutes++;
}
System.out.println(fillLoadingBar(initialMinutes, passedMinutes, false));
System.out.println(formatTitle(title));
}
private static String fillLoadingBar(long initialMinutes, long passedMinutes, boolean progressive) {
double wholePercentage = ((double) passedMinutes / initialMinutes) * 100;
long remainingMinutes = initialMinutes - passedMinutes;
int numberOfEquals = (int) wholePercentage;
var sb = new StringBuilder("[");
for (int i = 0; i < LINE_LENGTH; i++) {
2024-08-05 14:33:49 +02:00
if (i < numberOfEquals) {
sb.append("=");
} else {
sb.append("-");
}
}
sb.append("] ").append(PERCENTAGE_FORMAT.format(wholePercentage)).append("% ")
.append(minutesToTimeString(passedMinutes)).append(" - ").append(minutesToTimeString(remainingMinutes));
2024-08-05 14:33:49 +02:00
if (progressive) {
sb.append("\r");
}
return sb.toString();
}
private static String minutesToTimeString(long minutes) {
return LocalTime.of((int) minutes / MINS_PER_HOUR, (int) minutes % MINS_PER_HOUR).format(TIME_FORMATTER);
2024-08-05 14:33:49 +02:00
}
private static String formatTitle(String title) {
String separator = "*".repeat(title.length());
return separator + "\n" + title + "\n" + separator;
2024-08-05 14:33:49 +02:00
}
}