use of org.jline.utils.AttributedString in project SpongeCommon by SpongePowered.
the class BrigadierHighlighter method highlight.
@Override
public AttributedString highlight(final LineReader lineReader, final String buffer) {
final CommandDispatcher<S> dispatcher = this.dispatcherProvider.get();
if (dispatcher == null) {
return new AttributedString(buffer);
}
try {
final ParseResults<S> results = dispatcher.parse(buffer, this.commandSourceProvider.get());
final ImmutableStringReader reader = results.getReader();
final AttributedStringBuilder builder = new AttributedStringBuilder();
int lastPos = 0;
int argColorIdx = 0;
for (final ParsedCommandNode<S> node : results.getContext().getLastChild().getNodes()) {
// Sometimes Brigadier will spit out ranges that are invalid for the current input string????
final int start = Math.min(node.getRange().getStart(), reader.getTotalLength());
final int end = Math.min(node.getRange().getEnd(), reader.getTotalLength());
if (lastPos < start) {
builder.append(reader.getString(), lastPos, start);
}
builder.append(reader.getString().substring(start, end), BrigadierHighlighter.ARGUMENT_STYLES[argColorIdx]);
argColorIdx = (argColorIdx + 1) % BrigadierHighlighter.ARGUMENT_STYLES.length;
lastPos = end;
}
if (lastPos < reader.getTotalLength()) {
builder.append(reader.getString().substring(lastPos), results.getExceptions().isEmpty() ? BrigadierHighlighter.LITERAL_STYLE : BrigadierHighlighter.ERROR_STYLE);
}
return builder.toAttributedString();
} catch (final Exception ex) {
SpongeCommon.logger().warn("Error while highlighting console command line", ex);
return new AttributedString(buffer);
}
}
use of org.jline.utils.AttributedString in project felix by apache.
the class Posix method toColumn.
private void toColumn(CommandSession session, Process process, PrintStream out, Stream<String> ansi, boolean horizontal) {
Terminal terminal = Shell.getTerminal(session);
int width = process.isTty(1) ? terminal.getWidth() : 80;
List<AttributedString> strings = ansi.map(AttributedString::fromAnsi).collect(Collectors.toList());
if (!strings.isEmpty()) {
int max = strings.stream().mapToInt(AttributedString::columnLength).max().getAsInt();
int c = Math.max(1, width / max);
while (c > 1 && c * max + (c - 1) >= width) {
c--;
}
int columns = c;
int lines = (strings.size() + columns - 1) / columns;
IntBinaryOperator index;
if (horizontal) {
index = (i, j) -> i * columns + j;
} else {
index = (i, j) -> j * lines + i;
}
AttributedStringBuilder sb = new AttributedStringBuilder();
for (int i = 0; i < lines; i++) {
for (int j = 0; j < columns; j++) {
int idx = index.applyAsInt(i, j);
if (idx < strings.size()) {
AttributedString str = strings.get(idx);
boolean hasRightItem = j < columns - 1 && index.applyAsInt(i, j + 1) < strings.size();
sb.append(str);
if (hasRightItem) {
for (int k = 0; k <= max - str.length(); k++) {
sb.append(' ');
}
}
}
}
sb.append('\n');
}
out.print(sb.toAnsi(terminal));
}
}
use of org.jline.utils.AttributedString in project flink by apache.
the class CliChangelogResultView method computeHeaderLines.
@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);
}
use of org.jline.utils.AttributedString in project flink by apache.
the class CliTableResultView method computeHeaderLines.
@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;
}
// middle
final StringBuilder middleBuilder = new StringBuilder();
middleBuilder.append(CliStrings.RESULT_PAGE);
middleBuilder.append(' ');
if (page == LAST_PAGE) {
middleBuilder.append(CliStrings.RESULT_LAST_PAGE);
} else {
middleBuilder.append(page);
}
middleBuilder.append(CliStrings.RESULT_PAGE_OF);
middleBuilder.append(pageCount);
final String middle = middleBuilder.toString();
// 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 totalLeftSpace = getWidth() - middle.length();
final int leftSpace = totalLeftSpace / 2 - left.length();
statusLine.append(left);
repeatChar(statusLine, ' ', leftSpace);
statusLine.append(middle);
final int rightSpacing = getWidth() - statusLine.length() - right.length();
repeatChar(statusLine, ' ', rightSpacing);
statusLine.append(right);
return Arrays.asList(statusLine.toAttributedString(), AttributedString.EMPTY);
}
use of org.jline.utils.AttributedString in project flink by apache.
the class CliUtils method formatTwoLineHelpOptions.
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());
}
Aggregations