Skip to content

Commit da3321f

Browse files
fxlianglotem
authored andcommitted
feat(tool): deduplicate input history and limit its total length
1 parent 80cefba commit da3321f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

tools/line_editor.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <unistd.h>
1010
#endif
1111
#include <cstdio>
12+
#include <algorithm>
1213

1314
LineEditor::LineEditor(size_t max_length) : max_length_(max_length) {}
1415

@@ -68,9 +69,13 @@ bool LineEditor::ReadLine(std::string* out) {
6869
}
6970
}
7071
if (!whitespace_only) {
71-
if (history_.empty() || history_.back() != *out) {
72+
const auto it = std::find(history_.begin(), history_.end(), *out);
73+
if (it == history_.end()) {
7274
history_.push_back(*out);
7375
}
76+
if (history_.size() > 4096) {
77+
history_.erase(history_.begin());
78+
}
7479
}
7580
}
7681
suggestion_.clear();

0 commit comments

Comments
 (0)