From da389bf87f854e597d392373b68220cc3e672be8 Mon Sep 17 00:00:00 2001 From: wangyunlai Date: Mon, 14 Nov 2022 15:03:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E5=A2=9E=E5=BC=BA=20(#120)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如果使用readline,可以记录历史命令 --- src/obclient/client.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/obclient/client.cpp b/src/obclient/client.cpp index 2ea089b..b67b4bf 100644 --- a/src/obclient/client.cpp +++ b/src/obclient/client.cpp @@ -31,6 +31,7 @@ See the Mulan PSL v2 for more details. */ #ifdef USE_READLINE #include "readline/readline.h" +#include "readline/history.h" #endif #define MAX_MEM_BUFFER_SIZE 8192 @@ -39,9 +40,26 @@ See the Mulan PSL v2 for more details. */ using namespace common; #ifdef USE_READLINE +const std::string HISTORY_FILE = std::string(getenv("HOME")) + "/.miniob.history"; + char *my_readline(const char *prompt) { - return readline(prompt); + int size = history_length; + if (size == 0) { + read_history(HISTORY_FILE.c_str()); + + FILE *fp = fopen(HISTORY_FILE.c_str(), "a"); + if (fp != nullptr) { + fclose(fp); + } + } + + char *line = readline(prompt); + if (line != nullptr && line[0] != 0) { + add_history(line); + append_history(1, HISTORY_FILE.c_str()); + } + return line; } #else // USE_READLINE char *my_readline(const char *prompt) -- GitLab