aboutsummaryrefslogtreecommitdiffstats
path: root/eval
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2021-09-02 12:35:40 +0000
committerHåvard Pettersen <havardpe@oath.com>2021-09-02 12:57:42 +0000
commit42d0ef76316ec72775a5922639550c728e24ee77 (patch)
tree57053b2869ab0539927ee902f4fd9d7e5a8eb16d /eval
parent14376e0436c4fc055898d47b43908e60699c6b47 (diff)
use editline
Diffstat (limited to 'eval')
-rw-r--r--eval/src/apps/eval_expr/CMakeLists.txt2
-rw-r--r--eval/src/apps/eval_expr/eval_expr.cpp59
2 files changed, 52 insertions, 9 deletions
diff --git a/eval/src/apps/eval_expr/CMakeLists.txt b/eval/src/apps/eval_expr/CMakeLists.txt
index aca1307e76e..66bbf3a0e3b 100644
--- a/eval/src/apps/eval_expr/CMakeLists.txt
+++ b/eval/src/apps/eval_expr/CMakeLists.txt
@@ -6,4 +6,6 @@ vespa_add_executable(eval_eval_expr_app
INSTALL bin
DEPENDS
vespaeval
+ EXTERNAL_DEPENDS
+ edit
)
diff --git a/eval/src/apps/eval_expr/eval_expr.cpp b/eval/src/apps/eval_expr/eval_expr.cpp
index 67eda68937a..248a2abf9a0 100644
--- a/eval/src/apps/eval_expr/eval_expr.cpp
+++ b/eval/src/apps/eval_expr/eval_expr.cpp
@@ -17,6 +17,8 @@
#include <vespa/eval/eval/test/test_io.h>
#include <vespa/vespalib/util/stringfmt.h>
+#include <histedit.h>
+
using vespalib::make_string_short::fmt;
using namespace vespalib::eval;
@@ -201,10 +203,6 @@ void handle_message(Context &ctx, const Inspector &req, Cursor &reply) {
}
}
-const vespalib::string exit_cmd("exit");
-const vespalib::string verbose_cmd("verbose ");
-const vespalib::string def_cmd("def ");
-
bool is_only_whitespace(const vespalib::string &str) {
for (auto c: str) {
if (!isspace(c)) {
@@ -214,14 +212,57 @@ bool is_only_whitespace(const vespalib::string &str) {
return true;
}
+struct EditLineWrapper {
+ EditLine *my_el;
+ History *my_hist;
+ static vespalib::string prompt;
+ static char *prompt_fun(EditLine *) { return &prompt[0]; }
+ EditLineWrapper()
+ : my_el(el_init("vespa-eval-expr", stdin, stdout, stderr)),
+ my_hist(history_init())
+ {
+ el_set(my_el, EL_EDITOR, "emacs");
+ el_set(my_el, EL_PROMPT, prompt_fun);
+ HistEvent evt;
+ memset(&evt, 0, sizeof(evt));
+ history(my_hist, &evt, H_SETSIZE, 1024);
+ el_set(my_el, EL_HIST, history, my_hist);
+ }
+ ~EditLineWrapper();
+ bool read_line(vespalib::string &line_out) {
+ do {
+ int line_len = 0;
+ const char *line = el_gets(my_el, &line_len);
+ if (line == nullptr) {
+ return false;
+ }
+ line_out.assign(line, line_len);
+ if ((line_out.size() > 0) && (line_out[line_out.size() - 1] == '\n')) {
+ line_out.pop_back();
+ }
+ } while (is_only_whitespace(line_out));
+ HistEvent evt;
+ memset(&evt, 0, sizeof(evt));
+ history(my_hist, &evt, H_ENTER, line_out.c_str());
+ return true;
+ }
+};
+EditLineWrapper::~EditLineWrapper()
+{
+ el_set(my_el, EL_HIST, history, nullptr);
+ history_end(my_hist);
+ el_end(my_el);
+}
+vespalib::string EditLineWrapper::prompt("> ");
+
+const vespalib::string exit_cmd("exit");
+const vespalib::string verbose_cmd("verbose ");
+const vespalib::string def_cmd("def ");
+
int interactive_mode(Context &ctx) {
- StdIn std_in;
- LineReader input(std_in);
+ EditLineWrapper input;
vespalib::string line;
while (input.read_line(line)) {
- if (is_only_whitespace(line)) {
- continue;
- }
if (line == exit_cmd) {
return 0;
}