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);
}
}
}
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;
}
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);
}
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
}
}
Aggregations