Skip to content

Commit

Permalink
java - solution base class - prettify output
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 7, 2024
1 parent 64711fe commit 36ed448
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
61 changes: 61 additions & 0 deletions src/main/java/com/github/pareronia/aoc/solution/ANSIColors.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 9 additions & 13 deletions src/main/java/com/github/pareronia/aoc/solution/SolutionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> V lap(final String prefix, final Callable<V> callable)
Expand All @@ -52,7 +48,7 @@ public static <V> V lap(final String prefix, final Callable<V> 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;
}

Expand Down

0 comments on commit 36ed448

Please sign in to comment.