/** Returns a String representation of the text with ANSI escape codes embedded, unless ANSI is * {@linkplain Ansi#enabled()} not enabled}, in which case the plain text is returned. * @return a String representation of the text with ANSI escape codes embedded (if enabled) */ public String toString() { if (!Ansi.this.enabled()) { return plain.toString().substring(from, from + length); } if (length == 0) { return ""; } StringBuilder sb = new StringBuilder(plain.length() + 20 * sections.size()); StyledSection current = null; int end = Math.min(from + length, plain.length()); for (int i = from; i < end; i++) { StyledSection section = findSectionContaining(i); if (section != current) { if (current != null) { sb.append(current.endStyles); } if (section != null) { sb.append(section.startStyles); } current = section; } sb.append(plain.charAt(i)); } if (current != null) { sb.append(current.endStyles); } return sb.toString(); }
/** Returns a String representation of the text with ANSI escape codes embedded, unless ANSI is * {@linkplain Ansi#enabled()} not enabled}, in which case the plain text is returned. * @return a String representation of the text with ANSI escape codes embedded (if enabled) */ public String toString() { if (!Ansi.this.enabled()) { return plain.toString().substring(from, from + length); } if (length == 0) { return ""; } StringBuilder sb = new StringBuilder(plain.length() + 20 * sections.size()); StyledSection current = null; int end = Math.min(from + length, plain.length()); for (int i = from; i < end; i++) { StyledSection section = findSectionContaining(i); if (section != current) { if (current != null) { sb.append(current.endStyles); } if (section != null) { sb.append(section.startStyles); } current = section; } sb.append(plain.charAt(i)); } if (current != null) { sb.append(current.endStyles); } return sb.toString(); }