use of org.jline.terminal.Terminal in project flink by apache.
the class CliClientTest method verifySqlCompletion.
private void verifySqlCompletion(String statement, int position, List<String> expectedHints) throws IOException {
final MockExecutor mockExecutor = new MockExecutor();
String sessionId = mockExecutor.openSession("test-session");
final SqlCompleter completer = new SqlCompleter(sessionId, mockExecutor);
final SqlMultiLineParser parser = new SqlMultiLineParser(new SqlCommandParserImpl(mockExecutor, sessionId));
try (Terminal terminal = TerminalUtils.createDumbTerminal()) {
final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();
final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
final List<Candidate> candidates = new ArrayList<>();
final List<String> results = new ArrayList<>();
completer.complete(reader, parsedLine, candidates);
candidates.forEach(item -> results.add(item.value()));
assertThat(results.containsAll(expectedHints)).isTrue();
assertThat(statement).isEqualTo(mockExecutor.receivedStatement);
assertThat(position).isEqualTo(mockExecutor.receivedPosition);
}
}
use of org.jline.terminal.Terminal in project flink by apache.
the class CliView method ensureTerminalFullScreen.
private void ensureTerminalFullScreen() {
final Terminal terminal = client.getTerminal();
terminal.puts(Capability.enter_ca_mode);
terminal.puts(Capability.keypad_xmit);
terminal.puts(Capability.cursor_invisible);
}
use of org.jline.terminal.Terminal in project flink by apache.
the class CliClient method executeFile.
/**
* Execute content from Sql file and prints status information and/or errors on the terminal.
*
* @param content SQL file content
*/
private boolean executeFile(String content, OutputStream outputStream, ExecutionMode mode) {
terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EXECUTE_FILE).toAnsi());
// append line delimiter
InputStream inputStream = new ByteArrayInputStream(SqlMultiLineParser.formatSqlFile(content).getBytes());
Terminal dumbTerminal = TerminalUtils.createDumbTerminal(inputStream, outputStream);
try {
LineReader lineReader = createLineReader(dumbTerminal);
return getAndExecuteStatements(lineReader, mode);
} catch (Throwable e) {
printExecutionException(e);
return false;
} finally {
try {
dumbTerminal.close();
} catch (IOException e) {
// ignore
}
}
}
use of org.jline.terminal.Terminal in project alluxio by Alluxio.
the class HelpCommand method printCommandInfo.
/**
* Prints the info about a command to the given print writer.
*
* @param command command to print info
* @param pw where to print the info
*/
public static void printCommandInfo(Command command, PrintWriter pw) {
String description = String.format("%s: %s", command.getCommandName(), command.getDescription());
int width = 80;
try (Terminal terminal = TerminalBuilder.terminal()) {
width = terminal.getWidth();
} catch (Exception e) {
// In case the terminal builder failed to decide terminal type, use default width
}
// Use default value if terminal width is assigned 0
if (width == 0) {
width = 80;
}
HELP_FORMATTER.printWrapped(pw, width, description);
HELP_FORMATTER.printUsage(pw, width, command.getUsage());
if (command.getOptions().getOptions().size() > 0) {
HELP_FORMATTER.printOptions(pw, width, command.getOptions(), HELP_FORMATTER.getLeftPadding(), HELP_FORMATTER.getDescPadding());
}
}
use of org.jline.terminal.Terminal in project Payara by payara.
the class MultimodeCommand method executeCommand.
@Override
protected int executeCommand() throws CommandException, CommandValidationException {
LineReader reader = null;
// restore echo flag, saved in validate
programOpts.setEcho(echo);
try {
if (file == null) {
System.out.println(strings.get("multimodeIntro"));
Completer completer = getAllCommandsCompleter();
Terminal asadminTerminal = TerminalBuilder.builder().name(ASADMIN).system(true).encoding(encoding != null ? Charset.forName(encoding) : Charset.defaultCharset()).build();
reader = newLineReaderBuilder().terminal(asadminTerminal).completer(completer).build();
reader.unsetOpt(LineReader.Option.INSERT_TAB);
} else {
printPrompt = false;
if (!file.canRead()) {
throw new CommandException("File: " + file + " can not be read");
}
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
return;
}
@Override
public void write(byte[] b) throws IOException {
return;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
return;
}
};
Terminal asadminTerminal = new ExternalTerminal(ASADMIN, "", new FileInputStream(file), out, encoding != null ? Charset.forName(encoding) : Charset.defaultCharset());
reader = newLineReaderBuilder().terminal(asadminTerminal).build();
}
return executeCommands(reader);
} catch (IOException e) {
throw new CommandException(e);
} finally {
try {
if (file != null && reader != null && reader.getTerminal() != null) {
reader.getTerminal().close();
}
} catch (Exception e) {
// ignore it
}
}
}
Aggregations