@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; }
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(); }
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; }
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(" ");
AttributedStringBuilder sb = new AttributedStringBuilder(); sb.append(" "); sb.styled(AttributedStyle::bold, String.format("%3d", entry.index() + 1)); if (opt.isSet("d")) { sb.append(" ");
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 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; } }
public int first() { return history.iterator().next().index() + 1; }
public int first() { return history.iterator().next().index() + 1; }