@Override public List<HistoryEntry> getHistory() { final List<HistoryEntry> history = new ArrayList<>(); lineReader.getHistory() .forEach(entry -> history.add(HistoryEntry.of(entry.index() + 1, entry.line()))); return history; }
private static List<String> getHistory(final JLineReader reader) { final List<String> commands = new ArrayList<>(); reader.getHistory().forEach(entry -> commands.add(entry.line())); return commands; }
static void doTrimHistory(List<Entry> allItems, int max) { int idx = 0; while (idx < allItems.size()) { int ridx = allItems.size() - idx - 1; String line = allItems.get(ridx).line().trim(); ListIterator<Entry> iterator = allItems.listIterator(ridx); while (iterator.hasPrevious()) { String l = iterator.previous().line(); if (line.equals(l.trim())) { iterator.remove(); } } idx++; } while (allItems.size() > max) { allItems.remove(0); } }
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() .orElse(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() .orElse(null);
sb.append(entry.line()); out.println(sb.toAnsi(reader.getTerminal()));
sb.append(entry.line()); out.println(sb.toAnsi(reader.getTerminal()));
if (!items.isEmpty() && line.equals(items.getLast().line())) { return;
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; }
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; }
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; }
public int searchBackwards(String searchTerm, int startIndex, boolean startsWith) { boolean caseInsensitive = isSet(Option.CASE_INSENSITIVE_SEARCH); if (caseInsensitive) { searchTerm = searchTerm.toLowerCase(); } ListIterator<History.Entry> it = history.iterator(startIndex); while (it.hasPrevious()) { History.Entry e = it.previous(); 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 int searchBackwards(History history, String searchTerm, int startIndex, boolean startsWith) { ListIterator<Entry> it = history.iterator(startIndex); while (it.hasPrevious()) { History.Entry e = it.previous(); if (startsWith) { if (e.line().startsWith(searchTerm)) { return e.index(); } } else { if (e.line().contains(searchTerm)) { return e.index(); } } } return -1; } }
@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 String get(final int index) { return items.get(index - offset).line(); }
/** * Return the content of the current buffer. */ public String current() { if (index >= size()) { return ""; } return items.get(index).line(); }