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);
AttributedStringBuilder sb = new AttributedStringBuilder(); sb.append(" "); sb.styled(AttributedStyle::bold, String.format("%3d", entry.index() + 1)); if (opt.isSet("d")) { sb.append(" "); LocalTime lt = LocalTime.from(entry.time().atZone(ZoneId.systemDefault())) .truncatedTo(ChronoUnit.SECONDS); DateTimeFormatter.ISO_LOCAL_TIME.formatTo(lt, sb); sb.append(entry.line()); out.println(sb.toAnsi(reader.getTerminal()));
AttributedStringBuilder sb = new AttributedStringBuilder(); sb.append(" "); sb.styled(AttributedStyle::bold, String.format("%3d", entry.index() + 1)); if (opt.isSet("d")) { sb.append(" "); LocalTime lt = LocalTime.from(entry.time().atZone(ZoneId.systemDefault())) .truncatedTo(ChronoUnit.SECONDS); DateTimeFormatter.ISO_LOCAL_TIME.formatTo(lt, sb); sb.append(entry.line()); out.println(sb.toAnsi(reader.getTerminal()));
if (!items.isEmpty() && line.equals(items.getLast().line())) { return;
protected void trimHistory(Path path, int max) throws IOException { Log.trace("Trimming history path: ", path); // Load all history entries LinkedList<Entry> allItems = new LinkedList<>(); try (BufferedReader reader = Files.newBufferedReader(path)) { reader.lines().forEach(l -> { int idx = l.indexOf(':'); Instant time = Instant.ofEpochMilli(Long.parseLong(l.substring(0, idx))); String line = unescape(l.substring(idx + 1)); allItems.add(createEntry(allItems.size(), time, line)); }); } // Remove duplicates doTrimHistory(allItems, max); // Write history Path temp = Files.createTempFile(path.toAbsolutePath().getParent(), path.getFileName().toString(), ".tmp"); try (BufferedWriter writer = Files.newBufferedWriter(temp, StandardOpenOption.WRITE)) { for (Entry entry : allItems) { writer.append(format(entry)); } } Files.move(temp, path, StandardCopyOption.REPLACE_EXISTING); // Keep items in memory internalClear(); offset = allItems.get(0).index(); items.addAll(allItems); lastLoaded = items.size(); nbEntriesInFile = items.size(); maybeResize(); }
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; }
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; }
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)); } } }
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); } }
public int first() { return history.iterator().next().index() + 1; }
public int first() { return history.iterator().next().index() + 1; }
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(); }
private static List<String> getHistory(final JLineReader reader) { final List<String> commands = new ArrayList<>(); reader.getHistory().forEach(entry -> commands.add(entry.line())); return commands; }