Search in sources :

Example 36 with Terminal

use of org.jline.terminal.Terminal in project herddb by diennea.

the class HerdDBCLI method runSqlConsole.

private static void runSqlConsole(Statement statement, boolean pretty, boolean verbose, boolean persistHistory) throws IOException, SQLException {
    Terminal terminal = TerminalBuilder.builder().system(true).build();
    LineReaderBuilder readerBuilder = LineReaderBuilder.builder().history(new DefaultHistory()).terminal(terminal);
    if (persistHistory) {
        String historyDirectory = System.getProperty("user.home", ".");
        File historyFile = new File(historyDirectory, ".herddb.cli.history");
        if (verbose) {
            System.out.println("Storing SQL Console History to " + historyFile.getAbsolutePath());
        }
        readerBuilder = readerBuilder.variable(LineReader.HISTORY_FILE, historyFile);
        readerBuilder = readerBuilder.variable(LineReader.HISTORY_FILE_SIZE, 100);
    }
    LineReader reader = readerBuilder.build();
    String prompt = "herddb(" + statement.getConnection().getSchema() + "): ";
    while (true) {
        String line;
        try {
            line = reader.readLine(prompt);
            if (line == null) {
                return;
            }
            executeStatement(verbose, true, false, false, line, statement, null, false, pretty);
            prompt = "herddb(" + statement.getConnection().getSchema() + "): ";
        } catch (UserInterruptException | EndOfFileException e) {
            return;
        } catch (Exception e) {
            prettyPrintException(verbose, e);
        }
    }
}
Also used : EndOfFileException(org.jline.reader.EndOfFileException) LineReaderBuilder(org.jline.reader.LineReaderBuilder) LineReader(org.jline.reader.LineReader) DefaultHistory(org.jline.reader.impl.history.DefaultHistory) UserInterruptException(org.jline.reader.UserInterruptException) Terminal(org.jline.terminal.Terminal) File(java.io.File) ScriptException(javax.script.ScriptException) EndOfFileException(org.jline.reader.EndOfFileException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UserInterruptException(org.jline.reader.UserInterruptException) ParseException(org.apache.commons.cli.ParseException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException)

Example 37 with Terminal

use of org.jline.terminal.Terminal in project karaf by apache.

the class ShellTable method getEncoding.

private Charset getEncoding(PrintStream ps) {
    if (ps.getClass() == ThreadPrintStream.class) {
        try {
            return ((Terminal) Job.Utils.current().session().get(".jline.terminal")).encoding();
        } catch (Throwable t) {
        // ignore
        }
        try {
            ps = (PrintStream) ps.getClass().getMethod("getCurrent").invoke(ps);
        } catch (Throwable t) {
        // ignore
        }
    }
    try {
        Field f = ps.getClass().getDeclaredField("charOut");
        f.setAccessible(true);
        OutputStreamWriter osw = (OutputStreamWriter) f.get(ps);
        return Charset.forName(osw.getEncoding());
    } catch (Throwable t) {
    // ignore
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) OutputStreamWriter(java.io.OutputStreamWriter) Terminal(org.jline.terminal.Terminal)

Example 38 with Terminal

use of org.jline.terminal.Terminal 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)

Example 39 with Terminal

use of org.jline.terminal.Terminal in project karaf by apache.

the class FileCompleter method completeCandidates.

public void completeCandidates(final Session session, CommandLine commandLine, final List<Candidate> candidates) {
    // buffer can be null
    if (candidates == null) {
        return;
    }
    String buffer = commandLine.getCursorArgument().substring(0, commandLine.getArgumentPosition());
    if (OS_IS_WINDOWS) {
        buffer = buffer.replaceAll("/", File.separator);
    }
    Terminal terminal = (Terminal) session.get(".jline.terminal");
    Path current;
    String curBuf;
    int lastSep = buffer.lastIndexOf(separator());
    if (lastSep >= 0) {
        curBuf = buffer.substring(0, lastSep + 1);
        if (curBuf.startsWith("~")) {
            if (curBuf.startsWith("~" + separator())) {
                current = getUserHome().resolve(curBuf.substring(2));
            } else {
                current = getUserHome().getParent().resolve(curBuf.substring(1));
            }
        } else {
            current = getUserDir().resolve(curBuf);
        }
    } else {
        curBuf = "";
        current = getUserDir();
    }
    try {
        Files.newDirectoryStream(current, this::accept).forEach(p -> {
            String value = curBuf + p.getFileName().toString();
            if (Files.isDirectory(p)) {
                String s = OS_IS_WINDOWS ? "\\\\" : "/";
                candidates.add(new Candidate(value + s, getDisplay(terminal, p), null, null, s, null, false));
            } else {
                candidates.add(new Candidate(value, getDisplay(terminal, p), null, null, null, null, true));
            }
        });
    } catch (IOException e) {
    // Ignore
    }
}
Also used : Path(java.nio.file.Path) Candidate(org.apache.karaf.shell.api.console.Candidate) IOException(java.io.IOException) Terminal(org.jline.terminal.Terminal)

Aggregations

Terminal (org.jline.terminal.Terminal)39 LineReader (org.jline.reader.LineReader)14 IOException (java.io.IOException)13 InputStream (java.io.InputStream)8 EndOfFileException (org.jline.reader.EndOfFileException)8 UserInterruptException (org.jline.reader.UserInterruptException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Attributes (org.jline.terminal.Attributes)7 PrintStream (java.io.PrintStream)6 Path (java.nio.file.Path)6 CommandSession (org.apache.felix.service.command.CommandSession)6 OutputStream (java.io.OutputStream)5 ArrayList (java.util.ArrayList)5 InputStreamReader (java.io.InputStreamReader)4 Reader (java.io.Reader)4 ParsedLine (org.jline.reader.ParsedLine)4 BufferedReader (java.io.BufferedReader)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3