diff --git a/src/main/java/com/github/pareronia/aoc/solution/ANSIColors.java b/src/main/java/com/github/pareronia/aoc/solution/ANSIColors.java new file mode 100644 index 00000000..0d0c6d77 --- /dev/null +++ b/src/main/java/com/github/pareronia/aoc/solution/ANSIColors.java @@ -0,0 +1,61 @@ +package com.github.pareronia.aoc.solution; + +public class ANSIColors { + + public static final String RESET = "\u001B[0m"; + + // Text attributes + public static final String BOLD = "\u001B[1m"; + + // Regular text colors + public static final String BLACK = "\u001B[30m"; + public static final String RED = "\u001B[31m"; + public static final String GREEN = "\u001B[32m"; + public static final String YELLOW = "\u001B[33m"; + public static final String BLUE = "\u001B[34m"; + public static final String MAGENTA = "\u001B[35m"; + public static final String CYAN = "\u001B[36m"; + public static final String WHITE = "\u001B[37m"; + + // Bright text colors + public static final String BRIGHT_BLACK = "\u001B[30;1m"; + public static final String BRIGHT_RED = "\u001B[31;1m"; + public static final String BRIGHT_GREEN = "\u001B[32;1m"; + public static final String BRIGHT_YELLOW = "\u001B[33;1m"; + public static final String BRIGHT_BLUE = "\u001B[34;1m"; + public static final String BRIGHT_MAGENTA = "\u001B[35;1m"; + public static final String BRIGHT_CYAN = "\u001B[36;1m"; + public static final String BRIGHT_WHITE = "\u001B[37;1m"; + + // Background colors + public static final String BACKGROUND_BLACK = "\u001B[40m"; + public static final String BACKGROUND_RED = "\u001B[41m"; + public static final String BACKGROUND_GREEN = "\u001B[42m"; + public static final String BACKGROUND_YELLOW = "\u001B[43m"; + public static final String BACKGROUND_BLUE = "\u001B[44m"; + public static final String BACKGROUND_MAGENTA = "\u001B[45m"; + public static final String BACKGROUND_CYAN = "\u001B[46m"; + public static final String BACKGROUND_WHITE = "\u001B[47m"; + + // Bright background colors + public static final String BRIGHT_BACKGROUND_BLACK = "\u001B[40;1m"; + public static final String BRIGHT_BACKGROUND_RED = "\u001B[41;1m"; + public static final String BRIGHT_BACKGROUND_GREEN = "\u001B[42;1m"; + public static final String BRIGHT_BACKGROUND_YELLOW = "\u001B[43;1m"; + public static final String BRIGHT_BACKGROUND_BLUE = "\u001B[44;1m"; + public static final String BRIGHT_BACKGROUND_MAGENTA = "\u001B[45;1m"; + public static final String BRIGHT_BACKGROUND_CYAN = "\u001B[46;1m"; + public static final String BRIGHT_BACKGROUND_WHITE = "\u001B[47;1m"; + + public static String bold(final String text) { + return BOLD + text + RESET; + } + + public static String yellow(final String text) { + return YELLOW + text + RESET; + } + + public static String red(final String text) { + return RED + text + RESET; + } +} diff --git a/src/main/java/com/github/pareronia/aoc/solution/SolutionUtils.java b/src/main/java/com/github/pareronia/aoc/solution/SolutionUtils.java index 5c558840..f94b71f2 100644 --- a/src/main/java/com/github/pareronia/aoc/solution/SolutionUtils.java +++ b/src/main/java/com/github/pareronia/aoc/solution/SolutionUtils.java @@ -27,20 +27,16 @@ public static Puzzle puzzle(final Class klass) { } public static String printDuration(final Duration duration) { - final long timeSpent = duration.toNanos() / 1_000; - double time; - String unit; - if (timeSpent < 1000) { - time = timeSpent; - unit = "µs"; - } else if (timeSpent < 1_000_000) { - time = timeSpent / 1000.0; - unit = "ms"; + final double timeSpent = duration.toNanos() / 1_000_000.0; + String time; + if (timeSpent <= 1000) { + time = String.format("%.3f", timeSpent); + } else if (timeSpent <= 5_000) { + time = ANSIColors.yellow(String.format("%.0f", timeSpent)); } else { - time = timeSpent / 1_000_000.0; - unit = "s"; + time = ANSIColors.red(String.format("%.0f", timeSpent)); } - return String.format("%.3f %s", time, unit); + return String.format("%s ms", time); } public static V lap(final String prefix, final Callable callable) @@ -52,7 +48,7 @@ public static V lap(final String prefix, final Callable callable) final V answer = timed.result(); final String duration = printDuration(timed.duration()); System.out.println(String.format("%s : %s, took: %s", - prefix, answer, duration)); + prefix, ANSIColors.bold(answer.toString()), duration)); return answer; }