All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Schneider-Pargmann <msp@baylibre.com>
To: Masahiro Yamada <masahiroy@kernel.org>,
	Markus Schneider-Pargmann <msp@baylibre.com>
Cc: linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org
Subject: [RFC 2/2] scripts: Add fzfconfig helper script
Date: Wed, 27 Mar 2024 15:25:44 +0100	[thread overview]
Message-ID: <20240327142544.1728286-3-msp@baylibre.com> (raw)
In-Reply-To: <20240327142544.1728286-1-msp@baylibre.com>

Add a script to present all config options in fzf. This allows to fuzzy
search in all config options and their help texts. It also displays the
configuration state in the list. A preview window shows the actual
Kconfig snippet for the config option.

Using 'Enter' in the displayed list will open 'make menuconfig' and
automatically execute a search for the selected symbol. After that the
user can use the menuconfig to change the option or do other things.
After exiting menuconfig the fzf list is refreshed and you can continue
navigating the list from the point where you entered menuconfig.

Signed-off-by: Markus Schneider-Pargmann <msp@baylibre.com>
---
 scripts/fzfconfig        | 112 +++++++++++++++++++++++++++++++++++++++
 scripts/kconfig/Makefile |   4 ++
 2 files changed, 116 insertions(+)
 create mode 100755 scripts/fzfconfig

diff --git a/scripts/fzfconfig b/scripts/fzfconfig
new file mode 100755
index 000000000000..48f9590c1031
--- /dev/null
+++ b/scripts/fzfconfig
@@ -0,0 +1,112 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Author: Markus Schneider-Pargmann <msp@baylibre.com>
+#
+# List all config options inside of fzf and offer an easy option to jump to the
+# option in menuconfig
+
+config_script="scripts/config --file $KCONFIG_CONFIG"
+
+history_file="$HOME/.cache/kconfig_fzf.hist"
+
+kconfig_fzf_clenup_on_exit() {
+  rm -f "$menucfgpipe"
+}
+
+kconfig_fzf_oneline_config_items() {
+  make helpallconfig | \
+    grep -vE '^(Type  :|  Depends on:|  Visible if:|Selects:|Selected by|CONFIG_)' | \
+    sed 's/^  Prompt: \(.*\)$/1111111111\11111111111/g' | \
+    tr '\n' ' ' | \
+    sed 's/----- -----/\n/g' | \
+    sed 's/\(^ *\|-----\)//g' | \
+    sed 's/  */ /g' | \
+    sed 's/^\(.*\)1111111111\(.*\)1111111111\(.*\)/\t\2:\1\3/g' | \
+    sed 's/^\([^\t].*\)$/\t:\1/g' | \
+    sed 's/^\(.*\)Symbol: \([^ ]*\) \[\([^]]\{1,40\}\)[^]]*\].*Defined at \([^:]*\):\([0-9]*\) .*$/\4:\5\t:\2\3:\1/g' | \
+    grep -E '^.*:[0-9]*	:.*=.*:'
+}
+
+kconfig_fzf_make_menuconfig() {
+  menucfgpipe="$(mktemp --dry-run)"
+  trap kconfig_fzf_clenup_on_exit EXIT
+  mkfifo "$menucfgpipe"
+
+  make menuconfig < "$menucfgpipe" &
+  menuconfigpid=$!
+
+  echo "/^${1}\$" > "$menucfgpipe"
+
+  cat > "$menucfgpipe" < /dev/stdin &
+  redirectpid=$!
+
+  wait $menuconfigpid
+  kill $redirectpid
+}
+
+kconfig_fzf_get_catcmd() {
+  for cmd in bat batcat
+  do
+    if which $cmd > /dev/null
+    then
+      echo $cmd \
+        --paging=never \
+        --force-colorization \
+        --highlight-line {2}
+      return
+    fi
+  done
+  echo 'echo -e "+----------\n| "File: {1}:{2}"\n+----------"; cat --number'
+  exit 0
+}
+
+kconfig_fzf_command() {
+  menuconfig_action="execute(bash -c \"source $0;"' kconfig_fzf_make_menuconfig \$(echo {3} | cut -d '=' -f 1)")'
+  reload_action="reload(bash -c 'source $0; kconfig_fzf_oneline_config_items')"
+  catcmd="$(kconfig_fzf_get_catcmd)"
+
+  kconfig_fzf_oneline_config_items | column --table --separator '	' | \
+    env SHELL=/bin/bash \
+    fzf \
+      --history="$history_file" \
+      --tiebreak=begin \
+      --delimiter=: \
+      --nth=.. \
+      --with-nth=3,4 \
+      --preview "$catcmd"' \
+        "${srctree:-.}/"{1}' \
+      --preview-window 'right,90,+{2}-5,~3' \
+      --bind "ctrl-r:${reload_action}" \
+      --bind "ctrl-g:execute(echo \"'{}'\"; read)" \
+      --bind "enter:${menuconfig_action}+${reload_action}" \
+      --header 'enter: Open in menuconfig, ctrl-r: Reload, esc: Exit'
+    if [ "$?" -eq 130 ]
+    then
+      return 0
+    fi
+    return "$ret"
+}
+
+if [ "$#" -gt "0" ]
+then
+  echo "USAGE: $0"
+  echo ""
+  echo "Show all kconfig symbols in fzf and open selected item on enter in make menuconfig."
+  echo ""
+  echo "You can fuzzy search in these data fields: <KCONFIG_FILE>:<LINENO>:<CONFIG_SYMBOL>=<VALUE>:<PROMPT>:<HELP>."
+  echo "Not all fields are visible but the search is still done."
+  echo "A preview of the Kconfig file is shown on the right side."
+  echo ""
+  echo "Key bindings:"
+  echo "  Enter : Open symbol in make menuconfig"
+  echo "  CTRL-r: Reload config symbols"
+  echo "  Escape: Exit"
+  exit 0
+fi
+
+(return 0 2>/dev/null) && kconfig_fzf_sourced=1
+if [ "$kconfig_fzf_sourced" != "1" ]
+then
+  kconfig_fzf_command
+fi
diff --git a/scripts/kconfig/Makefile b/scripts/kconfig/Makefile
index 87df82c03afb..1f47b9ae9786 100644
--- a/scripts/kconfig/Makefile
+++ b/scripts/kconfig/Makefile
@@ -124,6 +124,10 @@ testconfig: $(obj)/conf
 	$(if $(findstring 1,$(KBUILD_VERBOSE)),--capture=no)
 clean-files += tests/.cache
 
+PHONY += fzfconfig
+fzfconfig:
+	$(srctree)/scripts/fzfconfig
+
 # Help text used by make help
 help:
 	@echo  'Configuration targets:'
-- 
2.43.0


      parent reply	other threads:[~2024-03-27 14:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-27 14:25 [RFC 0/2] kconfig: Add fzf fuzzy search for config options Markus Schneider-Pargmann
2024-03-27 14:25 ` [RFC 1/2] kconfig: Add helpallconfig Markus Schneider-Pargmann
2024-03-27 14:25 ` Markus Schneider-Pargmann [this message]

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=20240327142544.1728286-3-msp@baylibre.com \
    --to=msp@baylibre.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@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.