Search in sources :

Example 6 with AttributedStringBuilder

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();
}
Also used : AttributedStringBuilder(org.jline.utils.AttributedStringBuilder)

Example 7 with AttributedStringBuilder

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;
}
Also used : AttributedStringBuilder(org.jline.utils.AttributedStringBuilder)

Example 8 with AttributedStringBuilder

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));
    }
}
Also used : AttributedString(org.jline.utils.AttributedString) IntBinaryOperator(java.util.function.IntBinaryOperator) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Terminal(org.jline.terminal.Terminal)

Example 9 with AttributedStringBuilder

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;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EndOfFileException(org.jline.reader.EndOfFileException) ParsedLine(org.jline.reader.ParsedLine) SignalHandler(org.jline.terminal.Terminal.SignalHandler) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Job(org.apache.felix.service.command.Job) UserInterruptException(org.jline.reader.UserInterruptException) EndOfFileException(org.jline.reader.EndOfFileException) IOException(java.io.IOException) UserInterruptException(org.jline.reader.UserInterruptException)

Example 10 with AttributedStringBuilder

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Terminal(org.jline.terminal.Terminal) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)10 AttributedString (org.jline.utils.AttributedString)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Terminal (org.jline.terminal.Terminal)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 IntBinaryOperator (java.util.function.IntBinaryOperator)1 EOFError (org.apache.felix.gogo.runtime.EOFError)1 Program (org.apache.felix.gogo.runtime.Parser.Program)1 Statement (org.apache.felix.gogo.runtime.Parser.Statement)1 SyntaxError (org.apache.felix.gogo.runtime.SyntaxError)1 Token (org.apache.felix.gogo.runtime.Token)1 Function (org.apache.felix.service.command.Function)1 Job (org.apache.felix.service.command.Job)1