public static void repeatChar(AttributedStringBuilder sb, char c, int count) { IntStream.range(0, count).forEach(i -> sb.append(c)); }
public static AttributedString messageError(String message, String s) { final AttributedStringBuilder builder = new AttributedStringBuilder() .style(AttributedStyle.DEFAULT.bold().foreground(AttributedStyle.RED)) .append("[ERROR] ") .append(message); if (s != null) { builder .append(" Reason:\n") .append(s); } return builder.toAttributedString(); }
@Override protected List<AttributedString> computeMainLines() { final List<AttributedString> lines = new ArrayList<>(); final AttributedStringBuilder sb = new AttributedStringBuilder(); IntStream.range(0, row.length).forEach(i -> { final String name = columnNames[i]; final String type = columnTypes[i]; sb.setLength(0); sb.append(CliStrings.DEFAULT_MARGIN); sb.style(AttributedStyle.BOLD); sb.append(name); sb.append(" ("); sb.append(type); sb.append(')'); sb.append(':'); lines.add(sb.toAttributedString()); sb.setLength(0); sb.append(CliStrings.DEFAULT_MARGIN); sb.style(AttributedStyle.DEFAULT); sb.append(row[i]); lines.add(sb.toAttributedString()); lines.add(AttributedString.EMPTY); }); return lines; }
public static List<AttributedString> formatTwoLineHelpOptions(int width, List<Tuple2<String, String>> options) { final AttributedStringBuilder line1 = new AttributedStringBuilder(); final AttributedStringBuilder line2 = new AttributedStringBuilder(); // we assume that every options has not more than 11 characters (+ key and space) final int columns = (int) Math.ceil(((double) options.size()) / 2); final int space = (width - CliStrings.DEFAULT_MARGIN.length() - columns * 13) / columns; final Iterator<Tuple2<String, String>> iter = options.iterator(); while (iter.hasNext()) { // first line Tuple2<String, String> option = iter.next(); line1.style(AttributedStyle.DEFAULT.inverse()); line1.append(option.f0); line1.style(AttributedStyle.DEFAULT); line1.append(' '); line1.append(option.f1); repeatChar(line1, ' ', (11 - option.f1.length()) + space); // second line if (iter.hasNext()) { option = iter.next(); line2.style(AttributedStyle.DEFAULT.inverse()); line2.append(option.f0); line2.style(AttributedStyle.DEFAULT); line2.append(' '); line2.append(option.f1); repeatChar(line2, ' ', (11 - option.f1.length()) + space); } } return Arrays.asList(line1.toAttributedString(), line2.toAttributedString()); }
inputLine.append(CliStrings.DEFAULT_MARGIN + "> "); final String input = currentInput.toString(); inputLine.append(currentInput.substring(0, cursorPos)); inputLine.style(AttributedStyle.DEFAULT.inverse().blink()); if (cursorPos < input.length()) { inputLine.append(input.charAt(cursorPos)); inputLine.style(AttributedStyle.DEFAULT); inputLine.append(input.substring(cursorPos + 1, input.length())); } else { inputLine.append(' '); // show the cursor at the end final AttributedStringBuilder errorLine = new AttributedStringBuilder(); errorLine.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)); errorLine.append(CliStrings.DEFAULT_MARGIN + CliStrings.INPUT_ERROR); lines.add(AttributedString.EMPTY); lines.add(errorLine.toAttributedString());
@Override protected List<AttributedString> computeMainHeaderLines() { final AttributedStringBuilder schemaHeader = new AttributedStringBuilder(); // add change column schemaHeader.append(' '); schemaHeader.style(AttributedStyle.DEFAULT.underline()); schemaHeader.append("+/-"); schemaHeader.style(AttributedStyle.DEFAULT); Arrays.stream(resultDescriptor.getResultSchema().getFieldNames()).forEach(s -> { schemaHeader.append(' '); schemaHeader.style(AttributedStyle.DEFAULT.underline()); normalizeColumn(schemaHeader, s, MAX_COLUMN_WIDTH); schemaHeader.style(AttributedStyle.DEFAULT); }); return Collections.singletonList(schemaHeader.toAttributedString()); }
@Override protected List<AttributedString> computeHeaderLines() { final AttributedStringBuilder statusLine = new AttributedStringBuilder(); statusLine.style(AttributedStyle.INVERSE); // left final String left; if (isRetrieving()) { left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_REFRESH_INTERVAL + ' ' + REFRESH_INTERVALS.get(refreshInterval).f0; } else { left = CliStrings.DEFAULT_MARGIN + CliStrings.RESULT_STOPPED; } // right final String right; if (lastRetrieval == null) { right = CliStrings.RESULT_LAST_REFRESH + ' ' + CliStrings.RESULT_REFRESH_UNKNOWN + CliStrings.DEFAULT_MARGIN; } else { right = CliStrings.RESULT_LAST_REFRESH + ' ' + lastRetrieval.format(TIME_FORMATTER) + CliStrings.DEFAULT_MARGIN; } // all together final int middleSpace = getWidth() - left.length() - right.length(); statusLine.append(left); repeatChar(statusLine, ' ', middleSpace); statusLine.append(right); return Arrays.asList(statusLine.toAttributedString(), AttributedString.EMPTY); }
@Override protected List<AttributedString> computeMainHeaderLines() { final AttributedStringBuilder schemaHeader = new AttributedStringBuilder(); Arrays.stream(resultDescriptor.getResultSchema().getFieldNames()).forEach(s -> { schemaHeader.append(' '); schemaHeader.style(AttributedStyle.DEFAULT.underline()); normalizeColumn(schemaHeader, s, MAX_COLUMN_WIDTH); schemaHeader.style(AttributedStyle.DEFAULT); }); return Collections.singletonList(schemaHeader.toAttributedString()); }
private AttributedString computeTitleLine() { final String title = getTitle(); final AttributedStringBuilder titleLine = new AttributedStringBuilder(); titleLine.style(AttributedStyle.INVERSE); final int totalMargin = width - title.length(); final int margin = totalMargin / 2; repeatChar(titleLine, ' ', margin); titleLine.append(title); repeatChar(titleLine, ' ', margin + (totalMargin % 2)); return titleLine.toAttributedString(); }
final int columnWidth = computeColumnWidth(colIdx); row.append(' ');
public static AttributedString join(AttributedString delimiter, Iterable<AttributedString> elements) { Objects.requireNonNull(elements); AttributedStringBuilder sb = new AttributedStringBuilder(); int i = 0; for (AttributedString str : elements) { if (i++ > 0 && delimiter != null) { sb.append(delimiter); } sb.append(str); } return sb.toAttributedString(); }
private static void addHighlightedTextToBuilder(String output, String textToHighlight, AttributedStringBuilder builder) { String[] split = output.split(textToHighlight); for (int i = 0; i < split.length - 1; i++) { builder.append(split[i]) .style(SshSessionContext.<AttributedStyle>get(HIGHLIGHT_COLOR)) .append(textToHighlight) .style(SshSessionContext.<AttributedStyle>get(TEXT_STYLE)); } builder.append(split[split.length - 1]); }
private void appendUnderlinedFormal(AttributedStringBuilder result, ParameterDescription description) { for (char c : description.formal().toCharArray()) { if (c != ' ') { result.append("" + c, AttributedStyle.DEFAULT.underline()); } else { result.append(c); } } }
private void concat(List<AttributedString> lines, AttributedStringBuilder sb) { if (lines.size() > 1) { for (int i = 0; i < lines.size() - 1; i++) { sb.append(lines.get(i)); sb.style(sb.style().inverse()); sb.append("\\n"); sb.style(sb.style().inverseOff()); } } sb.append(lines.get(lines.size() - 1)); }
public static AttributedString append(CharSequence... strings) { AttributedStringBuilder sb = new AttributedStringBuilder(); for (CharSequence s : strings) { sb.append(s); } return sb.toAttributedString(); }