Skip to content

Commit

Permalink
Add option to disable timestamps for history file
Browse files Browse the repository at this point in the history
This allows using history files from older versions of JLine.
  • Loading branch information
electrum authored and gnodet committed Jul 17, 2018
1 parent b537a73 commit cd29a53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ enum Option {
HISTORY_REDUCE_BLANKS(true),
HISTORY_BEEP(true),
HISTORY_INCREMENTAL(true),
HISTORY_TIMESTAMPED(true),
/** when displaying candidates, group them by {@link Candidate#group()} */
AUTO_GROUP(true),
AUTO_MENU(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,7 @@ public void load() throws IOException {
Log.trace("Loading history from: ", path);
try (BufferedReader reader = Files.newBufferedReader(path)) {
internalClear();
reader.lines().forEach(l -> {
int idx = l.indexOf(':');
if (idx < 0) {
throw new IllegalArgumentException("Bad history file syntax! " +
"The history file `" + path + "` may be an older history: " +
"please remove it or use a different history file.");
}
Instant time = Instant.ofEpochMilli(Long.parseLong(l.substring(0, idx)));
String line = unescape(l.substring(idx + 1));
internalAdd(time, line);
});
reader.lines().forEach(line -> addHistoryLine(path, line));
lastLoaded = items.size();
nbEntriesInFile = lastLoaded;
maybeResize();
Expand All @@ -107,6 +97,23 @@ public void load() throws IOException {
}
}

protected void addHistoryLine(Path path, String line) {
if (reader.isSet(LineReader.Option.HISTORY_TIMESTAMPED)) {
int idx = line.indexOf(':');
if (idx < 0) {
throw new IllegalArgumentException("Bad history file syntax! " +
"The history file `" + path + "` may be an older history: " +
"please remove it or use a different history file.");
}
Instant time = Instant.ofEpochMilli(Long.parseLong(line.substring(0, idx)));
String unescaped = unescape(line.substring(idx + 1));
internalAdd(time, unescaped);
}
else {
internalAdd(Instant.now(), unescape(line));
}
}

@Override
public void purge() throws IOException {
internalClear();
Expand Down Expand Up @@ -232,7 +239,10 @@ public int last() {
}

private String format(Entry entry) {
return Long.toString(entry.time().toEpochMilli()) + ":" + escape(entry.line()) + "\n";
if (reader.isSet(LineReader.Option.HISTORY_TIMESTAMPED)) {
return Long.toString(entry.time().toEpochMilli()) + ":" + escape(entry.line()) + "\n";
}
return escape(entry.line()) + "\n";
}

public String get(final int index) {
Expand Down

0 comments on commit cd29a53

Please sign in to comment.