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();
}
}
}
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);
}
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());
}
}
}
});
}
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);
}
}
Aggregations