Skúšobne som si vytvoril CLR procesor pre farebný výpis na konzole. Syntax sa ale v blízkej budúcnosti pravdepodobne môže zmeniť.
import java.util.List; import java.util.ArrayList; import java.util.Arrays; enum Ansi { RESET("\u001B[0m"), RED("\u001B[31m"), GREEN("\u001B[32m"), YELLOW("\u001B[33m"), BLUE("\u001B[34m"), WHITE("\u001B[37m"), DEFAULT("\u001B[39m"); private final String code; Ansi(String code) { this.code = code; } public String c() { return code; } public static boolean contains(String val) { return Arrays.stream(Ansi.values()).anyMatch(n -> n.name().equalsIgnoreCase(val)); } } void main() { System.out.println(Ansi.RED); String name = "John Doe"; System.out.println(STR."\{Ansi.BLUE.c()}an old falcon \{Ansi.RESET.c()}"); System.out.println(CLR."RED Hello \{name}!"); System.out.println(CLR."GREEN Hi \{name}!"); System.out.println(CLR."YELLOW Halo \{name}!"); System.out.println(CLR."BLUE Ahoj \{name}!"); System.out.println(CLR."GREEN Ahoj \{name}!"); System.out.println(CLR."BLUE Ahoj ako sa mas \{name}?"); System.out.println(CLR."BLUEs ? 21 d"); System.out.println(CLR.""); System.out.println(CLR." "); } StringTemplate.Processor<String, RuntimeException> CLR = temp -> { List<String> fragments = new ArrayList<>(temp.fragments()); String first = fragments.getFirst(); String[] els = first.split(" ", 2); if (els.length != 2) { return StringTemplate.interpolate(temp.fragments(), temp.values()); } Ansi val = Ansi.DEFAULT; String col = els[0]; String msg = els[1]; if (!Ansi.contains(col)) { return StringTemplate.interpolate(temp.fragments(), temp.values()); } else { val = Ansi.valueOf(col); String res = switch (val) { // case null -> Ansi.DEFAULT.c(); case Ansi.RED -> Ansi.RED.c(); case Ansi.GREEN -> Ansi.GREEN.c(); case Ansi.YELLOW -> Ansi.YELLOW.c(); case Ansi.BLUE -> Ansi.BLUE.c(); default -> Ansi.DEFAULT.c(); }; fragments.removeFirst(); fragments.addFirst(res + msg); String last = fragments.removeLast(); fragments.addLast(last + Ansi.RESET.c()); return StringTemplate.interpolate(fragments, temp.values()); } };