From ec05b883653ba1bb9e92399f78b99a9d9342efc0 Mon Sep 17 00:00:00 2001 From: Devajith Date: Thu, 7 Nov 2024 16:18:07 +0100 Subject: [PATCH] [lineeditor] Add `setHistorySize()` method for adjusting history size (#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. --- llvm/include/llvm/LineEditor/LineEditor.h | 1 + llvm/lib/LineEditor/LineEditor.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/LineEditor/LineEditor.h b/llvm/include/llvm/LineEditor/LineEditor.h index 29fdf9f85c6fa5..ca0312bf6297f5 100644 --- a/llvm/include/llvm/LineEditor/LineEditor.h +++ b/llvm/include/llvm/LineEditor/LineEditor.h @@ -41,6 +41,7 @@ class LineEditor { void saveHistory(); void loadHistory(); + void setHistorySize(int size); static std::string getDefaultHistoryPath(StringRef ProgName); diff --git a/llvm/lib/LineEditor/LineEditor.cpp b/llvm/lib/LineEditor/LineEditor.cpp index d0d138bb1f9d8d..8f667b0238df8d 100644 --- a/llvm/lib/LineEditor/LineEditor.cpp +++ b/llvm/lib/LineEditor/LineEditor.cpp @@ -20,6 +20,7 @@ #endif using namespace llvm; +constexpr int DefaultHistorySize = 800; std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) { SmallString<32> Path; @@ -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(); } @@ -248,6 +249,11 @@ void LineEditor::loadHistory() { } } +void LineEditor::setHistorySize(int size) { + HistEvent HE; + ::history(Data->Hist, &HE, H_SETSIZE, size); +} + std::optional LineEditor::readLine() const { // Call el_gets to prompt the user and read the user's input. int LineLen = 0; @@ -291,6 +297,7 @@ LineEditor::~LineEditor() { void LineEditor::saveHistory() {} void LineEditor::loadHistory() {} +void LineEditor::setHistorySize(int size) {} std::optional LineEditor::readLine() const { ::fprintf(Data->Out, "%s", Prompt.c_str());