use of org.jline.reader.UserInterruptException in project nifi by apache.
the class CLIMain method runInteractiveCLI.
/**
* Runs the interactive CLI.
*
* @throws IOException if an error occurs
*/
private static void runInteractiveCLI() throws IOException {
// Logger.getLogger("org.jline").setLevel(Level.FINE);
try (final Terminal terminal = TerminalBuilder.builder().name(SHELL_NAME).system(true).nativeSignals(true).signalHandler(Terminal.SignalHandler.SIG_IGN).build();
final PrintStream output = new PrintStream(terminal.output(), true)) {
printHeader(BANNER_FILE, output);
final Context context = createContext(output, true);
final Map<String, Command> topLevelCommands = CommandFactory.createTopLevelCommands(context);
final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
final CommandProcessor commandProcessor = new CommandProcessor(topLevelCommands, commandGroups, context);
final Completer completer = new CLICompleter(topLevelCommands.values(), commandGroups.values());
final LineReader reader = LineReaderBuilder.builder().appName(SHELL_NAME).terminal(terminal).completer(completer).build();
reader.setOpt(LineReader.Option.AUTO_FRESH_LINE);
reader.unsetOpt(LineReader.Option.INSERT_TAB);
while (true) {
try {
final String line = reader.readLine(PROMPT);
if (StringUtils.isBlank(line)) {
continue;
}
final ParsedLine parsedLine = reader.getParsedLine();
final String[] parsedArgs = parsedLine.words().toArray(new String[parsedLine.words().size()]);
commandProcessor.process(parsedArgs);
} catch (UserInterruptException e) {
// Ignore
} catch (EndOfFileException e) {
return;
}
}
}
}
use of org.jline.reader.UserInterruptException in project herddb by diennea.
the class HerdDBCLI method runSqlConsole.
private static void runSqlConsole(Connection connection, Statement statement, boolean pretty) throws IOException {
Terminal terminal = TerminalBuilder.builder().system(true).build();
LineReader reader = LineReaderBuilder.builder().history(new DefaultHistory()).terminal(terminal).build();
String prompt = "herd: ";
while (true) {
String line = null;
try {
line = reader.readLine(prompt);
if (line == null) {
return;
}
executeStatement(true, true, false, false, line, statement, null, false, pretty);
} catch (UserInterruptException | EndOfFileException e) {
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of org.jline.reader.UserInterruptException 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.reader.UserInterruptException in project SpongeVanilla by SpongePowered.
the class MixinConsoleHandler method onRun.
@Inject(method = "run", at = @At("HEAD"), cancellable = true, remap = false)
private void onRun(CallbackInfo ci) {
final Terminal terminal = TerminalConsoleAppender.getTerminal();
if (terminal != null) {
LineReader reader = LineReaderBuilder.builder().appName("SpongeVanilla").terminal(terminal).completer(new ConsoleCommandCompleter(this.server)).build();
reader.unsetOpt(LineReader.Option.INSERT_TAB);
TerminalConsoleAppender.setReader(reader);
try {
String line;
while (!this.server.isServerStopped() && this.server.isServerRunning()) {
try {
line = reader.readLine("> ");
} catch (EndOfFileException e) {
// Continue reading after EOT
continue;
}
if (line == null) {
break;
}
line = line.trim();
if (!line.isEmpty()) {
this.server.addPendingCommand(line, this.server);
}
}
} catch (UserInterruptException e) {
this.server.initiateShutdown();
} finally {
TerminalConsoleAppender.setReader(null);
}
ci.cancel();
}
}
use of org.jline.reader.UserInterruptException in project batfish by batfish.
the class Client method runInteractive.
private void runInteractive() {
SignalHandler handler = signal -> _logger.debugf("Client: Ignoring signal: %s\n", signal);
Signal.handle(new Signal("INT"), handler);
try {
while (!_exit) {
try {
String rawLine = _reader.readLine("batfish> ");
if (rawLine == null) {
break;
}
processCommand(rawLine);
} catch (UserInterruptException e) {
continue;
}
}
} catch (EndOfFileException e) {
// ignored
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
_reader.getHistory().save();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Aggregations