Skip to content

Commit 4628d12

Browse files
committed
[filter-ui] fix history
1 parent 287d193 commit 4628d12

10 files changed

+120
-103
lines changed

src/command_executor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ exec_context::execute(source_location loc, const std::string& cmdline)
12741274

12751275
auto before_dls_gen = dls.dls_generation;
12761276
if (this->get_provenance<mouse_input>() && !prompt.p_editor.is_enabled()) {
1277-
auto& hist = prompt.get_history_for(cmdline[0]);
1277+
auto& hist = prompt.get_history_for();
12781278
hist_guard = hist.start_operation(cmdline.substr(1));
12791279
}
12801280

src/filter_sub_source.cc

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,24 @@
5454
using namespace lnav::roles::literals;
5555

5656
filter_sub_source::filter_sub_source(std::shared_ptr<textinput_curses> editor)
57-
: fss_editor(std::move(editor)),
58-
fss_regexp_history(
59-
lnav::textinput::history::for_context("regexp-filter"_frag)),
60-
fss_sql_history(lnav::textinput::history::for_context("sql-filter"_frag))
57+
: fss_editor(std::move(editor))
6158
{
59+
static auto& prompt = lnav::prompt::get();
60+
6261
this->fss_editor->set_visible(false);
6362
this->fss_editor->set_x(28);
6463
this->fss_editor->tc_popup.set_title("Pattern");
6564
this->fss_editor->tc_height = 1;
6665
this->fss_editor->tc_on_change
6766
= bind_mem(&filter_sub_source::rl_change, this);
6867
this->fss_editor->tc_on_history_search
69-
= bind_mem(&filter_sub_source::rl_history, this);
68+
= bind_mem(&lnav::prompt::rl_history_search, &prompt);
7069
this->fss_editor->tc_on_history_list
71-
= bind_mem(&filter_sub_source::rl_history, this);
70+
= bind_mem(&lnav::prompt::rl_history_list, &prompt);
7271
this->fss_editor->tc_on_completion_request
7372
= bind_mem(&filter_sub_source::rl_completion_request, this);
7473
this->fss_editor->tc_on_completion
75-
= bind_mem(&filter_sub_source::rl_completion, this);
74+
= bind_mem(&lnav::prompt::rl_completion, &prompt);
7675
this->fss_editor->tc_on_perform
7776
= bind_mem(&filter_sub_source::rl_perform, this);
7877
this->fss_editor->tc_on_blur = bind_mem(&filter_sub_source::rl_blur, this);
@@ -433,42 +432,22 @@ filter_sub_source::text_size_for_line(textview_curses& tc,
433432
void
434433
filter_sub_source::rl_change(textinput_curses& rc)
435434
{
435+
static auto& prompt = lnav::prompt::get();
436+
437+
if (rc.tc_popup_type == textinput_curses::popup_type_t::history
438+
&& !prompt.p_replace_from_history)
439+
{
440+
rc.tc_on_history_search(rc);
441+
return;
442+
}
443+
436444
auto* top_view = *lnav_data.ld_view_stack.top();
437445
auto rows = this->rows_for(top_view);
438446
auto& row = rows[this->tss_view->get_selection().value()];
439447

440448
row->ti_change(top_view, rc);
441449
}
442450

443-
void
444-
filter_sub_source::rl_history(textinput_curses& tc)
445-
{
446-
switch (tc.tc_text_format) {
447-
case text_format_t::TF_PCRE: {
448-
std::vector<attr_line_t> poss;
449-
this->fss_regexp_history.query_entries(
450-
tc.get_content(), [&poss, &tc](const auto& e) {
451-
auto al
452-
= attr_line_t::from_table_cell_content(e.e_content,
453-
tc.get_width())
454-
.highlight_fuzzy_matches(tc.get_content())
455-
.with_attr_for_all(
456-
lnav::prompt::SUBST_TEXT.value(e.e_content));
457-
poss.emplace_back(al);
458-
});
459-
tc.open_popup_for_history(poss);
460-
break;
461-
}
462-
case text_format_t::TF_PLAINTEXT:
463-
case text_format_t::TF_SQL: {
464-
break;
465-
}
466-
default:
467-
ensure(false);
468-
break;
469-
}
470-
}
471-
472451
void
473452
filter_sub_source::rl_completion_request_int(textinput_curses& tc,
474453
completion_request_type_t crt)
@@ -1014,6 +993,10 @@ filter_sub_source::text_filter_row::prime_text_input(textview_curses* top_view,
1014993
{
1015994
static auto& prompt = lnav::prompt::get();
1016995

996+
auto context = this->tfr_filter->get_lang() == filter_lang_t::SQL
997+
? lnav::prompt::context_t::sql_filter
998+
: lnav::prompt::context_t::regex_filter;
999+
prompt.focus_for(*top_view, ti, context, '\0', {});
10171000
ti.tc_text_format = this->tfr_filter->get_lang() == filter_lang_t::SQL
10181001
? text_format_t::TF_SQL
10191002
: text_format_t::TF_PCRE;
@@ -1210,6 +1193,7 @@ filter_sub_source::text_filter_row::ti_perform(textview_curses* top_view,
12101193
filter_sub_source& parent)
12111194
{
12121195
static const intern_string_t INPUT_SRC = intern_string::lookup("input");
1196+
static auto& prompt = lnav::prompt::get();
12131197

12141198
auto* tss = top_view->get_sub_source();
12151199
auto& fs = tss->get_filters();
@@ -1249,7 +1233,7 @@ filter_sub_source::text_filter_row::ti_perform(textview_curses* top_view,
12491233
this->tfr_filter->get_index(),
12501234
code_ptr);
12511235

1252-
parent.fss_regexp_history.insert_plain_content(new_value);
1236+
prompt.get_history_for().insert_plain_content(new_value);
12531237

12541238
auto iter
12551239
= std::find(fs.begin(), fs.end(), this->tfr_filter);
@@ -1289,6 +1273,7 @@ filter_sub_source::text_filter_row::ti_perform(textview_curses* top_view,
12891273
lnav_data.ld_exec_context.ec_msg_callback_stack.back()(um);
12901274
this->ti_abort(top_view, ti, parent);
12911275
} else {
1276+
prompt.get_history_for().insert_plain_content(new_value);
12921277
lnav_data.ld_log_source.set_sql_filter(new_value,
12931278
stmt.release());
12941279
tss->text_filters_changed();

src/filter_sub_source.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ public:
9595

9696
void rl_change(textinput_curses& rc);
9797

98-
void rl_history(textinput_curses& tc);
99-
10098
enum class completion_request_type_t {
10199
partial,
102100
full,
@@ -266,8 +264,6 @@ public:
266264
}
267265

268266
std::shared_ptr<textinput_curses> fss_editor;
269-
lnav::textinput::history fss_regexp_history;
270-
lnav::textinput::history fss_sql_history;
271267
std::unordered_set<std::string> fss_view_text_possibilities;
272268
attr_line_t fss_curr_line;
273269
log_level_t fss_curr_level;

src/lnav.prompt.cc

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <filesystem>
3131
#include <string>
3232
#include <unordered_set>
33+
#include <vector>
3334

3435
#include "lnav.prompt.hh"
3536

@@ -207,6 +208,8 @@ prompt::get()
207208
textinput::history::for_context("cmd"_frag),
208209
textinput::history::for_context("search"_frag),
209210
textinput::history::for_context("script"_frag),
211+
textinput::history::for_context("regexp-filter"_frag),
212+
lnav::textinput::history::for_context("sql-filter"_frag),
210213
};
211214

212215
return retval;
@@ -287,36 +290,41 @@ prompt::refresh_sql_expr_completions(textview_curses& tc)
287290

288291
void
289292
prompt::focus_for(textview_curses& tc,
293+
textinput_curses& editor,
294+
context_t context,
290295
char sigil,
291296
const std::vector<std::string>& args)
292297
{
293-
this->p_editor.tc_suggestion.clear();
298+
editor.tc_suggestion.clear();
294299
this->p_remote_paths.clear();
295-
switch (sigil) {
296-
case '|': {
300+
this->p_current_context = context;
301+
switch (context) {
302+
case context_t::script: {
297303
this->p_scripts = find_format_scripts(lnav_data.ld_config_paths);
298304
break;
299305
}
300-
case ':': {
306+
case context_t::cmd: {
301307
this->refresh_config_completions();
302308
this->refresh_sql_completions(tc);
303309
this->refresh_sql_expr_completions(tc);
304310
break;
305311
}
306-
case ';': {
312+
case context_t::sql: {
307313
this->refresh_sql_completions(tc);
308314
break;
309315
}
316+
default:
317+
break;
310318
}
311319

312320
for (const auto& [key, item] : this->p_sql_completions) {
313321
this->p_sql_completion_terms.emplace(key);
314322
}
315323

316324
this->p_env_vars.clear();
317-
switch (sigil) {
318-
case ':':
319-
case '|': {
325+
switch (context) {
326+
case context_t::cmd:
327+
case context_t::script: {
320328
for (char** var = environ; *var != nullptr; var++) {
321329
auto pair_sf = string_fragment::from_c_str(*var);
322330
auto split_sf = pair_sf.split_when(string_fragment::tag1{'='});
@@ -330,17 +338,17 @@ prompt::focus_for(textview_curses& tc,
330338
break;
331339
}
332340

333-
this->p_editor.tc_prefix.clear();
341+
editor.tc_prefix.clear();
334342
if (args.size() >= 3) {
335-
this->p_editor.tc_prefix.al_string = args[2];
343+
editor.tc_prefix.al_string = args[2];
336344
} else if (sigil) {
337-
this->p_editor.tc_prefix.al_string.push_back(sigil);
345+
editor.tc_prefix.al_string.push_back(sigil);
338346
}
339-
this->p_editor.set_height(1);
340-
this->p_editor.set_content(cget(args, 3).value_or(""));
341-
this->p_editor.move_cursor_to(textinput_curses::input_point::end());
342-
this->p_editor.tc_popup.set_title("");
343-
this->p_editor.focus();
347+
editor.set_height(1);
348+
editor.set_content(cget(args, 3).value_or(""));
349+
editor.move_cursor_to(textinput_curses::input_point::end());
350+
editor.tc_popup.set_title("");
351+
editor.focus();
344352
}
345353

346354
void
@@ -477,31 +485,30 @@ prompt::rl_history_search(textinput_curses& tc)
477485
void
478486
prompt::rl_history(textinput_curses& tc)
479487
{
480-
auto sigil = tc.tc_prefix.al_string.front();
481-
auto& hist = this->get_history_for(sigil);
488+
auto& hist = this->get_history_for();
482489
auto width = tc.get_width() - 1;
483490
auto pattern = tc.get_content();
484491
std::vector<attr_line_t> poss;
485-
auto cb = [&poss, sigil, width, &pattern](
492+
auto cb = [&poss, this, width, &pattern](
486493
const textinput::history::entry& e) {
487494
auto icon = e.e_status == log_level_t::LEVEL_ERROR ? ui_icon_t::error
488495
: ui_icon_t::ok;
489496
auto al = attr_line_t::from_table_cell_content(e.e_content, width)
490497
.highlight_fuzzy_matches(pattern)
491498
.with_attr_for_all(SUBST_TEXT.value(e.e_content));
492-
switch (sigil) {
493-
case ':':
499+
switch (this->p_current_context) {
500+
case context_t::cmd:
494501
readline_command_highlighter(al, std::nullopt);
495502
al.insert(0, " ");
496503
al.al_attrs.emplace_back(line_range{0, 1}, VC_ICON.value(icon));
497504
break;
498-
case ';':
505+
case context_t::sql:
499506
readline_sql_highlighter(
500507
al, lnav::sql::dialect::sqlite, std::nullopt);
501508
al.insert(0, " ");
502509
al.al_attrs.emplace_back(line_range{0, 1}, VC_ICON.value(icon));
503510
break;
504-
case '/':
511+
case context_t::search:
505512
readline_regex_highlighter(al, std::nullopt);
506513
break;
507514
}
@@ -517,11 +524,11 @@ prompt::rl_history(textinput_curses& tc)
517524
void
518525
prompt::rl_completion(textinput_curses& tc)
519526
{
520-
if (this->p_editor.tc_popup_type == textinput_curses::popup_type_t::history
527+
if (tc.tc_popup_type == textinput_curses::popup_type_t::history
521528
&& this->p_replace_from_history)
522529
{
523-
this->p_editor.blur();
524-
this->p_editor.tc_on_perform(tc);
530+
tc.blur();
531+
tc.tc_on_perform(tc);
525532
return;
526533
}
527534

@@ -553,8 +560,8 @@ prompt::rl_popup_change(textinput_curses& tc)
553560
return;
554561
}
555562

556-
if (this->p_history_changes > 0 && !this->p_editor.tc_change_log.empty()) {
557-
this->p_editor.tc_change_log.pop_back();
563+
if (this->p_history_changes > 0 && !tc.tc_change_log.empty()) {
564+
tc.tc_change_log.pop_back();
558565
}
559566

560567
const auto& al

src/lnav.prompt.hh

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "base/string_attr_type.hh"
4141
#include "format.scripts.hh"
4242
#include "help_text.hh"
43-
#include "lnav.prompt.hh"
4443
#include "mapbox/variant.hpp"
4544
#include "textinput.history.hh"
4645
#include "textinput_curses.hh"
@@ -90,27 +89,44 @@ struct prompt {
9089
role_t sim_role;
9190
};
9291

92+
enum class context_t {
93+
none,
94+
sql,
95+
cmd,
96+
search,
97+
script,
98+
regex_filter,
99+
sql_filter,
100+
};
101+
93102
lnav::textinput::history p_sql_history;
94103
lnav::textinput::history p_cmd_history;
95104
lnav::textinput::history p_search_history;
96105
lnav::textinput::history p_script_history;
106+
lnav::textinput::history p_regexp_filter_history;
107+
lnav::textinput::history p_sql_filter_history;
97108

98-
lnav::textinput::history& get_history_for(char sigil)
109+
lnav::textinput::history& get_history_for()
99110
{
100-
switch (sigil) {
101-
case ':':
111+
switch (this->p_current_context) {
112+
case context_t::cmd:
102113
return this->p_cmd_history;
103-
case ';':
114+
case context_t::sql:
104115
return this->p_sql_history;
105-
case '/':
116+
case context_t::search:
106117
return this->p_search_history;
107-
case '|':
118+
case context_t::script:
108119
return this->p_script_history;
120+
case context_t::regex_filter:
121+
return this->p_regexp_filter_history;
122+
case context_t::sql_filter:
123+
return this->p_sql_filter_history;
109124
default:
110125
ensure(false);
111126
}
112127
}
113128

129+
context_t p_current_context{context_t::none};
114130
std::map<std::string, std::string> p_env_vars;
115131
std::multimap<std::string, sql_item_t, strnatcaseless> p_sql_completions;
116132
std::set<std::string, strnatless> p_sql_completion_terms;
@@ -127,6 +143,8 @@ struct prompt {
127143
int32_t p_history_changes{0};
128144

129145
void focus_for(textview_curses& tc,
146+
textinput_curses& editor,
147+
context_t context,
130148
char sigil,
131149
const std::vector<std::string>& args);
132150

0 commit comments

Comments
 (0)