use of org.jline.reader.History in project hazelcast by hazelcast.
the class ClientConsoleApp method handleCommand.
/**
* Handle a command.
*/
@SuppressFBWarnings("DM_EXIT")
@SuppressWarnings({ "checkstyle:methodlength", "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity" })
protected void handleCommand(String commandInputted) {
String command = commandInputted;
if (command == null) {
return;
}
if (command.contains("__")) {
namespace = command.split("__")[0];
command = command.substring(command.indexOf("__") + 2);
}
if (echo) {
handleEcho(command);
}
if (command.startsWith("//")) {
return;
}
command = command.trim();
if (command.length() == 0) {
return;
}
String first = command;
int spaceIndex = command.indexOf(' ');
String[] argsSplit = command.split(" ");
String[] args = new String[argsSplit.length];
for (int i = 0; i < argsSplit.length; i++) {
args[i] = argsSplit[i].trim();
}
if (spaceIndex != -1) {
first = args[0];
}
if (equalsIgnoreCase(first, "help")) {
handleHelp();
} else if (first.startsWith("#") && first.length() > 1) {
int repeat = Integer.parseInt(first.substring(1));
long t0 = Clock.currentTimeMillis();
for (int i = 0; i < repeat; i++) {
handleCommand(command.substring(first.length()).replaceAll("\\$i", "" + i));
}
println("ops/s = " + repeat * ONE_THOUSAND / (Clock.currentTimeMillis() - t0));
} else if (first.startsWith("&") && first.length() > 1) {
final int fork = Integer.parseInt(first.substring(1));
ExecutorService pool = Executors.newFixedThreadPool(fork);
final String threadCommand = command.substring(first.length());
for (int i = 0; i < fork; i++) {
final int threadID = i;
pool.submit(new Runnable() {
public void run() {
String command = threadCommand;
String[] threadArgs = command.replaceAll("\\$t", "" + threadID).trim().split(" ");
// TODO &t #4 m.putmany x k
if ("m.putmany".equals(threadArgs[0]) || "m.removemany".equals(threadArgs[0])) {
if (threadArgs.length < LENGTH_BORDER) {
command += " " + Integer.parseInt(threadArgs[1]) * threadID;
}
}
handleCommand(command);
}
});
}
pool.shutdown();
try {
// wait 1h
pool.awaitTermination(ONE_HOUR, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
} else if (first.startsWith("@")) {
handleAt(first);
} else if (command.indexOf(';') != -1) {
handleColon(command);
} else if (equalsIgnoreCase(first, "silent")) {
silent = Boolean.parseBoolean(args[1]);
} else if (equalsIgnoreCase(first, "echo")) {
echo = Boolean.parseBoolean(args[1]);
println("echo: " + echo);
} else if (equalsIgnoreCase(first, "clear")) {
lineReader.getTerminal().puts(InfoCmp.Capability.clear_screen);
} else if (equalsIgnoreCase(first, "history")) {
History hist = lineReader.getHistory();
ListIterator<History.Entry> iterator = hist.iterator();
while (iterator.hasNext()) {
History.Entry entry = iterator.next();
if (iterator.hasNext()) {
String entryLine = new StringBuilder().append(entry.index() + 1).append(" - ").append(entry.line()).toString();
writer.println(entryLine);
writer.flush();
} else {
// remove the "history" command from the history
iterator.remove();
hist.resetIndex();
}
}
} else if (equalsIgnoreCase(first, "ns")) {
handleNamespace(args);
} else if (equalsIgnoreCase(first, "who")) {
handleWho();
} else if (equalsIgnoreCase(first, "jvm")) {
handleJvm();
} else if (lowerCaseInternal(first).contains("lock") && !first.contains(".")) {
handleLock(args);
} else if (first.contains(".size")) {
handleSize(args);
} else if (first.contains(".clear")) {
handleClear(args);
} else if (first.contains(".destroy")) {
handleDestroy(args);
} else if (first.contains(".iterator")) {
handleIterator(args);
} else if (first.contains(".contains")) {
handleContains(args);
} else if (first.contains(".stats")) {
handleStats(args);
} else if ("t.publish".equals(first)) {
handleTopicPublish(args);
} else if ("q.offer".equals(first)) {
handleQOffer(args);
} else if ("q.take".equals(first)) {
handleQTake(args);
} else if ("q.poll".equals(first)) {
handleQPoll(args);
} else if ("q.peek".equals(first)) {
handleQPeek(args);
} else if ("q.capacity".equals(first)) {
handleQCapacity(args);
} else if ("q.offermany".equals(first)) {
handleQOfferMany(args);
} else if ("q.pollmany".equals(first)) {
handleQPollMany(args);
} else if ("s.add".equals(first)) {
handleSetAdd(args);
} else if ("s.remove".equals(first)) {
handleSetRemove(args);
} else if ("s.addmany".equals(first)) {
handleSetAddMany(args);
} else if ("s.removemany".equals(first)) {
handleSetRemoveMany(args);
} else if (first.equals("m.replace")) {
handleMapReplace(args);
} else if (equalsIgnoreCase(first, "m.putIfAbsent")) {
handleMapPutIfAbsent(args);
} else if (first.equals("m.putAsync")) {
handleMapPutAsync(args);
} else if (first.equals("m.getAsync")) {
handleMapGetAsync(args);
} else if (first.equals("m.put")) {
handleMapPut(args);
} else if (first.equals("m.get")) {
handleMapGet(args);
} else if (equalsIgnoreCase(first, "m.getMapEntry")) {
handleMapGetMapEntry(args);
} else if (first.equals("m.remove")) {
handleMapRemove(args);
} else if (first.equals("m.delete")) {
handleMapDelete(args);
} else if (first.equals("m.evict")) {
handleMapEvict(args);
} else if (first.equals("m.putmany") || equalsIgnoreCase(first, "m.putAll")) {
handleMapPutMany(args);
} else if (first.equals("m.getmany")) {
handleMapGetMany(args);
} else if (first.equals("m.removemany")) {
handleMapRemoveMany(args);
} else if (command.equals("m.keys")) {
handleMapKeys();
} else if (command.equals("m.values")) {
handleMapValues();
} else if (command.equals("m.entries")) {
handleMapEntries();
} else if (first.equals("m.lock")) {
handleMapLock(args);
} else if (equalsIgnoreCase(first, "m.tryLock")) {
handleMapTryLock(args);
} else if (first.equals("m.unlock")) {
handleMapUnlock(args);
} else if (first.contains(".addListener")) {
handleAddListener(args);
} else if (first.equals("m.removeMapListener")) {
handleRemoveListener(args);
} else if (first.equals("mm.put")) {
handleMultiMapPut(args);
} else if (first.equals("mm.get")) {
handleMultiMapGet(args);
} else if (first.equals("mm.remove")) {
handleMultiMapRemove(args);
} else if (command.equals("mm.keys")) {
handleMultiMapKeys();
} else if (command.equals("mm.values")) {
handleMultiMapValues();
} else if (command.equals("mm.entries")) {
handleMultiMapEntries();
} else if (first.equals("mm.lock")) {
handleMultiMapLock(args);
} else if (equalsIgnoreCase(first, "mm.tryLock")) {
handleMultiMapTryLock(args);
} else if (first.equals("mm.unlock")) {
handleMultiMapUnlock(args);
} else if (first.equals("l.add")) {
handleListAdd(args);
} else if (first.equals("l.set")) {
handleListSet(args);
} else if ("l.addmany".equals(first)) {
handleListAddMany(args);
} else if (first.equals("l.remove")) {
handleListRemove(args);
} else if (first.equals("l.contains")) {
handleListContains(args);
} else if ("a.get".equals(first)) {
handleAtomicNumberGet(args);
} else if ("a.set".equals(first)) {
handleAtomicNumberSet(args);
} else if ("a.incrementAndGet".equals(first)) {
handleAtomicNumberInc(args);
} else if ("a.decrementAndGet".equals(first)) {
handleAtomicNumberDec(args);
} else if (first.equals("execute")) {
execute(args);
} else if (first.equals("partitions")) {
handlePartitions(args);
} else if (equalsIgnoreCase(first, "executeOnKey")) {
executeOnKey(args);
} else if (equalsIgnoreCase(first, "executeOnMember")) {
executeOnMember(args);
} else if (equalsIgnoreCase(first, "executeOnMembers")) {
executeOnMembers(args);
} else if (equalsIgnoreCase(first, "instances")) {
handleInstances(args);
} else if (equalsIgnoreCase(first, "quit") || equalsIgnoreCase(first, "exit") || equalsIgnoreCase(first, "shutdown")) {
println("Exiting from the client console application.");
writer.flush();
System.exit(0);
} else if (first.startsWith("e") && first.endsWith(".simulateLoad")) {
handleExecutorSimulate(args);
} else {
println("type 'help' for help");
}
}
use of org.jline.reader.History in project hazelcast by hazelcast.
the class SqlConsole method run.
public static void run(HazelcastInstance hzClient) {
LineReader reader = LineReaderBuilder.builder().parser(new MultilineParser()).variable(LineReader.SECONDARY_PROMPT_PATTERN, new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(SECONDARY_COLOR)).append("%M%P > ").toAnsi()).variable(LineReader.INDENTATION, 2).option(LineReader.Option.DISABLE_EVENT_EXPANSION, true).appName("hazelcast-sql").build();
AtomicReference<SqlResult> activeSqlResult = new AtomicReference<>();
reader.getTerminal().handle(Terminal.Signal.INT, signal -> {
SqlResult r = activeSqlResult.get();
if (r != null) {
r.close();
}
});
PrintWriter writer = reader.getTerminal().writer();
writer.println(sqlStartingPrompt(hzClient));
writer.flush();
for (; ; ) {
String command;
try {
command = reader.readLine(new AttributedStringBuilder().style(AttributedStyle.DEFAULT.foreground(SECONDARY_COLOR)).append("sql> ").toAnsi()).trim();
} catch (EndOfFileException | IOError e) {
// Ctrl+D, and kill signals result in exit
writer.println(Constants.EXIT_PROMPT);
writer.flush();
break;
} catch (UserInterruptException e) {
// Ctrl+C cancels the not-yet-submitted query
continue;
}
command = command.trim();
if (command.length() > 0 && command.charAt(command.length() - 1) == ';') {
command = command.substring(0, command.length() - 1).trim();
} else if (command.lastIndexOf(";") >= 0) {
String errorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("There are non-whitespace characters after the semicolon").toAnsi();
writer.println(errorPrompt);
writer.flush();
continue;
}
if ("".equals(command)) {
continue;
}
if (equalsIgnoreCase("clear", command)) {
reader.getTerminal().puts(InfoCmp.Capability.clear_screen);
continue;
}
if (equalsIgnoreCase("help", command)) {
writer.println(helpPrompt());
writer.flush();
continue;
}
if (equalsIgnoreCase("history", command)) {
History hist = reader.getHistory();
ListIterator<History.Entry> iterator = hist.iterator();
while (iterator.hasNext()) {
History.Entry entry = iterator.next();
if (iterator.hasNext()) {
String entryLine = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(String.valueOf(entry.index() + 1)).append(" - ").append(entry.line()).toAnsi();
writer.println(entryLine);
writer.flush();
} else {
// remove the "history" command from the history
iterator.remove();
hist.resetIndex();
}
}
continue;
}
if (equalsIgnoreCase("exit", command)) {
writer.println(Constants.EXIT_PROMPT);
writer.flush();
break;
}
executeSqlCmd(hzClient, command, reader.getTerminal(), activeSqlResult);
}
}
use of org.jline.reader.History in project accumulo by apache.
the class HistoryCommandTest method setUp.
@Before
public void setUp() throws Exception {
command = new HistoryCommand();
// Make sure everything is initialized
command.getOptions();
cl = createMock(CommandLine.class);
expect(cl.hasOption("c")).andReturn(false);
expect(cl.hasOption("np")).andReturn(true);
replay(cl);
History history = new DefaultHistory();
history.add("foo");
history.add("bar");
baos = new ByteArrayOutputStream();
// Construct a platform dependent new-line
String input = String.format("!1%n");
terminal = TerminalBuilder.builder().system(false).streams(new ByteArrayInputStream(input.getBytes()), baos).build();
reader = LineReaderBuilder.builder().history(history).terminal(terminal).build();
shell = new Shell(reader);
}
Aggregations