All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jesse Taube <mr.bossman075@gmail.com>
To: linux-kbuild@vger.kernel.org
Cc: Masahiro Yamada <masahiroy@kernel.org>,
	Jesse Taube <Mr.Bossman075@gmail.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Stephen Boyd <sboyd@kernel.org>
Subject: [PATCH v1] scripts: kconfig: nconf: Add search jump feature
Date: Sat, 17 Jun 2023 20:16:46 -0400	[thread overview]
Message-ID: <20230618001646.1228521-1-Mr.Bossman075@gmail.com> (raw)

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.

Signed-off-by: Jesse Taube <Mr.Bossman075@gmail.com>
---
 scripts/kconfig/nconf.c     | 79 +++++++++++++++++++++++++++++++++++--
 scripts/kconfig/nconf.gui.c | 31 ++++++++++++---
 scripts/kconfig/nconf.h     |  5 +++
 3 files changed, 106 insertions(+), 9 deletions(-)

diff --git a/scripts/kconfig/nconf.c b/scripts/kconfig/nconf.c
index 3ba8b1af390f..cbed2e15c579 100644
--- a/scripts/kconfig/nconf.c
+++ b/scripts/kconfig/nconf.c
@@ -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);
 }
 
diff --git a/scripts/kconfig/nconf.gui.c b/scripts/kconfig/nconf.gui.c
index 9aedf40f1dc0..44ab0e43c626 100644
--- a/scripts/kconfig/nconf.gui.c
+++ b/scripts/kconfig/nconf.gui.c
@@ -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;
 }
diff --git a/scripts/kconfig/nconf.h b/scripts/kconfig/nconf.h
index 6f925bc74eb3..747b30a4cd0f 100644
--- a/scripts/kconfig/nconf.h
+++ b/scripts/kconfig/nconf.h
@@ -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);
-- 
2.40.0


             reply	other threads:[~2023-06-18  0:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-18  0:16 Jesse Taube [this message]
2023-06-24  8:11 ` [PATCH v1] scripts: kconfig: nconf: Add search jump feature Masahiro Yamada
2023-06-24 20:36   ` Jesse T
2023-06-27  0:37     ` Masahiro Yamada
2023-06-27  3:08       ` Jesse T
2023-06-29 15:50         ` Masahiro Yamada

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230618001646.1228521-1-Mr.Bossman075@gmail.com \
    --to=mr.bossman075@gmail.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=sboyd@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.