From 857b1bfce15fbf400ae41d88dd310d69de56573d Mon Sep 17 00:00:00 2001 From: pancake Date: Sun, 18 Jul 2021 19:15:54 +0200 Subject: [PATCH] Add scr.hist.filter to toggle the filtered history up/down search ##shell (#18913) * Introduced in f6f7728cf411a08bf81374aaf301b896c49ff842 --- libr/cons/dietline.c | 11 ++++++++--- libr/core/cconfig.c | 18 +++++++++++++----- libr/core/cmd_quit.c | 2 +- libr/include/r_cons.h | 1 + libr/main/radare2.c | 2 +- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 12f7a2e469..4815c65830 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -345,7 +345,12 @@ R_API int r_line_set_hist_callback(RLine *line, RLineHistoryUpCb up, RLineHistor return 1; } -static inline bool match_hist_line(char *hist_line, char *cur_line) { +static inline bool match_hist_line(RLine *line, int i) { + const char *hist_line = line->history.data[i]; + char *cur_line = line->history.match; + if (!line->histfilter) { + return true; + } // Starts with but not equal to return r_str_startswith (hist_line, cur_line) && strcmp (hist_line, cur_line); } @@ -372,7 +377,7 @@ R_API int r_line_hist_cmd_up(RLine *line) { if (line->history.match) { int i; for (i= line->history.index - 1; i >= 0; i--) { - if (match_hist_line (line->history.data[i], line->history.match)) { + if (match_hist_line (line, i)) { line->history.index = i; break; } @@ -401,7 +406,7 @@ R_API int r_line_hist_cmd_down(RLine *line) { if (line->history.match) { int i; for (i = line->history.index + 1; i < line->history.top; i++) { - if (match_hist_line (line->history.data[i], line->history.match)) { + if (match_hist_line (line, i)) { break; } } diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index d3a34fa51e..97625359e3 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -441,6 +441,13 @@ static bool cb_scrlast(void *user, void *data) { return true; } +static bool cb_scr_histfilter(void *user, void *data) { + RCore *core = (RCore *) user; + RConfigNode *node = (RConfigNode *) data; + core->cons->line->histfilter = node->i_value; + return true; +} + static bool cb_scr_vi(void *user, void *data) { RCore *core = (RCore *) user; RConfigNode *node = (RConfigNode *) data; @@ -3356,8 +3363,6 @@ R_API int r_core_config_init(RCore *core) { SETBPREF ("asm.offset", "true", "Show offsets in disassembly"); SETBPREF ("hex.offset", "true", "Show offsets in hex-dump"); SETBPREF ("scr.square", "true", "Use square pixels or not"); - SETCB ("scr.prompt.vi", "false", &cb_scr_vi, "Use vi mode for input prompt"); - SETCB ("scr.prompt.mode", "false", &cb_scr_prompt_mode, "Set prompt color based on vi mode"); SETCB ("scr.wideoff", "false", &cb_scr_wideoff, "Adjust offsets to match asm.bits"); SETCB ("scr.rainbow", "false", &cb_scrrainbow, "Shows rainbow colors depending of address"); SETCB ("scr.last", "true", &cb_scrlast, "Cache last output after flush to make _ command work (disable for performance)"); @@ -3844,7 +3849,6 @@ R_API int r_core_config_init(RCore *core) { SETICB ("scr.pagesize", 1, &cb_scrpagesize, "Flush in pages when scr.linesleep is != 0"); SETCB ("scr.flush", "false", &cb_scrflush, "Force flush to console in realtime (breaks scripting)"); SETBPREF ("scr.slow", "true", "Do slow stuff on visual mode like RFlag.get_at(true)"); - SETCB ("scr.prompt.popup", "false", &cb_scr_prompt_popup, "Show widget dropdown for autocomplete"); #if __WINDOWS__ SETICB ("scr.vtmode", r_cons_singleton ()->vtmode, &scr_vtmode, "Use VT sequences on Windows (0: Disable, 1: Output, 2: Input & Output)"); @@ -3886,11 +3890,13 @@ R_API int r_core_config_init(RCore *core) { SETI ("scr.scrollbar", 0, "Show flagzone (fz) scrollbar in visual mode (0=no,1=right,2=top,3=bottom)"); SETBPREF ("scr.randpal", "false", "Random color palete or just get the next one from 'eco'"); SETCB ("scr.highlight.grep", "false", &cb_scr_color_grep_highlight, "Highlight (INVERT) the grepped words"); + SETCB ("scr.prompt.popup", "false", &cb_scr_prompt_popup, "Show widget dropdown for autocomplete"); + SETCB ("scr.prompt.vi", "false", &cb_scr_vi, "Use vi mode for input prompt"); + SETCB ("scr.prompt.mode", "false", &cb_scr_prompt_mode, "Set prompt color based on vi mode"); SETBPREF ("scr.prompt.file", "false", "Show user prompt file (used by r2 -q)"); SETBPREF ("scr.prompt.flag", "false", "Show flag name in the prompt"); SETBPREF ("scr.prompt.sect", "false", "Show section name in the prompt"); SETBPREF ("scr.tts", "false", "Use tts if available by a command (see ic)"); - SETCB ("scr.hist.block", "true", &cb_scr_histblock, "Use blocks for histogram"); SETCB ("scr.prompt", "true", &cb_scrprompt, "Show user prompt (used by r2 -q)"); SETCB ("scr.tee", "", &cb_teefile, "Pipe output to file of this name"); SETPREF ("scr.seek", "", "Seek to the specified address on startup"); @@ -3906,7 +3912,9 @@ R_API int r_core_config_init(RCore *core) { SETCB ("scr.utf8", r_str_bool (r_cons_is_utf8()), &cb_utf8, "Show UTF-8 characters instead of ANSI"); SETCB ("scr.utf8.curvy", "false", &cb_utf8_curvy, "Show curved UTF-8 corners (requires scr.utf8)"); SETBPREF ("scr.demo", "false", "Use demoscene effects if available"); - SETBPREF ("scr.histsave", "true", "Always save history on exit"); + SETCB ("scr.hist.block", "true", &cb_scr_histblock, "Use blocks for histogram"); + SETCB ("scr.hist.filter", "true", &cb_scr_histfilter, "Filter history for matching lines when using up/down keys"); + SETBPREF ("scr.hist.save", "true", "Always save history on exit"); n = NODECB ("scr.strconv", "asciiesc", &cb_scrstrconv); SETDESC (n, "Convert string before display"); SETOPTIONS (n, "asciiesc", "asciidot", NULL); diff --git a/libr/core/cmd_quit.c b/libr/core/cmd_quit.c index 9e6368f0d2..7d51921f7a 100644 --- a/libr/core/cmd_quit.c +++ b/libr/core/cmd_quit.c @@ -29,7 +29,7 @@ static int cmd_Quit(void *data, const char *input) { } return -2; } - r_config_set (core->config, "scr.histsave", "false"); + r_config_set (core->config, "scr.hist.save", "false"); } if (IS_DIGIT (input[0]) || input[0] == ' ') { core->num->value = r_num_math (core->num, input); diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 332c2fd0ce..4fad19b82d 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -1062,6 +1062,7 @@ struct r_line_t { char *clipboard; int disable; void *user; + bool histfilter; int (*hist_up)(void *user); int (*hist_down)(void *user); char *contents; diff --git a/libr/main/radare2.c b/libr/main/radare2.c index 53f1e363cd..46cfe9cc67 100644 --- a/libr/main/radare2.c +++ b/libr/main/radare2.c @@ -280,7 +280,7 @@ beach: } static bool mustSaveHistory(RConfig *c) { - if (!r_config_get_i (c, "scr.histsave")) { + if (!r_config_get_i (c, "scr.hist.save")) { return false; } if (!r_cons_is_interactive ()) {