jline.console.history.FileHistory Java Examples

The following examples show how to use jline.console.history.FileHistory. 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: ReplClient.java    From enkan with Eclipse Public License 1.0 6 votes vote down vote up
public void start(int initialPort) throws Exception {
    ConsoleReader console = new ConsoleReader();
    console.getTerminal().setEchoEnabled(false);
    console.setPrompt("\u001B[32menkan\u001B[0m> ");
    History history = new FileHistory(new File(System.getProperty("user.home"), ".enkan_history"));
    console.setHistory(history);

    CandidateListCompletionHandler handler = new CandidateListCompletionHandler();
    console.setCompletionHandler(handler);

    consoleHandler = new ConsoleHandler(console);
    if (initialPort > 0) {
        consoleHandler.connect(initialPort);
    }
    clientThread.execute(consoleHandler);
    clientThread.shutdown();
}
 
Example #2
Source File: HeroicInteractiveShell.java    From heroic with Apache License 2.0 6 votes vote down vote up
public static HeroicInteractiveShell buildInstance(
    final List<CommandDefinition> commands, FileInputStream input
) throws Exception {
    final ConsoleReader reader = new ConsoleReader("heroicsh", input, System.out, null);

    final FileHistory history = setupHistory(reader);

    if (history != null) {
        reader.setHistory(history);
    }

    reader.setPrompt(String.format("heroic> "));
    reader.addCompleter(new StringsCompleter(
        ImmutableList.copyOf(commands.stream().map((d) -> d.getName()).iterator())));
    reader.setHandleUserInterrupt(true);

    return new HeroicInteractiveShell(reader, commands, history);
}
 
Example #3
Source File: JLineDevice.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
public JLineDevice(InputStream in, PrintStream out) throws IOException {
    File hfile = new File(HFILE);
    if (!hfile.exists()) {
        File parentFile = hfile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        hfile.createNewFile();
    }
    writer = new PrintWriter(out, true);
    reader = new ConsoleReader(in, out);
    history = new FileHistory(hfile);
    reader.setHistory(history);

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            writeHistory();
        }
    }));
}
 
Example #4
Source File: HeroicInteractiveShell.java    From heroic with Apache License 2.0 5 votes vote down vote up
@java.beans.ConstructorProperties({ "reader", "commands", "history" })
public HeroicInteractiveShell(final ConsoleReader reader,
                              final List<CommandDefinition> commands,
                              final FileHistory history) {
    this.reader = reader;
    this.commands = commands;
    this.history = history;
}
 
Example #5
Source File: HeroicInteractiveShell.java    From heroic with Apache License 2.0 5 votes vote down vote up
private static FileHistory setupHistory(final ConsoleReader reader) throws IOException {
    final String home = System.getProperty("user.home");

    if (home == null) {
        return null;
    }

    return new FileHistory(new File(home, ".heroicsh-history"));
}
 
Example #6
Source File: TajoCli.java    From incubator-tajo with Apache License 2.0 5 votes vote down vote up
private void initHistory() {
  try {
    String historyPath = HOME_DIR + File.separator + HISTORY_FILE;
    if ((new File(HOME_DIR)).exists()) {
      reader.setHistory(new FileHistory(new File(historyPath)));
    } else {
      System.err.println("ERROR: home directory : '" + HOME_DIR +"' does not exist.");
    }
  } catch (Exception e) {
    System.err.println(e.getMessage());
  }
}
 
Example #7
Source File: CliConsole.java    From clamshell-cli with Apache License 2.0 5 votes vote down vote up
public void setHistoryFile(File f){
    histFile = f;
    try{
        history  = new FileHistory(histFile);
        console.setHistory(history);
    }catch(IOException ex){
        throw new RuntimeException ("Unable to set history file.", ex);
    }
}
 
Example #8
Source File: JLineConsole.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for the JLineConsole
 * @param appName the application name
 * @param in the <code>InputStream</code> to read input from
 * @param out the <code>PrintStream</code> to print output to
 * @param completerHolder holds an optional JLine completer to auto-complete commands
 */
@Inject
JLineConsole(@Named("appName") String appName,
             @Named("shellInput") InputStream in, @Named("shellOutput") PrintStream out,
             ShellSettings shellSettings, CompleterHolder completerHolder) {
    super(out);
    try {
        this.reader = new ConsoleReader(appName, in, out, null);
        this.reader.setExpandEvents(false);

        String userHome = System.getProperty("user.home");
        if (userHome != null && userHome.length()>0) {
            this.fileHistory = new FileHistory(new File(userHome, ShellSettings.HISTORY_FILE));
            this.reader.setHistory(fileHistory);
        } else {
            this.fileHistory = null;
        }

        Integer maxSuggestions = shellSettings.settings().getAsInt(ShellSettings.SUGGESTIONS_MAX, 100);
        this.reader.setAutoprintThreshold(maxSuggestions);

        reader.setBellEnabled(false);
        if (completerHolder.completer != null) {
            reader.addCompleter(completerHolder.completer);
        }
        if (completerHolder.completionHandler != null) {
            reader.setCompletionHandler(completerHolder.completionHandler);
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #9
Source File: Cqlsh.java    From sstable-tools with Apache License 2.0 4 votes vote down vote up
public Cqlsh() {
    try {
        Config applicationConfig = ConfigFactory.defaultApplication();
        config = ConfigFactory.parseFile(PREFERENCES_FILE).withFallback(applicationConfig);
        paging = config.getBoolean(PROP_PAGING_ENABLED);
        pageSize = config.getInt(PROP_PAGING_SIZE);
        sstables = config.getStringList(PROP_SSTABLES).stream().map(File::new).filter(f -> {
            if (!f.exists()) {
                System.err.printf(CANNOT_FIND_FILE, f.getAbsolutePath());
            }
            return f.exists();
        }).collect(Collectors.toSet());

        boolean persistInfo = false;
        if (sstables.size() > 0) {
            System.out.println(infoMsg("Using previously defined sstables: " + sstables));
            persistInfo = true;
        }

        preferences = config.getBoolean(PROP_PREFERENCES_ENABLED);
        String schema = config.getString(PROP_SCHEMA);
        if (!Strings.isNullOrEmpty(schema)) {
            System.out.printf(infoMsg("Using previously defined schema:%n%s%n"), schema);
            persistInfo = true;
            CassandraUtils.cqlOverride = schema;
        }

        if (persistInfo) {
            System.out.println(PERSISTENCE_NOTE);
        }

        history = new FileHistory(HISTORY_FILE);
        console = new ConsoleReader();
        console.setPrompt(prompt);
        console.setHistory(history);
        console.setHistoryEnabled(true);
        List<Completer> completers = Lists.newArrayList();

        ArgumentCompleter argCompleter = new ArgumentCompleter(
                caselessCompleter("use"),
                new FileNameCompleter()
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("describe"),
                caselessCompleter("sstables", "schema")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("dump"),
                caselessCompleter("where")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("create"),
                caselessCompleter("table")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("paging"),
                caselessCompleter("on", "off")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("persist"),
                caselessCompleter("on", "off")
        );
        completers.add(argCompleter);
        argCompleter = new ArgumentCompleter(
                caselessCompleter("schema"),
                new AggregateCompleter(new FileNameCompleter(), caselessCompleter("on", "off"))
        );
        completers.add(argCompleter);
        completers.add(caselessCompleter("exit", "help", "select"));
        for (Completer c : completers) {
            console.addCompleter(c);
        }
        console.setHandleUserInterrupt(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #10
Source File: ShellCommand.java    From citeproc-java with Apache License 2.0 4 votes vote down vote up
@Override
public int doRun(String[] remainingArgs, InputReader in, PrintWriter out)
        throws OptionParserException, IOException {
    // prepare console
    final ConsoleReader reader = new ConsoleReader();
    reader.setPrompt("> ");
    reader.addCompleter(new ShellCommandCompleter(EXCLUDED_COMMANDS));
    FileHistory history = new FileHistory(new File(
            CSLToolContext.current().getConfigDir(), "shell_history.txt"));
    reader.setHistory(history);

    // enable colored error stream for ANSI terminals
    if (reader.getTerminal().isAnsiSupported()) {
        OutputStream errout = new ErrorOutputStream(reader.getTerminal()
                .wrapOutIfNeeded(System.out));
        System.setErr(new PrintStream(errout, false,
                ((OutputStreamWriter)reader.getOutput()).getEncoding()));
    }

    PrintWriter cout = new PrintWriter(reader.getOutput(), true);

    // print welcome message
    cout.println("Welcome to " + CSLToolContext.current().getToolName() +
            " " + CSLTool.getVersion());
    cout.println();
    cout.println("Type `help' for a list of commands and `help "
            + "<command>' for information");
    cout.println("on a specific command. Type `quit' to exit " +
            CSLToolContext.current().getToolName() + ".");
    cout.println();

    String line;
    ShellContext.enter();
    try {
        line = mainLoop(reader, cout);
    } finally {
        ShellContext.exit();

        // make sure we save the history before we exit
        history.flush();
    }

    // print Goodbye message
    if (line == null) {
        // user pressed Ctrl+D
        cout.println();
    }
    cout.println("Bye!");

    return 0;
}