org.jline.reader.impl.history.DefaultHistory Java Examples

The following examples show how to use org.jline.reader.impl.history.DefaultHistory. 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 Project: rug-cli   Author: atomist-attic   File: ShellUtils.java    License: 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 Project: ksql-fork-with-deep-learning-function   Author: kaiwaehner   File: JLineReader.java    License: Apache License 2.0 5 votes vote down vote up
public JLineReader(Terminal terminal) {
  // The combination of parser/expander here allow for multiple-line commands connected by '\\'
  DefaultParser parser = new DefaultParser();
  parser.setEofOnEscapedNewLine(true);
  parser.setQuoteChars(new char[0]);
  parser.setEscapeChars(new char[]{'\\'});

  final Expander expander = new NoOpExpander();
  // TODO: specify a completer to use here via a call to LineReaderBuilder.completer()
  this.lineReader = LineReaderBuilder.builder()
      .appName("KSQL")
      .expander(expander)
      .parser(parser)
      .terminal(terminal)
      .build();

  this.lineReader.setOpt(LineReader.Option.HISTORY_IGNORE_DUPS);
  this.lineReader.setOpt(LineReader.Option.HISTORY_IGNORE_SPACE);

  Path historyFilePath = Paths.get(System.getProperty(
      "history-file",
      System.getProperty("user.home")
      + "/.ksql-history"
  )).toAbsolutePath();
  if (CliUtils.createFile(historyFilePath)) {
    this.lineReader.setVariable(LineReader.HISTORY_FILE, historyFilePath);
    LOGGER.info("Command history saved at: " + historyFilePath);
  } else {
    terminal.writer().println(String.format(
        "WARNING: Unable to create command history file '%s', command history will not be saved.",
        historyFilePath
    ));
  }

  this.lineReader.unsetOpt(LineReader.Option.HISTORY_INCREMENTAL);
  this.history = new DefaultHistory(this.lineReader);

  this.prompt = DEFAULT_PROMPT;
}
 
Example #3
Source Project: incubator-ratis   Author: apache   File: LogServiceShell.java    License: 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 Project: rdf4j   Author: eclipse   File: ConsoleIO.java    License: 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 Project: ratis   Author: hortonworks   File: LogServiceShell.java    License: 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 Project: Cleanstone   Author: CleanstoneMC   File: CleanstoneCommandHistory.java    License: 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 #7
Source Project: ssh-shell-spring-boot   Author: fonimus   File: SshShellHistoryAutoConfiguration.java    License: 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 #8
Source Project: ssh-shell-spring-boot   Author: fonimus   File: HistoryCommandTest.java    License: Apache License 2.0 4 votes vote down vote up
@BeforeEach
void setUp() {
    SshShellHelper helper = mock(SshShellHelper.class);
    when(helper.getHistory()).thenReturn(new DefaultHistory());
    cmd = new HistoryCommand(helper);
}
 
Example #9
Source Project: ssh-shell-spring-boot   Author: fonimus   File: SshShellHelper.java    License: 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());
}