use of jline.console.history.History in project presto by prestodb.
the class Console method runConsole.
private static void runConsole(QueryRunner queryRunner, ClientSession session, AtomicBoolean exiting) {
try (TableNameCompleter tableNameCompleter = new TableNameCompleter(queryRunner);
LineReader reader = new LineReader(getHistory(), commandCompleter(), lowerCaseCommandCompleter(), tableNameCompleter)) {
tableNameCompleter.populateCache();
StringBuilder buffer = new StringBuilder();
while (!exiting.get()) {
// read a line of input from user
String prompt = PROMPT_NAME;
if (session.getSchema() != null) {
prompt += ":" + session.getSchema();
}
if (buffer.length() > 0) {
prompt = Strings.repeat(" ", prompt.length() - 1) + "-";
}
String commandPrompt = prompt + "> ";
String line = reader.readLine(commandPrompt);
// add buffer to history and clear on user interrupt
if (reader.interrupted()) {
String partial = squeezeStatement(buffer.toString());
if (!partial.isEmpty()) {
reader.getHistory().add(partial);
}
buffer = new StringBuilder();
continue;
}
// exit on EOF
if (line == null) {
System.out.println();
return;
}
// check for special commands if this is the first line
if (buffer.length() == 0) {
String command = line.trim();
if (HISTORY_INDEX_PATTERN.matcher(command).matches()) {
int historyIndex = parseInt(command.substring(1));
History history = reader.getHistory();
if ((historyIndex <= 0) || (historyIndex > history.index())) {
System.err.println("Command does not exist");
continue;
}
line = history.get(historyIndex - 1).toString();
System.out.println(commandPrompt + line);
}
if (command.endsWith(";")) {
command = command.substring(0, command.length() - 1).trim();
}
switch(command.toLowerCase(ENGLISH)) {
case "exit":
case "quit":
return;
case "history":
for (History.Entry entry : reader.getHistory()) {
System.out.printf("%5d %s%n", entry.index() + 1, entry.value());
}
continue;
case "help":
System.out.println();
System.out.println(getHelpText());
continue;
}
}
// not a command, add line to buffer
buffer.append(line).append("\n");
// execute any complete statements
String sql = buffer.toString();
StatementSplitter splitter = new StatementSplitter(sql, ImmutableSet.of(";", "\\G"));
for (Statement split : splitter.getCompleteStatements()) {
Optional<Object> statement = getParsedStatement(split.statement());
if (statement.isPresent() && isSessionParameterChange(statement.get())) {
Map<String, String> properties = queryRunner.getSession().getProperties();
Map<String, String> preparedStatements = queryRunner.getSession().getPreparedStatements();
session = processSessionParameterChange(statement.get(), session, properties, preparedStatements);
queryRunner.setSession(session);
tableNameCompleter.populateCache();
} else {
OutputFormat outputFormat = OutputFormat.ALIGNED;
if (split.terminator().equals("\\G")) {
outputFormat = OutputFormat.VERTICAL;
}
process(queryRunner, split.statement(), outputFormat, true);
}
reader.getHistory().add(squeezeStatement(split.statement()) + split.terminator());
}
// replace buffer with trailing partial statement
buffer = new StringBuilder();
String partial = splitter.getPartialStatement();
if (!partial.isEmpty()) {
buffer.append(partial).append('\n');
}
}
} catch (IOException e) {
System.err.println("Readline error: " + e.getMessage());
}
}
use of jline.console.history.History in project apex-core by apache.
the class ApexCli method processLine.
private void processLine(String line, final ConsoleReader reader, boolean expandMacroAlias) {
try {
// clear interrupt flag
Thread.interrupted();
if (reader.isHistoryEnabled()) {
History history = reader.getHistory();
if (history instanceof FileHistory) {
try {
((FileHistory) history).flush();
} catch (IOException ex) {
// ignore
}
}
}
//LOG.debug("line: \"{}\"", line);
List<String[]> commands = tokenizer.tokenize(line);
if (commands == null) {
return;
}
for (final String[] args : commands) {
if (args.length == 0 || StringUtils.isBlank(args[0])) {
continue;
}
//LOG.debug("Got: {}", mapper.writeValueAsString(args));
if (expandMacroAlias) {
if (macros.containsKey(args[0])) {
List<String> macroItems = expandMacro(macros.get(args[0]), args);
for (String macroItem : macroItems) {
if (consolePresent) {
System.out.println("expanded-macro> " + macroItem);
}
processLine(macroItem, reader, false);
}
continue;
}
if (aliases.containsKey(args[0])) {
processLine(aliases.get(args[0]), reader, false);
continue;
}
}
CommandSpec cs = null;
if (changingLogicalPlan) {
cs = logicalPlanChangeCommands.get(args[0]);
} else {
if (currentApp != null) {
cs = connectedCommands.get(args[0]);
}
if (cs == null) {
cs = globalCommands.get(args[0]);
}
}
if (cs == null) {
if (connectedCommands.get(args[0]) != null) {
System.err.println("\"" + args[0] + "\" is valid only when connected to an application. Type \"connect <appid>\" to connect to an application.");
lastCommandError = true;
} else if (logicalPlanChangeCommands.get(args[0]) != null) {
System.err.println("\"" + args[0] + "\" is valid only when changing a logical plan. Type \"begin-logical-plan-change\" to change a logical plan");
lastCommandError = true;
} else {
System.err.println("Invalid command '" + args[0] + "'. Type \"help\" for list of commands");
lastCommandError = true;
}
} else {
try {
cs.verifyArguments(args);
} catch (CliException ex) {
cs.printUsage(args[0]);
throw ex;
}
final Command command = cs.command;
commandThread = new Thread() {
@Override
public void run() {
try {
command.execute(args, reader);
lastCommandError = false;
} catch (Exception e) {
handleException(e);
} catch (Error e) {
handleException(e);
System.err.println("Fatal error encountered");
System.exit(1);
}
}
};
mainThread = Thread.currentThread();
commandThread.start();
try {
commandThread.join();
} catch (InterruptedException ex) {
System.err.println("Interrupted");
}
commandThread = null;
}
}
} catch (Exception e) {
handleException(e);
}
}
use of jline.console.history.History 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.History in project apex-core by apache.
the class ApexCli method getLaunchAppPackageArgs.
String[] getLaunchAppPackageArgs(AppPackage ap, ConfigPackage cp, LaunchCommandLineInfo commandLineInfo, ConsoleReader reader) throws Exception {
String matchAppName = null;
if (commandLineInfo.args.length > 1) {
matchAppName = commandLineInfo.args[1];
}
List<AppInfo> applications = new ArrayList<>(getAppsFromPackageAndConfig(ap, cp, commandLineInfo.useConfigApps));
if (matchAppName != null) {
Iterator<AppInfo> it = applications.iterator();
while (it.hasNext()) {
AppInfo ai = it.next();
if ((commandLineInfo.exactMatch && !ai.name.equals(matchAppName)) || !ai.name.toLowerCase().matches(".*" + matchAppName.toLowerCase() + ".*")) {
it.remove();
}
}
}
AppInfo selectedApp = null;
if (applications.isEmpty()) {
throw new CliException("No applications in Application Package" + (matchAppName != null ? " matching \"" + matchAppName + "\"" : ""));
} else if (applications.size() == 1) {
selectedApp = applications.get(0);
} else {
//Store the appNames sorted in alphabetical order and their position in matchingAppFactories list
TreeMap<String, Integer> appNamesInAlphabeticalOrder = new TreeMap<>();
// Display matching applications
for (int i = 0; i < applications.size(); i++) {
String appName = applications.get(i).name;
appNamesInAlphabeticalOrder.put(appName, i);
}
//Create a mapping between the app display number and original index at matchingAppFactories
int index = 1;
HashMap<Integer, Integer> displayIndexToOriginalUnsortedIndexMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : appNamesInAlphabeticalOrder.entrySet()) {
//Map display number of the app to original unsorted index
displayIndexToOriginalUnsortedIndexMap.put(index, entry.getValue());
//Display the app names
System.out.printf("%3d. %s\n", index++, entry.getKey());
}
// Exit if not in interactive mode
if (!consolePresent) {
throw new CliException("More than one application in Application Package match '" + matchAppName + "'");
} else {
boolean useHistory = reader.isHistoryEnabled();
reader.setHistoryEnabled(false);
History previousHistory = reader.getHistory();
History dummyHistory = new MemoryHistory();
reader.setHistory(dummyHistory);
List<Completer> completers = new ArrayList<>(reader.getCompleters());
for (Completer c : completers) {
reader.removeCompleter(c);
}
reader.setHandleUserInterrupt(true);
String optionLine;
try {
optionLine = reader.readLine("Choose application: ");
} finally {
reader.setHandleUserInterrupt(false);
reader.setHistoryEnabled(useHistory);
reader.setHistory(previousHistory);
for (Completer c : completers) {
reader.addCompleter(c);
}
}
try {
int option = Integer.parseInt(optionLine);
if (0 < option && option <= applications.size()) {
int appIndex = displayIndexToOriginalUnsortedIndexMap.get(option);
selectedApp = applications.get(appIndex);
}
} catch (Exception ex) {
// ignore
}
}
}
if (selectedApp == null) {
throw new CliException("No application selected");
}
DTConfiguration launchProperties = getLaunchAppPackageProperties(ap, cp, commandLineInfo, selectedApp.name);
String appFile = ap.tempDirectory() + "/app/" + selectedApp.file;
List<String> launchArgs = new ArrayList<>();
launchArgs.add("launch");
launchArgs.add("-exactMatch");
List<String> absClassPath = new ArrayList<>(ap.getClassPath());
for (int i = 0; i < absClassPath.size(); i++) {
String path = absClassPath.get(i);
if (!path.startsWith("/")) {
absClassPath.set(i, ap.tempDirectory() + "/" + path);
}
}
if (cp != null) {
StringBuilder files = new StringBuilder();
for (String file : cp.getClassPath()) {
if (files.length() != 0) {
files.append(',');
}
files.append(cp.tempDirectory()).append(File.separatorChar).append(file);
}
if (!StringUtils.isBlank(files.toString())) {
if (commandLineInfo.libjars != null) {
commandLineInfo.libjars = files.toString() + "," + commandLineInfo.libjars;
} else {
commandLineInfo.libjars = files.toString();
}
}
files.setLength(0);
for (String file : cp.getFiles()) {
if (files.length() != 0) {
files.append(',');
}
files.append(cp.tempDirectory()).append(File.separatorChar).append(file);
}
if (!StringUtils.isBlank(files.toString())) {
if (commandLineInfo.files != null) {
commandLineInfo.files = files.toString() + "," + commandLineInfo.files;
} else {
commandLineInfo.files = files.toString();
}
}
}
StringBuilder libjarsVal = new StringBuilder();
if (!absClassPath.isEmpty() || commandLineInfo.libjars != null) {
if (!absClassPath.isEmpty()) {
libjarsVal.append(org.apache.commons.lang3.StringUtils.join(absClassPath, ','));
}
if (commandLineInfo.libjars != null) {
if (libjarsVal.length() > 0) {
libjarsVal.append(",");
}
libjarsVal.append(commandLineInfo.libjars);
}
}
if (appFile.endsWith(".json") || appFile.endsWith(".properties")) {
if (libjarsVal.length() > 0) {
libjarsVal.append(",");
}
libjarsVal.append(ap.tempDirectory()).append("/app/*.jar");
}
if (libjarsVal.length() > 0) {
launchArgs.add("-libjars");
launchArgs.add(libjarsVal.toString());
}
File launchPropertiesFile = new File(ap.tempDirectory(), "launch.xml");
launchProperties.writeToFile(launchPropertiesFile, "");
launchArgs.add("-conf");
launchArgs.add(launchPropertiesFile.getCanonicalPath());
if (commandLineInfo.localMode) {
launchArgs.add("-local");
}
if (commandLineInfo.archives != null) {
launchArgs.add("-archives");
launchArgs.add(commandLineInfo.archives);
}
if (commandLineInfo.files != null) {
launchArgs.add("-files");
launchArgs.add(commandLineInfo.files);
}
if (commandLineInfo.origAppId != null) {
launchArgs.add("-originalAppId");
launchArgs.add(commandLineInfo.origAppId);
}
if (commandLineInfo.queue != null) {
launchArgs.add("-queue");
launchArgs.add(commandLineInfo.queue);
}
if (commandLineInfo.tags != null) {
launchArgs.add("-tags");
launchArgs.add(commandLineInfo.tags);
}
launchArgs.add(appFile);
if (!appFile.endsWith(".json") && !appFile.endsWith(".properties")) {
launchArgs.add(selectedApp.name);
}
LOG.debug("Launch command: {}", StringUtils.join(launchArgs, " "));
return launchArgs.toArray(new String[] {});
}
Aggregations