Skip to content

Commit

Permalink
[lineeditor] Add setHistorySize() method for adjusting history size (
Browse files Browse the repository at this point in the history
…llvm#110092)

This patch adds a `setHistorySize` method to `LineEditor`.

This is particularly useful for tools like `clang-repl`, `clang-query`,
`mlir-query`, and other REPL interfaces, where managing history size
might be needed.
  • Loading branch information
devajithvs authored Nov 7, 2024
1 parent c3e9f48 commit ec05b88
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions llvm/include/llvm/LineEditor/LineEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class LineEditor {

void saveHistory();
void loadHistory();
void setHistorySize(int size);

static std::string getDefaultHistoryPath(StringRef ProgName);

Expand Down
9 changes: 8 additions & 1 deletion llvm/lib/LineEditor/LineEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#endif

using namespace llvm;
constexpr int DefaultHistorySize = 800;

std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) {
SmallString<32> Path;
Expand Down Expand Up @@ -220,8 +221,8 @@ LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
NULL); // Fix the delete key.
::el_set(Data->EL, EL_CLIENTDATA, Data.get());

setHistorySize(DefaultHistorySize);
HistEvent HE;
::history(Data->Hist, &HE, H_SETSIZE, 800);
::history(Data->Hist, &HE, H_SETUNIQUE, 1);
loadHistory();
}
Expand All @@ -248,6 +249,11 @@ void LineEditor::loadHistory() {
}
}

void LineEditor::setHistorySize(int size) {
HistEvent HE;
::history(Data->Hist, &HE, H_SETSIZE, size);
}

std::optional<std::string> LineEditor::readLine() const {
// Call el_gets to prompt the user and read the user's input.
int LineLen = 0;
Expand Down Expand Up @@ -291,6 +297,7 @@ LineEditor::~LineEditor() {

void LineEditor::saveHistory() {}
void LineEditor::loadHistory() {}
void LineEditor::setHistorySize(int size) {}

std::optional<std::string> LineEditor::readLine() const {
::fprintf(Data->Out, "%s", Prompt.c_str());
Expand Down

0 comments on commit ec05b88

Please sign in to comment.