aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Ryabitsev <konstantin@linuxfoundation.org>2024-03-06 14:46:14 -0500
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2024-03-06 14:46:14 -0500
commit333189c0b727676acb3d99e9634d9a2b69e3cdd0 (patch)
tree9680b91ae3a3282e59f35d0f3b6001148ff41e85
parenta8e930c9f3bffcc58347349f41c239cb943e2b1f (diff)
downloadb4-333189c0b727676acb3d99e9634d9a2b69e3cdd0.tar.gz
Add tab-completion generation with shtab
Make it easy to generate tab-completion files for bash, tcsh and zsh using python's shtab module. E.g., to generate for bash: ./misc/tc-generate.sh bash > /tmp/b4 sudo cp /tmp/b4 /usr/share/bash-completion/completions/b4 This builds on the idea proposed by Emil Velikov, but uses the CLI approach in order not to add the optional shtab dependency to the project. Co-Authored-By: Emil Velikov <emil.l.velikov@gmail.com> Link: https://lore.kernel.org/tools/20240301-shell-completion-v2-1-741c88669859@gmail.com Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
-rwxr-xr-xmisc/tc-generate.sh33
1 files changed, 33 insertions, 0 deletions
diff --git a/misc/tc-generate.sh b/misc/tc-generate.sh
new file mode 100755
index 0000000..cb9545c
--- /dev/null
+++ b/misc/tc-generate.sh
@@ -0,0 +1,33 @@
+#!/usr/bin/env bash
+# Generate tab-complete files for use with bash, zsh, tcsh
+# E.g.:
+# ./misc/tc-generate.sh bash > /tmp/b4
+# sudo cp /tmp/b4 /usr/share/bash-completion/completions/b4
+#
+# Requires shtab (python3-shtab)
+#
+if [[ -z $1 ]]; then
+ echo "Specify the shell type as parameter (bash, zsh, tcsh)"
+ exit 1
+fi
+
+REAL_PATH=$(realpath -e ${BASH_SOURCE[0]})
+PROJ_TOP="${PROJ_TOP:-$(dirname ${REAL_PATH})}"
+while [[ ${PROJ_TOP} != "/" ]]; do
+ if [[ -d ${PROJ_TOP}/src/b4 ]]; then
+ break
+ fi
+ PROJ_TOP=$(dirname ${PROJ_TOP})
+done
+if [[ $PROJ_TOP == "/" ]]; then
+ echo "Please run me from the b4 project directory."
+ exit 1
+fi
+
+if which shtab >/dev/null 2>&1; then
+ PYTHONPATH="${PROJ_TOP}/src${PYTHONPATH:+:$PYTHONPATH}" \
+ shtab --shell=$1 -u b4.command.setup_parser
+else
+ echo "Install shtab to generate tab-completion files."
+ exit 1
+fi \ No newline at end of file