TerminalCharacter { public String toString(TerminalCharacter prev) { if(equalsIgnoreCharacter(prev)) return String.valueOf(character); else { StringBuilder builder = new StringBuilder(); builder.append(ANSI.START); if(!style.equals(prev.getStyle())) { builder.append(style.getValueComparedToPrev(prev.getStyle())); } if(!this.color.equals(prev.color)) { if(prev.getStyle().isInvert()) { if(builder.charAt(builder.length()-1) == '[') builder.append(this.color.toString()); else builder.append(';').append(this.color.toString()); } else { if(builder.charAt(builder.length()-1) == '[') builder.append(this.color.toString(prev.color)); else builder.append(';').append(this.color.toString(prev.color)); } } builder.append('m'); builder.append(getCharacter()); return builder.toString(); } } TerminalCharacter(char c);  TerminalCharacter(char c, TerminalTextStyle style);  TerminalCharacter(char c, TerminalColor color);  TerminalCharacter(char c, TerminalColor color,
                             CharacterType type);  TerminalCharacter(char c, TerminalColor color,
                             TerminalTextStyle style); char getCharacter(); void setCharacter(char c); TerminalTextStyle getStyle(); String toString(TerminalCharacter prev); @Override String toString(); boolean equalsIgnoreCharacter(TerminalCharacter that); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testTerminalCharacterAsString() { TerminalCharacter character = new TerminalCharacter('c', new TerminalTextStyle(CharacterType.BOLD)); assertEquals(ANSI.START+ CharacterType.BOLD.getValue()+";"+ 3+ Color.DEFAULT.getValue()+";"+ 4+Color.DEFAULT.getValue()+ "mc", character.toString()); }
@Test public void testPrevCharacterAsString() { TerminalCharacter c1 = new TerminalCharacter('c', new TerminalTextStyle(CharacterType.BOLD)); assertEquals(ANSI.START + CharacterType.BOLD.getValue() + ";" + 3+Color.DEFAULT.getValue() + ";" + 4+Color.DEFAULT.getValue() + "mc", c1.toString()); TerminalCharacter c2 = new TerminalCharacter('f', new TerminalColor(Color.DEFAULT, Color.BLUE), new TerminalTextStyle(CharacterType.CROSSED_OUT)); assertEquals(ANSI.START + BOLD_OFF + ";" + CharacterType.CROSSED_OUT.getValue() + ";" + 4+Color.BLUE.getValue() + "mf", c2.toString(c1)); TerminalCharacter c3 = new TerminalCharacter('f', new TerminalColor(Color.RED, Color.BLUE)); TerminalCharacter c4 = new TerminalCharacter('f', new TerminalColor(Color.RED, Color.BLUE)); assertEquals("f", c4.toString(c3)); c4 = new TerminalCharacter('g', new TerminalColor(Color.RED, Color.BLUE), new TerminalTextStyle(CharacterType.BOLD)); assertEquals(ANSI.START + CharacterType.BOLD.getValue() + "mg", c4.toString(c3)); c3 = new TerminalCharacter('f', new TerminalColor(Color.RED, Color.BLUE), new TerminalTextStyle(CharacterType.BOLD)); c4 = new TerminalCharacter('g'); assertEquals(ANSI.START + "22;39;49mg", c4.toString(c3)); c4 = new TerminalCharacter('g', new TerminalTextStyle(CharacterType.INVERT)); assertEquals(ANSI.START + "22;7;39;49mg", c4.toString(c3)); }