aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/apps/eval_expr/eval_expr.cpp
diff options
context:
space:
mode:
authorHÃ¥vard Pettersen <3535158+havardpe@users.noreply.github.com>2021-09-02 15:34:52 +0200
committerGitHub <noreply@github.com>2021-09-02 15:34:52 +0200
commit25931432f58e53fb90068c143f61ee638dc54d69 (patch)
treeff7cd9f81be1696bdd5c918cda6e37849a4ee128 /eval/src/apps/eval_expr/eval_expr.cpp
parent045321f2a1b2d00d33ccf7c64c2708b6e2c94667 (diff)
parent29862b387bf5451c0936e7c00a458193154d5e4f (diff)
Merge pull request #18956 from vespa-engine/havardpe/use-edit-line
use editline
Diffstat (limited to 'eval/src/apps/eval_expr/eval_expr.cpp')
-rw-r--r--eval/src/apps/eval_expr/eval_expr.cpp57
1 files changed, 48 insertions, 9 deletions
diff --git a/eval/src/apps/eval_expr/eval_expr.cpp b/eval/src/apps/eval_expr/eval_expr.cpp
index 67eda68937a..60040a175c7 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,55 @@ bool is_only_whitespace(const vespalib::string &str) {
return true;
}
+struct EditLineWrapper {
+ EditLine *my_el;
+ History *my_hist;
+ HistEvent ignore;
+ 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())
+ {
+ memset(&ignore, 0, sizeof(ignore));
+ el_set(my_el, EL_EDITOR, "emacs");
+ el_set(my_el, EL_PROMPT, prompt_fun);
+ history(my_hist, &ignore, 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));
+ history(my_hist, &ignore, 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;
}