make commandline menu nicer

This commit is contained in:
simon 2024-07-04 16:14:48 +02:00
parent a3990ebbcd
commit e4d3f1a141
2 changed files with 58 additions and 14 deletions

View File

@ -96,6 +96,12 @@ public class Compiler {
} }
public static void main(String[] args) { public static void main(String[] args) {
Logger rootLog = Logger.getLogger("");
Formatter logFormatter = new LogFormatter();
for (Handler handler : rootLog.getHandlers()) {
handler.setFormatter(logFormatter);
}
Options options = new Options(); Options options = new Options();
options.addOption("d", "debug", false, "Enable debug logging"); options.addOption("d", "debug", false, "Enable debug logging");
@ -109,19 +115,23 @@ public class Compiler {
} }
if (cmd.hasOption("debug")) { if (cmd.hasOption("debug")) {
Logger rootLog = Logger.getLogger(""); rootLog.setLevel(Level.FINEST);
rootLog.setLevel( Level.FINEST ); rootLog.getHandlers()[0].setLevel(Level.FINEST);
rootLog.getHandlers()[0].setLevel( Level.FINEST );
Formatter logFormatter = new LogFormatter();
for (Handler handler : rootLog.getHandlers()) {
handler.setFormatter(logFormatter);
}
MethodContext.DEBUG = true; MethodContext.DEBUG = true;
LOGGER.info("Debug logging enabled"); LOGGER.info("Debug logging enabled");
} }
if (cmd.getArgs().length == 0) { if (cmd.getArgs().length == 0) {
LOGGER.severe("No source files provided"); HelpFormatter formatter = new HelpFormatter();
String header = """
Options:""";
String footer = """
Examples:
java -jar compilerUltimate.jar MyClass.java
java -jar compilerUltimate.jar MyClass.java AnotherClass.java""";
formatter.printHelp("java -jar compilerUltimate.jar [options] <file1> <file2> ...", header, options, footer);
return; return;
} }

View File

@ -1,18 +1,52 @@
package de.maishai; package de.maishai;
import lombok.Getter;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
@Getter
enum TextColor {
RED("\033[0;31m"),
GREEN("\u001B[32m"),
YELLOW("\u001B[33m"),
BLUE("\u001B[34m"),
PURPLE("\u001B[35m"),
CYAN("\u001B[36m"),
WHITE("\u001B[37m");
private final String code;
TextColor(String code) {
this.code = code;
}
}
public class LogFormatter extends Formatter { public class LogFormatter extends Formatter {
private static final String ANSI_RESET = "\u001B[0m";
private static String colorize(String message, TextColor color) {
return color.getCode() + message + ANSI_RESET;
}
private static String basicFormat(LogRecord logRecord) {
String messageWithParameters = MessageFormat.format(logRecord.getMessage(), logRecord.getParameters());
return logRecord.getLevel() + ": " + messageWithParameters + "\n";
}
@Override @Override
public String format(LogRecord logRecord) { public String format(LogRecord logRecord) {
String formattedMessage = MessageFormat.format(logRecord.getMessage(), logRecord.getParameters());
if (logRecord.getLevel() == Level.FINEST) { return switch (logRecord.getLevel().getName()) {
return formattedMessage + "\n"; case "FINEST" -> {
} String messageWithParameters = MessageFormat.format(logRecord.getMessage(), logRecord.getParameters());
return logRecord.getLevel() + ": " + formattedMessage + "\n"; yield colorize("CodeGen: " + messageWithParameters, TextColor.GREEN) + "\n";
}
case "INFO" -> colorize(basicFormat(logRecord), TextColor.CYAN);
case "SEVERE" -> colorize(basicFormat(logRecord), TextColor.RED);
default -> basicFormat(logRecord);
};
} }
} }