use of org.jline.utils.AttributedStringBuilder in project karaf by apache.
the class AnsiSplitter method substring.
public static String substring(String text, int begin, int end, int tabs) throws IOException {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.tabs(tabs);
sb.appendAnsi(text);
return sb.columnSubSequence(begin, end).toAnsi();
}
use of org.jline.utils.AttributedStringBuilder in project karaf by apache.
the class FileCompleter method getDisplay.
protected String getDisplay(Terminal terminal, Path p) {
// TODO: use $LS_COLORS for output
String name = p.getFileName().toString();
if (Files.isDirectory(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.append(OS_IS_WINDOWS ? "\\\\" : "/");
name = sb.toAnsi(terminal);
} else if (Files.isSymbolicLink(p)) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.BOLD.foreground(AttributedStyle.RED));
sb.append(name);
sb.style(AttributedStyle.DEFAULT);
sb.append("@");
name = sb.toAnsi(terminal);
}
return name;
}
use of org.jline.utils.AttributedStringBuilder 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.AttributedStringBuilder in project felix by apache.
the class Shell method runShell.
private Object runShell(final CommandSession session, Terminal terminal, LineReader reader) throws InterruptedException {
AtomicBoolean reading = new AtomicBoolean();
session.setJobListener((job, previous, current) -> {
if (previous == Status.Background || current == Status.Background || previous == Status.Suspended || current == Status.Suspended) {
int width = terminal.getWidth();
String status = current.name().toLowerCase();
terminal.writer().write(getStatusLine(job, width, status));
terminal.flush();
if (reading.get() && !stopping.get()) {
reader.callWidget(LineReader.REDRAW_LINE);
reader.callWidget(LineReader.REDISPLAY);
}
}
});
SignalHandler intHandler = terminal.handle(Signal.INT, s -> {
Job current = session.foregroundJob();
if (current != null) {
current.interrupt();
}
});
SignalHandler suspHandler = terminal.handle(Signal.TSTP, s -> {
Job current = session.foregroundJob();
if (current != null) {
current.suspend();
}
});
Object result = null;
try {
while (!stopping.get()) {
try {
reading.set(true);
try {
String prompt = Shell.getPrompt(session);
String rprompt = Shell.getRPrompt(session);
if (stopping.get()) {
break;
}
reader.readLine(prompt, rprompt, null, null);
} finally {
reading.set(false);
}
ParsedLine parsedLine = reader.getParsedLine();
if (parsedLine == null) {
throw new EndOfFileException();
}
try {
result = session.execute(((ParsedLineImpl) parsedLine).program());
// set $_ to last result
session.put(Shell.VAR_RESULT, result);
if (result != null && !Boolean.FALSE.equals(session.get(".Gogo.format"))) {
System.out.println(session.format(result, Converter.INSPECT));
}
} catch (Exception e) {
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(sb.style().foreground(AttributedStyle.RED));
sb.append(e.toString());
sb.style(sb.style().foregroundDefault());
terminal.writer().println(sb.toAnsi(terminal));
terminal.flush();
session.put(Shell.VAR_EXCEPTION, e);
}
waitJobCompletion(session);
} catch (UserInterruptException e) {
// continue;
} catch (EndOfFileException e) {
reader.getHistory().save();
break;
}
}
} finally {
terminal.handle(Signal.INT, intHandler);
terminal.handle(Signal.TSTP, suspHandler);
}
return result;
}
use of org.jline.utils.AttributedStringBuilder in project karaf by apache.
the class InfoFeatureCommand method colorize.
private static String colorize(Session session, String xml) {
Terminal terminal = (Terminal) session.get(".jline.terminal");
Map<String, String> colorMap = getColorMap(session, "XML", DEFAULT_XML_COLORS);
Map<Pattern, String> patternColors = new LinkedHashMap<>();
patternColors.put(Pattern.compile("(</?[a-z]*)\\s?>?"), "el");
patternColors.put(Pattern.compile("(/?>)"), "el");
patternColors.put(Pattern.compile("\\s([a-z-]*)\\="), "at");
patternColors.put(Pattern.compile("[a-z-]*\\=(\"[^\"]*\")"), "av");
patternColors.put(Pattern.compile("(<!--.*-->)"), "cm");
patternColors.put(Pattern.compile("(\\<!\\[CDATA\\[).*"), "cd");
patternColors.put(Pattern.compile(".*(]]>)"), "cd");
String[] styles = new String[xml.length()];
// Match all regexes on this snippet, store positions
for (Map.Entry<Pattern, String> entry : patternColors.entrySet()) {
Matcher matcher = entry.getKey().matcher(xml);
while (matcher.find()) {
int s = matcher.start(1);
int e = matcher.end();
String c = entry.getValue();
Arrays.fill(styles, s, e, c);
}
}
AttributedStringBuilder asb = new AttributedStringBuilder();
String prev = null;
for (int i = 0; i < xml.length(); i++) {
String s = styles[i];
if (!Objects.equals(s, prev)) {
applyStyle(asb, colorMap, s);
prev = s;
}
asb.append(xml.charAt(i));
}
return asb.toAnsi(terminal);
}
Aggregations