Search in sources :

Example 6 with FileHistory

use of jline.console.history.FileHistory in project voltdb by VoltDB.

the class SQLCommand method interactWithTheUser.

// The main loop for interactive mode.
public static void interactWithTheUser() throws Exception {
    final SQLConsoleReader interactiveReader = new SQLConsoleReader(new FileInputStream(FileDescriptor.in), System.out);
    interactiveReader.setBellEnabled(false);
    FileHistory historyFile = null;
    try {
        // Maintain persistent history in ~/.sqlcmd_history.
        historyFile = new FileHistory(new File(System.getProperty("user.home"), ".sqlcmd_history"));
        interactiveReader.setHistory(historyFile);
        // Make Ctrl-D (EOF) exit if on an empty line, otherwise delete the next character.
        KeyMap keyMap = interactiveReader.getKeys();
        keyMap.bind(new Character(KeyMap.CTRL_D).toString(), new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                CursorBuffer cursorBuffer = interactiveReader.getCursorBuffer();
                if (cursorBuffer.length() == 0) {
                    // tells caller to stop (basically a goto)
                    throw new SQLCmdEarlyExitException();
                } else {
                    try {
                        interactiveReader.delete();
                    } catch (IOException e1) {
                    }
                }
            }
        });
        getInteractiveQueries(interactiveReader);
    } finally {
        // Flush input history to a file.
        if (historyFile != null) {
            try {
                historyFile.flush();
            } catch (IOException e) {
                System.err.printf("* Unable to write history to \"%s\" *\n", historyFile.getFile().getPath());
                if (m_debug) {
                    e.printStackTrace();
                }
            }
        }
        // Clean up jline2 resources.
        if (interactiveReader != null) {
            interactiveReader.shutdown();
        }
    }
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) CursorBuffer(jline.console.CursorBuffer) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) FileHistory(jline.console.history.FileHistory) KeyMap(jline.console.KeyMap)

Example 7 with FileHistory

use of jline.console.history.FileHistory in project grakn by graknlabs.

the class HistoryFile method create.

// The result of createNewFile indicates if the file already existed, but that doesn't matter
@SuppressWarnings("ResultOfMethodCallIgnored")
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
public static HistoryFile create(ConsoleReader consoleReader, String historyFilename) throws IOException {
    // Create history file
    File historyFile = new File(historyFilename);
    historyFile.createNewFile();
    FileHistory jlineHistory = new FileHistory(historyFile);
    consoleReader.setHistory(jlineHistory);
    // Make sure history is saved on shutdown
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            jlineHistory.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }));
    return new HistoryFile(jlineHistory);
}
Also used : IOException(java.io.IOException) File(java.io.File) FileHistory(jline.console.history.FileHistory) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 8 with FileHistory

use of jline.console.history.FileHistory in project hive by apache.

the class CliDriver method setupCmdHistory.

private void setupCmdHistory() {
    final String HISTORYFILE = ".hivehistory";
    String historyDirectory = System.getProperty("user.home");
    PersistentHistory history = null;
    try {
        if ((new File(historyDirectory)).exists()) {
            String historyFile = historyDirectory + File.separator + HISTORYFILE;
            history = new FileHistory(new File(historyFile));
            reader.setHistory(history);
        } else {
            System.err.println("WARNING: Directory for Hive history file: " + historyDirectory + " does not exist.   History will not be available during this session.");
        }
    } catch (Exception e) {
        System.err.println("WARNING: Encountered an error while trying to initialize Hive's " + "history file.  History will not be available during this session.");
        System.err.println(e.getMessage());
    }
    // add shutdown hook to flush the history to history file
    ShutdownHookManager.addShutdownHook(new Runnable() {

        @Override
        public void run() {
            History h = reader.getHistory();
            if (h instanceof FileHistory) {
                try {
                    ((FileHistory) h).flush();
                } catch (IOException e) {
                    System.err.println("WARNING: Failed to write command history file: " + e.getMessage());
                }
            }
        }
    });
}
Also used : PersistentHistory(jline.console.history.PersistentHistory) IOException(java.io.IOException) FileHistory(jline.console.history.FileHistory) PersistentHistory(jline.console.history.PersistentHistory) History(jline.console.history.History) File(java.io.File) FileHistory(jline.console.history.FileHistory) FileNotFoundException(java.io.FileNotFoundException) StringUtils.stringifyException(org.apache.hadoop.util.StringUtils.stringifyException) CommandProcessorException(org.apache.hadoop.hive.ql.processors.CommandProcessorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLException(java.sql.SQLException) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) IOException(java.io.IOException)

Example 9 with FileHistory

use of jline.console.history.FileHistory in project neo4j by neo4j.

the class FileHistorian method setupHistory.

@Nonnull
public static Historian setupHistory(@Nonnull final ConsoleReader reader, @Nonnull final Logger logger, @Nonnull final File historyFile) throws IOException {
    try {
        File dir = historyFile.getParentFile();
        if (!dir.isDirectory() && !dir.mkdir()) {
            throw new IOException("Failed to create directory for history: " + dir.getAbsolutePath());
        }
        final FileHistory history = new FileHistory(historyFile);
        reader.setHistory(history);
        // Make sure we flush history on exit
        addShutdownHookToFlushHistory(logger, history);
        return new FileHistorian(history);
    } catch (IOException e) {
        logger.printError("Could not load history file. Falling back to session-based history.\n" + e.getMessage());
        MemoryHistory history = new MemoryHistory();
        reader.setHistory(history);
        return new FileHistorian(history);
    }
}
Also used : MemoryHistory(jline.console.history.MemoryHistory) IOException(java.io.IOException) File(java.io.File) FileHistory(jline.console.history.FileHistory) Nonnull(javax.annotation.Nonnull)

Aggregations

FileHistory (jline.console.history.FileHistory)9 File (java.io.File)7 IOException (java.io.IOException)7 MemoryHistory (jline.console.history.MemoryHistory)4 FileNotFoundException (java.io.FileNotFoundException)2 History (jline.console.history.History)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Splitter (com.google.common.base.Splitter)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FileInputStream (java.io.FileInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 Nonnull (javax.annotation.Nonnull)1 ConsoleReader (jline.console.ConsoleReader)1 CursorBuffer (jline.console.CursorBuffer)1 KeyMap (jline.console.KeyMap)1 StringsCompleter (jline.console.completer.StringsCompleter)1