org.jline.reader.EndOfFileException Java Examples

The following examples show how to use org.jline.reader.EndOfFileException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Console.java    From xraft with MIT License 6 votes vote down vote up
void start() {
    commandContext.setRunning(true);
    showInfo();
    String line;
    while (commandContext.isRunning()) {
        try {
            line = reader.readLine(PROMPT);
            if (line.trim().isEmpty())
                continue;
            dispatchCommand(line);
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        } catch (EndOfFileException ignored) {
            break;
        }
    }
}
 
Example #2
Source File: Cli.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to read a logical line of input from the user. Can span multiple physical lines, as
 * long as all but the last end with '\\'.
 *
 * @return The parsed, logical line.
 * @throws EndOfFileException If there is no more input available from the user.
 * @throws IOException        If any other I/O error occurs.
 */
private String readLine() throws IOException {
  while (true) {
    try {
      String result = terminal.getLineReader().readLine();
      // A 'dumb' terminal (the kind used at runtime if a 'system' terminal isn't available) will
      // return null on EOF and user interrupt, instead of throwing the more fine-grained
      // exceptions. This null-check helps ensure that, upon encountering EOF, even a 'dumb'
      // terminal will be able to exit intelligently.
      if (result == null) {
        throw new EndOfFileException();
      } else {
        return result.trim();
      }
    } catch (UserInterruptException exception) {
      // User hit ctrl-C, just clear the current line and try again.
      terminal.writer().println("^C");
      terminal.flush();
    }
  }
}
 
Example #3
Source File: SimpleTerminalConsole.java    From TerminalConsoleAppender with MIT License 6 votes vote down vote up
private void readCommands(Terminal terminal) {
    LineReader reader = buildReader(LineReaderBuilder.builder().terminal(terminal));
    TerminalConsoleAppender.setReader(reader);

    try {
        String line;
        while (isRunning()) {
            try {
                line = reader.readLine("> ");
            } catch (EndOfFileException ignored) {
                // Continue reading after EOT
                continue;
            }

            if (line == null) {
                break;
            }

            processInput(line);
        }
    } catch (UserInterruptException e) {
        shutdown();
    } finally {
        TerminalConsoleAppender.setReader(null);
    }
}
 
Example #4
Source File: Console.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Start the interactive console, return error code on exit
 *
 * @throws IOException
 */
public void start() throws IOException {
	loadSettings();
	loadHistory();

	consoleIO.writeln(APP_CFG.getFullName());
	consoleIO.writeln("Working dir: " + settingMap.get(WorkDir.NAME).getAsString());
	consoleIO.writeln("Type 'help' for help.");

	int exitCode = 0;
	try {
		boolean exitFlag = false;
		while (!exitFlag) {
			final String command = consoleIO.readCommand();
			if (command == null) {
				// EOF
				break;
			}
			exitFlag = executeCommand(command);
			if (exitOnError && consoleIO.wasErrorWritten()) {
				exitCode = 2;
				exitFlag = true;
			}
		}
	} catch (UserInterruptException | EndOfFileException e) {
		exitCode = 0;
	} finally {
		disconnect.execute(false);
	}

	saveSettings();
	saveHistory();

	if (exitCode != 0) {
		System.exit(exitCode);
	}
	consoleIO.writeln("Bye");
	consoleIO.getOutputStream().close();
}
 
Example #5
Source File: ConsoleIO.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read a command from input
 *
 * @return one line of input, or null on error
 * @throws IOException
 */
protected String readCommand() throws IOException {
	try {
		String line = input.readLine(getPrompt());
		if (line == null) {
			return null;
		}
		line = line.trim();
		if (line.endsWith(".")) {
			line = line.substring(0, line.length() - 1);
		}
		return line;
	} catch (EndOfFileException e) {
		return null;
	}
}
 
Example #6
Source File: LoginCommand.java    From rug-cli with GNU General Public License v3.0 6 votes vote down vote up
private void login(String username, String code, Settings settings) {
    printBanner();

    LineReader reader = ShellUtils.lineReader(null, Optional.empty());
    try {
        if (username == null) {
            username = reader.readLine(getPrompt("Username"));
        }
        String password = reader.readLine(getPrompt("Password"), new Character('*'));
        postForTokenAndHandleResponse(username, password, code, settings, reader);
    }
    catch (EndOfFileException | UserInterruptException e) {
        log.error("Canceled!");
    }
    finally {
        ShellUtils.shutdown(reader);
    }
}
 
Example #7
Source File: Cli.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
public void runNonInteractively(String input) throws Exception {
  // Allow exceptions to halt execution of the Ksql script as soon as the first one is encountered
  for (String logicalLine : getLogicalLines(input)) {
    try {
      handleLine(logicalLine);
    } catch (EndOfFileException exception) {
      // Swallow these silently; they're thrown by the exit command to terminate the REPL
      return;
    }
  }
}
 
Example #8
Source File: SshShellRunnable.java    From ssh-shell-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public Input readInput() {
    SshContext ctx = SSH_THREAD_CONTEXT.get();
    if (ctx != null) {
        ctx.setPostProcessorsList(null);
    }
    try {
        return super.readInput();
    } catch (EndOfFileException e) {
        throw new ExitRequest(1);
    }
}
 
Example #9
Source File: Shell.java    From joinery with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String readLine(final String prompt)
throws IOException {
    try {
        return console.readLine(prompt);
    } catch (EndOfFileException eof) {
        return null;
    }
}
 
Example #10
Source File: Console.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(String commandStrippedLine) throws IOException {
  throw new EndOfFileException();
}
 
Example #11
Source File: InteractiveMode.java    From rdflint with MIT License 4 votes vote down vote up
/**
 * execute interacitve mode.
 */
void execute(Map<String, String> cmdOptions) throws IOException {
  // initialize graph model
  Model m = ModelFactory.createDefaultModel();

  // initialize jline
  Terminal terminal = TerminalBuilder.builder()
      .system(true)
      .build();
  LineReader lineReader = LineReaderBuilder.builder()
      .terminal(terminal)
      .completer(new InteractiveCompleter(m))
      .parser(new InteractiveParser())
      .build();

  String welcome = MessageFormat
      .format(messages.getString("interactivemode.welcome"), RdfLint.VERSION);
  System.out.println(welcome);// NOPMD

  RdfLintParameters params = new RdfLintParameters();
  try {
    // load configurations
    params = ConfigurationLoader.loadParameters(cmdOptions);
    // load rdf
    m.removeAll();
    m.add(DatasetLoader.loadRdfSet(params, params.getTargetDir()));
  } catch (Exception ex) {
    System.out.println(ex.getLocalizedMessage()); // NOPMD
  }

  while (true) {
    String line;

    try {
      line = lineReader.readLine("SPARQL> ");
    } catch (UserInterruptException | EndOfFileException e) {
      return;
    }

    if (!interactiveCommand(System.out, line, params, cmdOptions, m)) {
      return;
    }
  }
}
 
Example #12
Source File: ShellCommandRunner.java    From rug-cli with GNU General Public License v3.0 4 votes vote down vote up
private void exit(ArtifactDescriptor artifact, List<ArtifactDescriptor> dependencies) {
    // Exit the current shell
    invokeCommand(new String[] { "exit" }, artifact, dependencies);
    throw new EndOfFileException();
}