Skip to content

Commit

Permalink
Add a way to not persist some history entries, fixes #282
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 7, 2018
1 parent d2cc0e3 commit 840d45e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 10 additions & 0 deletions reader/src/main/java/org/jline/reader/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ default void add(String line) {

void add(Instant time, String line);

/**
* Check if an entry should be persisted or not.
*
* @param entry the entry to check
* @return <code>true</code> if the given entry should be persisted, <code>false</code> otherwise
*/
default boolean isPersistable(Entry entry) {
return true;
}

//
// Entries
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public void save() throws IOException {
try (BufferedWriter writer = Files.newBufferedWriter(path.toAbsolutePath(),
StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
for (Entry entry : items.subList(lastLoaded, items.size())) {
writer.append(format(entry));
if (isPersistable(entry)) {
writer.append(format(entry));
}
}
}
nbEntriesInFile += items.size() - lastLoaded;
Expand All @@ -149,7 +151,7 @@ protected void trimHistory(Path path, int max) throws IOException {
int idx = l.indexOf(':');
Instant time = Instant.ofEpochMilli(Long.parseLong(l.substring(0, idx)));
String line = unescape(l.substring(idx + 1));
allItems.add(new EntryImpl(allItems.size(), time, line));
allItems.add(createEntry(allItems.size(), time, line));
});
}
// Remove duplicates
Expand All @@ -171,6 +173,17 @@ protected void trimHistory(Path path, int max) throws IOException {
maybeResize();
}

/**
* Create a history entry. Subclasses may override to use their own entry implementations.
* @param index index of history entry
* @param time entry creation time
* @param line the entry text
* @return entry object
*/
protected EntryImpl createEntry(int index, Instant time, String line) {
return new EntryImpl(index, time, line);
}

private void internalClear() {
offset = 0;
index = 0;
Expand Down Expand Up @@ -302,7 +315,7 @@ public Spliterator<Entry> spliterator() {
return items.spliterator();
}

static class EntryImpl implements Entry {
protected static class EntryImpl implements Entry {

private final int index;
private final Instant time;
Expand Down

0 comments on commit 840d45e

Please sign in to comment.