AnsiMessagePrinter implements MessagePrinter { public String format(final String message) { logger.debug("Message: {}", message); if (null == message) { return null; } final StringReader reader = new StringReader(message); int ch = 0; State state = new Normal(new Stack<>(), new StringWriter()); try { while (0 <= (ch = reader.read())) { logger.trace("Char: {} - {}", ch < 32 ? ' ' : (char) ch, ch); final State old = state; state = state.next(ch); if (old != state) { logger.debug("{} -> {}", old, state); } } } catch (final IOException ex) { throw new IllegalStateException(); } logger.debug("State: {}", state); logger.debug("Tags: {}", state.getTags()); assertTrue(state instanceof Normal, "Expression is invalid[" + state + ": " + message); assertTrue(state.getTags().isEmpty(), "The closing tag[" + state.getTags() + "] missed: " + message); return state.getWriter().toString(); } AnsiMessagePrinter(final PrintStream out); String format(final String message); String escape(final String str); void print(final String messageId, Object...args); void println(); void println(final String messageId, Object... args); @Override void flush(); static final String COLOR_RESET; static final String COLOR_BLACK; static final String COLOR_RED; static final String COLOR_GREEN; static final String COLOR_YELLOW; static final String COLOR_BLUE; static final String COLOR_MAGENTA; static final String COLOR_CYAN; static final String COLOR_WHITE; static final String COLOR_BRIGHT_BLACK; static final String COLOR_BRIGHT_RED; static final String COLOR_BRIGHT_GREEN; static final String COLOR_BRIGHT_YELLOW; static final String COLOR_BRIGHT_BLUE; static final String COLOR_BRIGHT_MAGENTA; static final String COLOR_BRIGHT_CYAN; static final String COLOR_BRIGHT_WHITE; static final String BG_BLACK; static final String BG_RED; static final String BG_GREEN; static final String BG_YELLOW; static final String BG_BLUE; static final String BG_MAGENTA; static final String BG_CYAN; static final String BG_WHITE; static final String BG_BRIGHT_BLACK; static final String BG_BRIGHT_RED; static final String BG_BRIGHT_GREEN; static final String BG_BRIGHT_YELLOW; static final String BG_BRIGHT_BLUE; static final String BG_BRIGHT_MAGENTA; static final String BG_BRIGHT_CYAN; static final String BG_BRIGHT_WHITE; }
@Test public void testFormat() { final String resetCode = randomUUID().toString(); final String blue = randomUUID().toString(); final HashMap<String, String> colors = new HashMap<>(); colors.put("blue", blue); colors.put("red", blue); final AnsiMessagePrinter printer = new AnsiMessagePrinter(mock(PrintStream.class)); printer.setResetCode(resetCode); printer.setColors(colors); final String encoded = printer.format("<blue>hello, world</blue>"); logger.debug("Encoded: {}", encoded); assertTrue(encoded.startsWith(blue)); assertTrue(encoded.contains("hello")); assertTrue(encoded.contains("world")); assertTrue(encoded.endsWith(resetCode)); }
@Test public void shouldNotThrowException() { final String resetCode = randomUUID().toString(); final String blue = randomUUID().toString(); final HashMap<String, String> colors = new HashMap<>(); colors.put("blue", blue); colors.put("red", blue); final AnsiMessagePrinter printer = new AnsiMessagePrinter(mock(PrintStream.class)); printer.setResetCode(resetCode); printer.setColors(colors); printer.format("<red>No command!!</red>"); }