Skip to content

Commit

Permalink
feat: 彩色控制台
Browse files Browse the repository at this point in the history
  • Loading branch information
0XPYEX0 committed Aug 31, 2024
1 parent 17f279c commit 9508575
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions src/main/java/me/xpyex/module/cnusername/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,73 @@

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;

public class Logging {
private static final String PREFIX = "[CnUsername] ";
private static final String PREFIX = "§b[CnUsername] §r";
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("HH:mm:ss");
private static Logger LOGGER;
private static final AtomicReference<Logger> LOGGER = new AtomicReference<>();

public static void info(String s) {
if (LOGGER == null) {
System.out.println("[" + FORMAT.format(new Date()) + " INFO]: " + PREFIX + s);
if (LOGGER.get() == null) {
System.out.println(ColoredConsole.toANSI("[" + FORMAT.format(new Date()) + " §aINFO§r]: " + PREFIX + s));
return;
}
getLogger().info(PREFIX + s);
getLogger().info(ColoredConsole.toANSI(PREFIX + s));
}

public static void warning(String s) {
if (LOGGER == null) {
System.out.println("[" + FORMAT.format(new Date()) + " WARN]: " + PREFIX + s);
if (LOGGER.get() == null) {
System.out.println(ColoredConsole.toANSI("[" + FORMAT.format(new Date()) + " §eWARN§r]: " + PREFIX + s));
return;
}
getLogger().warning(PREFIX + s);
getLogger().warning(ColoredConsole.toANSI(PREFIX + s));
}

public static Logger getLogger() {
return LOGGER;
return LOGGER.get();
//
}

public static void setLogger(Logger logger) {
LOGGER = logger;
LOGGER.set(logger);
//
}

public static class ColoredConsole {
private static final HashMap<String, String> MCColorToANSI = new HashMap<>();
static {
MCColorToANSI.put("§0", "\u001B[30m");
MCColorToANSI.put("§1", "\u001B[34m");
MCColorToANSI.put("§2", "\u001B[32m");
MCColorToANSI.put("§3", "\u001B[36m");
MCColorToANSI.put("§4", "\u001B[31m");
MCColorToANSI.put("§5", "\u001B[35m");
MCColorToANSI.put("§6", "\u001B[33m");
MCColorToANSI.put("§7", "\u001B[37m");
MCColorToANSI.put("§8", "\u001B[90m");
MCColorToANSI.put("§9", "\u001B[94m");
MCColorToANSI.put("§a", "\u001B[92m");
MCColorToANSI.put("§b", "\u001B[96m");
MCColorToANSI.put("§c", "\u001B[91m");
MCColorToANSI.put("§d", "\u001B[95m");
MCColorToANSI.put("§e", "\u001B[93m");
MCColorToANSI.put("§f", "\u001B[97m");
MCColorToANSI.put("§k", "\u001B[5m");
MCColorToANSI.put("§l", "\u001B[1m");
MCColorToANSI.put("§m", "\u001B[9m");
MCColorToANSI.put("§n", "\u001B[4m");
MCColorToANSI.put("§o", "\u001B[3m");
MCColorToANSI.put("§r", "\u001B[0m");
}

public static String toANSI(String s) {
if (s == null || s.isEmpty()) return "";
final String[] out = {s};
MCColorToANSI.forEach((mc, ansi) -> out[0] = out[0].replace(mc, ansi));
return out[0];
}
}
}

0 comments on commit 9508575

Please sign in to comment.