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;
|
|
|
|
|
|
|
|
class SimpleLoadingBar {
|
|
|
|
|
|
|
|
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][0-9]|2[0-4]):[0-5][0-9]");
|
|
|
|
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.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 (nextArg.equals("-msg")) {
|
|
|
|
title = args.length > 2 ? args[2] : title;
|
|
|
|
} else {
|
|
|
|
verifyTimeFormat(nextArg, "Zweites Argument");
|
|
|
|
endTime = LocalTime.parse(nextArg, TIME_FORMATTER);
|
|
|
|
}
|
2024-08-07 12:06:12 +02:00
|
|
|
}
|
|
|
|
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 && args[2].equals("-msg");
|
|
|
|
title = hasTitleArg ? args[3] : title;
|
|
|
|
title = title.isBlank() ? fallbackTitle : title;
|
|
|
|
showLoadingBar(startTime, endTime, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void printHelp() {
|
|
|
|
System.out.println(new StringBuilder().append("Mögliche Argumente für LoadingBar:\n")
|
|
|
|
.append("Startzeit, Endzeit, Endnachricht (Optional)\n")
|
|
|
|
.append(TIME_FORMAT).append(" ").append(TIME_FORMAT).append("-msg <Nachricht>\n")
|
|
|
|
.append("Endzeit (Startzeit = jetzt), Endnachricht (Optional)\n")
|
|
|
|
.append(TIME_FORMAT).append("-msg <Nachricht>\n")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.");
|
|
|
|
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 < 100; i++) {
|
|
|
|
if (i < numberOfEquals) {
|
|
|
|
sb.append("=");
|
|
|
|
} else {
|
|
|
|
sb.append("-");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.append("] ").append(PERCENTAGE_FORMAT.format(wholePercentage)).append("% ")
|
|
|
|
.append(minutesToTimeString(passedMinutes)).append(" - ").append(minutesToTimeString(remainingMinutes));
|
|
|
|
if (progressive) {
|
|
|
|
sb.append("\r");
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static String minutesToTimeString(long minutes) {
|
|
|
|
return LocalTime.of((int) minutes / 60, (int) minutes % 60).format(TIME_FORMATTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static String formatTitle(String title) {
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
for (int i = 0; i < title.length(); i++) {
|
|
|
|
sb.append("*");
|
|
|
|
}
|
|
|
|
return sb.toString() + "\n" + title + "\n" + sb.toString();
|
|
|
|
}
|
|
|
|
}
|