org.jline.reader.History Java Examples

The following examples show how to use org.jline.reader.History. 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: ShellUtils.java    From rug-cli with GNU General Public License v3.0 7 votes vote down vote up
public static LineReader lineReader(File historyPath, Optional<SignalHandler> handler,
        Completer... completers) {
    // Protect the history file as may contain sensitive information
    FileUtils.setPermissionsToOwnerOnly(historyPath);

    // Create JLine LineReader
    History history = new DefaultHistory();
    LineReader reader = LineReaderBuilder.builder().terminal(terminal(handler)).history(history)
            .parser(new DefaultParser()).variable(LineReader.HISTORY_FILE, historyPath)
            .completer(new AggregateCompleter(completers)).highlighter(new DefaultHighlighter())
            .build();
    history.attach(reader);

    setOptions(reader);

    return reader;
}
 
Example #2
Source File: Commands.java    From sqlline with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String calculateCommand(int currentOffset, Set<Integer> offsets) {
  if (!offsets.add(currentOffset)) {
    throw new IllegalArgumentException(
        "Cycled rerun of commands from history " + offsets);
  }

  History history = sqlLine.getLineReader().getHistory();
  Iterator<History.Entry> iterator = currentOffset > 0
      ? history.iterator(currentOffset - 1)
      : history.reverseIterator(history.size() - 1 + currentOffset);
  String command = iterator.next().line();
  if (command.trim().startsWith("!/") || command.startsWith("!rerun")) {
    String[] cmd = sqlLine.split(command);
    if (cmd.length > 2 || (cmd.length == 2 && !cmd[1].matches("-?\\d+"))) {
      return command;
    }
    int offset = cmd.length == 1 ? -1 : Integer.parseInt(cmd[1]);
    if (history.size() < offset || history.size() - 1 < -offset) {
      return command;
    }
    return calculateCommand(offset, offsets);
  }
  return command;
}
 
Example #3
Source File: LogServiceShell.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  final Terminal terminal = TerminalBuilder.builder()
      .system(true)
      .build();

  History defaultHistory = new DefaultHistory();
  // Register a shutdown-hook per JLine documentation to save history
  Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    try {
      defaultHistory.save();
    } catch (IOException e) {
      LOG.debug("Failed to save terminal history", e);
    }
  }));

  final LineReader lineReader = LineReaderBuilder.builder()
      .terminal(terminal)
      .highlighter(new DefaultHighlighter())
      .history(defaultHistory)
      .build();

  LogServiceShellOpts opts = new LogServiceShellOpts();
  JCommander.newBuilder()
      .addObject(opts)
      .build()
      .parse(args);

  try (LogServiceClient logServiceClient = new LogServiceClient(opts.getMetaQuorum())) {
    LogServiceShell client = new LogServiceShell(terminal, lineReader, logServiceClient);
    client.run();
  }
}
 
Example #4
Source File: ConsoleIO.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Build JLine line reader with default history
 *
 * @return line reader
 */
private LineReader buildLineReader() {
	History history = new DefaultHistory();
	LineReader reader = LineReaderBuilder.builder().terminal(this.terminal).history(history).build();

	Path file = Paths.get(appInfo.getDataDirectory().toString(), "history.txt");
	reader.setVariable(LineReader.HISTORY_FILE, file);

	return reader;
}
 
Example #5
Source File: LogServiceShell.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  final Terminal terminal = TerminalBuilder.builder()
      .system(true)
      .build();

  History defaultHistory = new DefaultHistory();
  // Register a shutdown-hook per JLine documentation to save history
  Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    try {
      defaultHistory.save();
    } catch (IOException e) {
      LOG.debug("Failed to save terminal history", e);
    }
  }));

  final LineReader lineReader = LineReaderBuilder.builder()
      .terminal(terminal)
      .highlighter(new DefaultHighlighter())
      .history(defaultHistory)
      .build();

  LogServiceShellOpts opts = new LogServiceShellOpts();
  JCommander.newBuilder()
      .addObject(opts)
      .build()
      .parse(args);

  try (LogServiceClient logServiceClient = new LogServiceClient(opts.metaQuorum)) {
    LogServiceShell client = new LogServiceShell(terminal, lineReader, logServiceClient);
    client.run();
  }
}
 
Example #6
Source File: Commands.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void rerun(String line, DispatchCallback callback) {
  String[] cmd = sqlLine.split(line);
  History history = sqlLine.getLineReader().getHistory();
  int size = history.size();
  if (cmd.length > 2 || (cmd.length == 2 && !cmd[1].matches("-?\\d+"))) {
    if (size == 0) {
      sqlLine.error("Usage: rerun <offset>, history should not be empty");
    } else {
      sqlLine.error("Usage: rerun <offset>, available range of offset is -"
          + (size - 1) + ".." + size);
    }
    callback.setToFailure();
    return;
  }

  int offset = cmd.length == 1 ? -1 : Integer.parseInt(cmd[1]);
  if (size < offset || size - 1 < -offset || offset == 0) {
    if (offset == 0) {
      sqlLine.error(
          "Usage: rerun <offset>, offset should be positive or negative");
    }
    if (size == 0) {
      sqlLine.error("Usage: rerun <offset>, history should not be empty");
    } else {
      sqlLine.error("Usage: rerun <offset>, available range of offset is -"
          + (size - 1) + ".." + size);
    }
    callback.setToFailure();
    return;
  }

  sqlLine.dispatch(calculateCommand(offset, new HashSet<>()), callback);
}
 
Example #7
Source File: InputReader.java    From presto with Apache License 2.0 4 votes vote down vote up
public History getHistory()
{
    return reader.getHistory();
}
 
Example #8
Source File: JLineReader.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
@Override
public String expandHistory(History history, String line) {
  return line;
}
 
Example #9
Source File: JLineReader.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends History.Entry> getHistory() {
  return lineReader.getHistory();
}
 
Example #10
Source File: TestLineReader.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends History.Entry> getHistory() {
  return new ArrayList<>();
}
 
Example #11
Source File: CleanstoneCommandHistory.java    From Cleanstone with MIT License 4 votes vote down vote up
@Primary
@Bean
public History history(LineReader lineReader) {
    lineReader.setVariable(LineReader.HISTORY_FILE, Paths.get("history.log"));
    return new DefaultHistory(lineReader);
}
 
Example #12
Source File: SshShellHistoryAutoConfiguration.java    From ssh-shell-spring-boot with Apache License 2.0 4 votes vote down vote up
@Bean
@Primary
public History history(LineReader lineReader, @Qualifier(HISTORY_FILE) File historyFile) {
    lineReader.setVariable(LineReader.HISTORY_FILE, historyFile.toPath());
    return new DefaultHistory(lineReader);
}
 
Example #13
Source File: SshShellHelper.java    From ssh-shell-spring-boot with Apache License 2.0 2 votes vote down vote up
/**
 * Return the terminal reader history
 *
 * @return history
 */
public History getHistory() {
    return new DefaultHistory(this.reader());
}
 
Example #14
Source File: LineReader.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 votes vote down vote up
Iterable<? extends History.Entry> getHistory();