Skip to contentSkip to conversation

scripts: kconfig: nconf: Add search jump feature

PATCH Superseded Jesse Taube 2 participants 6 messages Jun 18, 2023 – Jun 29, 2023

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts: kconfig: nconf: Add search jump feature

Menuconfig has a feature where you can "press the key in the (#) prefix to jump directly to that location. You will be returned to the current search results after exiting this new menu."

This commit adds this feature to nconfig, with almost identical code.

---
 scripts/kconfig/nconf.c     | 79 +++++++++++++++++++++++++++++++++++--
 scripts/kconfig/nconf.gui.c | 31 ++++++++++++---
 scripts/kconfig/nconf.h     |  5 +++
 3 files changed, 106 insertions(+), 9 deletions(-)
scripts/kconfig/nconf.c +75 -4
index 3ba8b1af390f..cbed2e15c579 100644
@@ -234,6 +234,9 @@ search_help[] =
 "   is located.  A location followed by a [ = y] indicates that this is\n"
 "   a selectable menu item, and the current value is displayed inside\n"
 "   brackets.\n"
+"   Press the key in the (#) prefix to jump directly to that\n"
+"   location. You will be returned to the current search results\n"
+"   after exiting this new menu.\n"
 "o  The 'Selects:' line tells, what symbol will be automatically selected\n"
 "   if this symbol is selected (y or m).\n"
 "o  The 'Selected by' line tells what symbol has selected this symbol.\n"
@@ -685,6 +688,54 @@ static int do_exit(void)
 	return 0;
 }
 
+#define JUMP_NB			9
+
+struct update_text_data {
+	struct list_head *head;
+	struct menu **targets;
+	int *keys;
+};
+
+static size_t get_nth_line(char *buf, size_t n)
+{
+	size_t i = 0;
+
+	while (n > 0 && buf[i]) {
+		if (buf[i++] == '\n')
+			n--;
+	}
+	return i;
+}
+
+static void update_text(char *buf, size_t start, size_t end, void *_data)
+{
+	struct update_text_data *data = _data;
+	struct jump_key *pos;
+	int k = 0;
+
+	start = get_nth_line(buf, start);
+	end = get_nth_line(buf, end);
+
+	list_for_each_entry(pos, data->head, entries) {
+		if (pos->offset >= start && pos->offset < end) {
+			char header[4];
+
+			if (k < JUMP_NB) {
+				int key = '0' + (pos->index % JUMP_NB) + 1;
+
+				sprintf(header, "(%c)", key);
+				data->keys[k] = key;
+				data->targets[k] = pos->target;
+				k++;
+			} else {
+				sprintf(header, "   ");
+			}
+
+			memcpy(buf + pos->offset, header, sizeof(header) - 1);
+		}
+	}
+	data->keys[k] = 0;
+}
 
 static void search_conf(void)
 {
@@ -693,6 +744,7 @@ static void search_conf(void)
 	struct gstr title;
 	char *dialog_input;
 	int dres;
+	bool again = false;
 
 	title = str_new();
 	str_printf( &title, "Enter (sub)string or regexp to search for "
@@ -721,11 +773,30 @@ static void search_conf(void)
 		dialog_input += strlen(CONFIG_);
 
 	sym_arr = sym_re_search(dialog_input);
-	res = get_relations_str(sym_arr, NULL);
+
+	do {
+		LIST_HEAD(head);
+		struct menu *targets[10];
+		int keys[JUMP_NB + 1], i;
+		struct update_text_data data = {
+			.head = &head,
+			.targets = targets,
+			.keys = keys,
+		};
+
+		res = get_relations_str(sym_arr, &head);
+		dres = show_scroll_win_ext(main_window,
+				"Search Results", str_get(&res),
+				keys, &update_text, &data);
+		again = false;
+		for (i = 0; i < JUMP_NB && keys[i]; i++)
+			if (dres == keys[i]) {
+				conf(targets[i]->parent);
+				again = true;
+			}
+		str_free(&res);
+	} while (again);
 	free(sym_arr);
-	show_scroll_win(main_window,
-			"Search Results", str_get(&res));
-	str_free(&res);
 	str_free(&title);
 }
 
scripts/kconfig/nconf.gui.c +26 -5
index 9aedf40f1dc0..44ab0e43c626 100644
@@ -497,10 +497,19 @@ void refresh_all_windows(WINDOW *main_window)
 	refresh();
 }
 
-/* layman's scrollable window... */
+/* Discards const qualifier, but char *text
+ * won't be modifed unless update_text_fnupdate_text is not NULL.
+ */
 void show_scroll_win(WINDOW *main_window,
 		const char *title,
 		const char *text)
+{
+	(void)show_scroll_win_ext(main_window, title, (char *)text, (int []) {0}, NULL, NULL);
+}
+
+/* layman's scrollable window... */
+int show_scroll_win_ext(WINDOW *main_window, const char *title, char *text,
+			 int *keys, update_text_fn update_text, void *data)
 {
 	int res;
 	int total_lines = get_line_no(text);
@@ -514,6 +523,7 @@ void show_scroll_win(WINDOW *main_window,
 	WINDOW *win;
 	WINDOW *pad;
 	PANEL *panel;
+	bool done = false;
 
 	getmaxyx(stdscr, lines, columns);
 
@@ -549,8 +559,11 @@ void show_scroll_win(WINDOW *main_window,
 	panel = new_panel(win);
 
 	/* handle scrolling */
-	do {
-
+	while (!done) {
+		if (update_text) {
+			update_text(text, start_y, start_y + text_lines, data);
+			fill_window(pad, text);
+		}
 		copywin(pad, win, start_y, start_x, 2, 2, text_lines,
 				text_cols, 0);
 		print_in_middle(win,
@@ -593,8 +606,15 @@ void show_scroll_win(WINDOW *main_window,
 		case 'l':
 			start_x++;
 			break;
+		default:
+			for (i = 0; keys[i]; i++) {
+				if (res == keys[i]) {
+					done = true;
+					break;
+				}
+			}
 		}
-		if (res == 10 || res == 27 || res == 'q' ||
+		if (res == 0 || res == 10 || res == 27 || res == 'q' ||
 			res == KEY_F(F_HELP) || res == KEY_F(F_BACK) ||
 			res == KEY_F(F_EXIT))
 			break;
@@ -606,9 +626,10 @@ void show_scroll_win(WINDOW *main_window,
 			start_x = 0;
 		if (start_x >= total_cols-text_cols)
 			start_x = total_cols-text_cols;
-	} while (res);
+	}
 
 	del_panel(panel);
 	delwin(win);
 	refresh_all_windows(main_window);
+	return res;
 }
scripts/kconfig/nconf.h +5 -0
index 6f925bc74eb3..747b30a4cd0f 100644
@@ -67,6 +67,9 @@ typedef enum {
 
 void set_colors(void);
 
+typedef void (*update_text_fn)(char *buf, size_t start, size_t end, void
+			       *_data);
+
 /* this changes the windows attributes !!! */
 void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs);
 int get_line_length(const char *line);
@@ -78,6 +81,8 @@ int dialog_inputbox(WINDOW *main_window,
 		const char *title, const char *prompt,
 		const char *init, char **resultp, int *result_len);
 void refresh_all_windows(WINDOW *main_window);
+int show_scroll_win_ext(WINDOW *main_window, const char *title, char *text,
+			 int *keys, update_text_fn update_text, void *data);
 void show_scroll_win(WINDOW *main_window,
 		const char *title,
 		const char *text);
Signature · 1 line
2.40.0
#1 lore raw
Quoted 8 lines · Jesse Taube

Menuconfig has a feature where you can "press the key in the (#) prefix to jump directly to that location. You will be returned to the current search results after exiting this new menu."

This commit adds this feature to nconfig, with almost identical code.

Setting the code duplication aside,
does this patch work correctly?

$ make defconfig
$ make nconfig

Press F8

Input "MODULES" in the search box.

You will see:

(1) -> Enable loadable module support

Press the "1" key.

It will navigate to "General setup"
instead of "Enable loadable module support".

Signature · 2 lines
Best Regards
Masahiro Yamada
author
#2 lore raw
Quoted 7 lines · Masahiro Yamada
Quoted 5 lines · Jesse Taube

Menuconfig has a feature where you can "press the key in the (#) prefix to jump directly to that location. You will be returned to the current search results after exiting this new menu."

Quoted 6 lines
Quoted 3 lines

This commit adds this feature to nconfig, with almost identical code.

Setting the code duplication aside,

If it does function as expected will you accept the patch? Should I mark the copied codes' original location?

Quoted 14 lines

does this patch work correctly?

$ make defconfig
$ make nconfig

Press F8

Input "MODULES" in the search box.

You will see:

Quoted 7 lines

(1) -> Enable loadable module support

Press the "1" key.

It will navigate to "General setup"
instead of "Enable loadable module support".

Hmm, this is a confusing issue.
It will take you to the parent menu of the option, it should move you to the parent option and
move the cursor to the option location. I will see if I can resolve this oversight.

Thanks,
Jesse Taube

Quoted 10 lines

--
Best Regards
Masahiro Yamada

#3 lore raw
Quoted 11 lines · Jesse T
Quoted 9 lines · Masahiro Yamada
Quoted 7 lines · Jesse Taube

Menuconfig has a feature where you can "press the key in the (#) prefix to jump directly to that location. You will be returned to the current search results after exiting this new menu."

This commit adds this feature to nconfig, with almost identical code.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Setting the code duplication aside,

If it does function as expected will you accept the patch?

Probably.

The menuconfig code you copied is really ugly. Actually, I was refactoring the search jump code a few months ago, but I did not get around to completing it.

Quoted 44 lines

Should I mark the copied codes' original location?

Quoted 14 lines

does this patch work correctly?

$ make defconfig
$ make nconfig

Press F8

Input "MODULES" in the search box.

You will see:

Quoted 7 lines

(1) -> Enable loadable module support

Press the "1" key.

It will navigate to "General setup"
instead of "Enable loadable module support".

Hmm, this is a confusing issue.
It will take you to the parent menu of the option, it should move you to the parent option and
move the cursor to the option location. I will see if I can resolve this oversight.

Thanks,
Jesse Taube

Quoted 10 lines

--
Best Regards
Masahiro Yamada

Signature · 2 lines
Best Regards
Masahiro Yamada
author
#4 lore raw
Quoted 19 lines · Masahiro Yamada
Quoted 17 lines · Jesse T
Quoted 13 lines · Masahiro Yamada
Quoted 8 lines · Jesse Taube

Menuconfig has a feature where you can "press the key in the (#) prefix to jump directly to that location. You will be returned to the current search results after exiting this new menu."

This commit adds this feature to nconfig, with almost identical code.

Setting the code duplication aside,

Quoted 6 lines
Quoted 1 line

If it does function as expected will you accept the patch?

Probably.

The menuconfig code you copied is really ugly.

Yes, I saw and was surprised it was allowed, grandfathered in I guess.

Quoted 2 lines

Actually, I was refactoring the search jump code a few months ago, but I did not get around to completing it.

This isn't on git.kernel.org, would you mind sharing it? If you have any ideas on the best way to implement this, I would love suggestions.

Thanks,
Jesse T

Quoted 58 lines
Quoted 33 lines

Should I mark the copied codes' original location?

Quoted 14 lines

does this patch work correctly?

$ make defconfig
$ make nconfig

Press F8

Input "MODULES" in the search box.

You will see:

Quoted 7 lines

(1) -> Enable loadable module support

Press the "1" key.

It will navigate to "General setup"
instead of "Enable loadable module support".

Quoted 19 lines

Hmm, this is a confusing issue.
It will take you to the parent menu of the option, it should move you to the parent option and
move the cursor to the option location. I will see if I can resolve this oversight.

Thanks,
Jesse Taube

Quoted 10 lines

--
Best Regards
Masahiro Yamada

--
Best Regards
Masahiro Yamada

#5 lore raw
Quoted 21 lines · Jesse T
Quoted 19 lines · Masahiro Yamada
Quoted 17 lines · Jesse T
Quoted 13 lines · Masahiro Yamada
Quoted 8 lines · Jesse Taube

Menuconfig has a feature where you can "press the key in the (#) prefix to jump directly to that location. You will be returned to the current search results after exiting this new menu."

This commit adds this feature to nconfig, with almost identical code.

Setting the code duplication aside,

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

If it does function as expected will you accept the patch?

Probably.

The menuconfig code you copied is really ugly.

Yes, I saw and was surprised it was allowed, grandfathered in I guess.

Yes, you are grandfathered in.

Code refactoring is one of maintainer's jobs.

Quoted 7 lines
Quoted 3 lines

Actually, I was refactoring the search jump code a few months ago, but I did not get around to completing it.

This isn't on git.kernel.org, would you mind sharing it? If you have any ideas on the best way to implement this, I would love suggestions.

Digging into my local repository, it looks like
I had written up functional code already.

I just filled in the commit description.

I will post it.

Quoted 61 lines

Thanks,
Jesse T

Quoted 58 lines
Quoted 33 lines

Should I mark the copied codes' original location?

Quoted 14 lines

does this patch work correctly?

$ make defconfig
$ make nconfig

Press F8

Input "MODULES" in the search box.

You will see:

Quoted 7 lines

(1) -> Enable loadable module support

Press the "1" key.

It will navigate to "General setup"
instead of "Enable loadable module support".

Quoted 19 lines

Hmm, this is a confusing issue.
It will take you to the parent menu of the option, it should move you to the parent option and
move the cursor to the option location. I will see if I can resolve this oversight.

Thanks,
Jesse Taube

Quoted 10 lines

--
Best Regards
Masahiro Yamada

--
Best Regards
Masahiro Yamada

Signature · 2 lines
Best Regards
Masahiro Yamada