/** * Creates a new instance of the class. Required by Log4J2. * * @param config the configuration * @param options the options * @return a new instance, or {@code null} if the options are invalid */ public static ColorConverter newInstance(Configuration config, String[] options) { if (options.length < 1) { LOGGER.error("Incorrect number of options on style. " + "Expected at least 1, received {}", options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on style"); return null; } PatternParser parser = PatternLayout.createPatternParser(config); List<PatternFormatter> formatters = parser.parse(options[0]); AnsiElement element = (options.length == 1 ? null : ELEMENTS.get(options[1])); return new ColorConverter(formatters, element); }
@Override public void format(LogEvent event, StringBuilder toAppendTo) { StringBuilder buf = new StringBuilder(); for (PatternFormatter formatter : this.formatters) { formatter.format(event, buf); } if (buf.length() > 0) { AnsiElement element = this.styling; if (element == null) { // Assume highlighting element = LEVELS.get(event.getLevel().intLevel()); element = (element == null ? AnsiColor.GREEN : element); } appendAnsiString(toAppendTo, buf.toString(), element); } }
@Override protected String transform(ILoggingEvent event, String in) { AnsiElement element = ELEMENTS.get(getFirstOption()); if (element == null) { // Assume highlighting element = LEVELS.get(event.getLevel().toInteger()); element = (element == null ? AnsiColor.GREEN : element); } return toAnsiString(in, element); }
private void printBanner(BufferedImage image, int margin, boolean invert, PrintStream out) { AnsiElement background = (invert ? AnsiBackground.BLACK : AnsiBackground.DEFAULT); out.print(AnsiOutput.encode(AnsiColor.DEFAULT)); out.print(AnsiOutput.encode(background)); out.println(); out.println(); AnsiColor lastColor = AnsiColor.DEFAULT; for (int y = 0; y < image.getHeight(); y++) { for (int i = 0; i < margin; i++) { out.print(" "); } for (int x = 0; x < image.getWidth(); x++) { Color color = new Color(image.getRGB(x, y), false); AnsiColor ansiColor = AnsiColors.getClosest(color); if (ansiColor != lastColor) { out.print(AnsiOutput.encode(ansiColor)); lastColor = ansiColor; } out.print(getAsciiPixel(color, invert)); } out.println(); } out.print(AnsiOutput.encode(AnsiColor.DEFAULT)); out.print(AnsiOutput.encode(AnsiBackground.DEFAULT)); out.println(); }
protected String toAnsiString(String in, AnsiElement element) { return AnsiOutput.toString(element, in); }
private ColorConverter(List<PatternFormatter> formatters, AnsiElement styling) { super("style", "style"); this.formatters = formatters; this.styling = styling; }
protected void appendAnsiString(StringBuilder toAppendTo, String in, AnsiElement element) { toAppendTo.append(AnsiOutput.toString(element, in)); }