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) {
Logger rootLog = Logger.getLogger("");
Formatter logFormatter = new LogFormatter();
for (Handler handler : rootLog.getHandlers()) {
handler.setFormatter(logFormatter);
}
Options options = new Options();
options.addOption("d", "debug", false, "Enable debug logging");
@ -109,19 +115,23 @@ public class Compiler {
}
if (cmd.hasOption("debug")) {
Logger rootLog = Logger.getLogger("");
rootLog.setLevel( Level.FINEST );
rootLog.getHandlers()[0].setLevel( Level.FINEST );
Formatter logFormatter = new LogFormatter();
for (Handler handler : rootLog.getHandlers()) {
handler.setFormatter(logFormatter);
}
rootLog.setLevel(Level.FINEST);
rootLog.getHandlers()[0].setLevel(Level.FINEST);
MethodContext.DEBUG = true;
LOGGER.info("Debug logging enabled");
}
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;
}

View File

@ -1,18 +1,52 @@
package de.maishai;
import lombok.Getter;
import java.text.MessageFormat;
import java.util.logging.Formatter;
import java.util.logging.Level;
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 {
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
public String format(LogRecord logRecord) {
String formattedMessage = MessageFormat.format(logRecord.getMessage(), logRecord.getParameters());
if (logRecord.getLevel() == Level.FINEST) {
return formattedMessage + "\n";
}
return logRecord.getLevel() + ": " + formattedMessage + "\n";
return switch (logRecord.getLevel().getName()) {
case "FINEST" -> {
String messageWithParameters = MessageFormat.format(logRecord.getMessage(), logRecord.getParameters());
yield colorize("CodeGen: " + messageWithParameters, TextColor.GREEN) + "\n";
}
case "INFO" -> colorize(basicFormat(logRecord), TextColor.CYAN);
case "SEVERE" -> colorize(basicFormat(logRecord), TextColor.RED);
default -> basicFormat(logRecord);
};
}
}