@EventListener public void onContextClosedEvent(ContextClosedEvent event) throws IOException { history.save(); } }
private String calculateCommand(int currentOffset, Set<Integer> offsets) { if (!offsets.add(currentOffset)) { throw new IllegalArgumentException( "Cycled rerun of commands from history " + offsets); } History history = sqlLine.getLineReader().getHistory(); Iterator<History.Entry> iterator = currentOffset > 0 ? history.iterator(currentOffset - 1) : history.reverseIterator(history.size() - 1 + currentOffset); String command = iterator.next().line(); if (command.trim().startsWith("!/") || command.startsWith("!rerun")) { String[] cmd = sqlLine.split(command); if (cmd.length > 2 || (cmd.length == 2 && !cmd[1].matches("-?\\d+"))) { return command; } int offset = cmd.length == 1 ? -1 : Integer.parseInt(cmd[1]); if (history.size() < offset || history.size() - 1 < -offset) { return command; } return calculateCommand(offset, offsets); } return command; }
@Override public void clear() { try { history.purge(); } catch (IOException e) { throw new UncheckedIOException(e); } }
@ShellMethod(value = "Display or save the history of previously run commands") public List<String> history(@ShellOption(help = "A file to save history to.", defaultValue = ShellOption.NULL) File file) throws IOException { if (file == null) { List<String> result = new ArrayList<>(jLineHistory.size()); jLineHistory.forEach(e -> result.add(e.line())); return result; } else { try (FileWriter w = new FileWriter(file)) { for (org.jline.reader.History.Entry entry : jLineHistory) { w.append(entry.line()).append(System.lineSeparator()); } } return Collections.singletonList(String.format("Wrote %d entries to %s", jLineHistory.size(), file)); } } }
public void setHistoryFile(String historyFile) { final String currentValue = get(HISTORY_FILE); if (Objects.equals(currentValue, historyFile) || Objects.equals(currentValue, Commands.expand(historyFile))) { return; } if (DEFAULT.equalsIgnoreCase(historyFile)) { set(HISTORY_FILE, DEFAULT); } else { propertiesMap.put(HISTORY_FILE, Commands.expand(historyFile)); } if (sqlLine != null && sqlLine.getLineReader() != null) { History history = sqlLine.getLineReader().getHistory(); if (history == null) { history = new DefaultHistory(); } else { try { history.save(); } catch (IOException e) { sqlLine.handleException(e); } } sqlLine.getLineReader() .setVariable(LineReader.HISTORY_FILE, get(HISTORY_FILE)); history.attach(sqlLine.getLineReader()); } }
protected boolean doSearchHistory(boolean backward) { if (history.isEmpty()) { return false; history.moveTo(searchIndex); if (pair == null) { pair = StreamSupport.stream( Spliterators.spliteratorUnknownSize(history.reverseIterator(searchIndex < 0 ? history.last() : searchIndex - 1), Spliterator.ORDERED), false) .flatMap(e -> matches(pat, e.line(), e.index()).stream()) .findFirst() if (pair == null) { pair = StreamSupport.stream( Spliterators.spliteratorUnknownSize(history.iterator((searchIndex < 0 ? history.last() : searchIndex) + 1), Spliterator.ORDERED), false) .flatMap(e -> matches(pat, e.line(), e.index()).stream()) .findFirst() buf.clear(); if (searchIndex >= 0) { buf.write(history.get(searchIndex)); } else { buf.write(originalBuffer.toString());
default boolean isEmpty() { return size() == 0; }
switch (c) { case '!': if (history.size() == 0) { throw new IllegalArgumentException("!!: event not found"); rep = history.get(history.index() - 1); break; case '#': idx = searchBackwards(history, sc, history.index(), false); if (idx < 0) { throw new IllegalArgumentException("!?" + sc + ": event not found"); } else { rep = history.get(idx); if (history.size() == 0) { throw new IllegalArgumentException("!$: event not found"); String previous = history.get(history.index() - 1).trim(); int lastSpace = previous.lastIndexOf(' '); if (lastSpace != -1) { if (neg && idx > 0 && idx <= history.size()) { rep = history.get(history.index() - idx); } else if (!neg && idx > history.index() - history.size() && idx <= history.index()) { rep = history.get(idx - 1); } else { throw new IllegalArgumentException((neg ? "!-" : "!") + line.substring(i1, i) + ": event not found");
default Iterator<Entry> reverseIterator(int index) { return new Iterator<Entry>() { private final ListIterator<Entry> it = iterator(index + 1); @Override public boolean hasNext() { return it.hasPrevious(); } @Override public Entry next() { return it.previous(); } }; }
public int searchForwards(String searchTerm, int startIndex, boolean startsWith) { boolean caseInsensitive = isSet(Option.CASE_INSENSITIVE_SEARCH); if (caseInsensitive) { searchTerm = searchTerm.toLowerCase(); } if (startIndex > history.last()) { startIndex = history.last(); } ListIterator<History.Entry> it = history.iterator(startIndex); if (searchIndex != -1 && it.hasNext()) { it.next(); } while (it.hasNext()) { History.Entry e = it.next(); String line = e.line(); if (caseInsensitive) { line = line.toLowerCase(); } int idx = line.indexOf(searchTerm); if ((startsWith && idx == 0) || (!startsWith && idx >= 0)) { return e.index(); } } return -1; }
protected boolean viRepeatSearch() { if (searchDir == 0) { return false; } int si = searchDir < 0 ? searchBackwards(searchString, searchIndex, false) : searchForwards(searchString, searchIndex, false); if (si == -1 || si == history.index()) { return false; } searchIndex = si; /* * Show the match. */ buf.clear(); history.moveTo(searchIndex); buf.write(history.get(searchIndex)); if (VICMD.equals(keyMap)) { buf.move(-1); } return true; }
history.attach(this);
@Override public CharSequence get(int index) { return history.get(index - 1); } }
public int last() { return first() + history.size() - 1; }
public int first() { return history.iterator().next().index() + 1; }
fileHistory.attach(lineReader); setLineReader(lineReader); return lineReader;
@Override public CharSequence get(int index) { return history.get(index - 1); } }