]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shell-completion/*/lsmod: add bash/fish/zsh completion
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Sun, 22 Sep 2024 21:11:57 +0000 (16:11 -0500)
Note that completions are explicitly aimed to be simple, depending on as
little as possible shell specific helpers.

The goal is that people unfamiliar with these can extend them with zero
ramp-up. Doing things efficiently or "properly" is somewhat secondary.

v2:
 - wire the completions to the autotools build

v3:
 - use SPDX style copyright statements

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Makefile.am
configure.ac
meson.build
meson_options.txt
shell-completion/bash/lsmod [new file with mode: 0644]
shell-completion/fish/lsmod.fish [new file with mode: 0644]
shell-completion/zsh/_lsmod [new file with mode: 0644]

index 6bbe23d0bc1802cbf7dd87c63b89538b504765f6..b5ff612cdf3eb642deb41769e3e7fb9a3ba1f8e5 100644 (file)
@@ -125,7 +125,16 @@ noarch_pkgconfig_DATA = tools/kmod.pc
 
 bashcompletiondir=@bashcompletiondir@
 dist_bashcompletion_DATA = \
-       shell-completion/bash/kmod
+       shell-completion/bash/kmod \
+       shell-completion/bash/lsmod
+
+fishcompletiondir=@fishcompletiondir@
+dist_fishcompletion_DATA = \
+       shell-completion/fish/lsmod.fish
+
+zshcompletiondir=@zshcompletiondir@
+dist_zshcompletion_DATA = \
+       shell-completion/zsh/_lsmod
 
 install-exec-hook:
 if BUILD_TOOLS
@@ -338,7 +347,9 @@ EXTRA_DIST += testsuite/rootfs-pristine
 
 AM_DISTCHECK_CONFIGURE_FLAGS=--enable-gtk-doc --sysconfdir=/etc \
        --with-zlib --with-zstd --with-xz --with-openssl \
-       --with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir)
+       --with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir) \
+       --with-fishcompletiondir=$$dc_install_base/$(fishcompletiondir) \
+       --with-zshcompletiondir=$$dc_install_base/$(zshcompletiondir)
 
 distclean-local: $(DISTCLEAN_LOCAL_HOOKS)
 
index c422cbb1ebc01a78b765bb3d484f7e1fbbb292c5..c204570d942cdf5d7b7d4544d9b2e9d59bf5bb4c 100644 (file)
@@ -163,6 +163,22 @@ AC_ARG_WITH([bashcompletiondir],
        ])])
 AC_SUBST([bashcompletiondir], [$with_bashcompletiondir])
 
+AC_ARG_WITH([fishcompletiondir],
+       AS_HELP_STRING([--with-fishcompletiondir=DIR], [Fish completions directory]),
+       [],
+       [AS_IF([$($PKG_CONFIG --exists fish)], [
+               with_fishcompletiondir=$($PKG_CONFIG --variable=completionsdir fish)
+       ] , [
+               with_fishcompletiondir=${datadir}/fish/vendor_functions.d
+       ])])
+AC_SUBST([fishcompletiondir], [$with_fishcompletiondir])
+
+AC_ARG_WITH([zshcompletiondir],
+       AS_HELP_STRING([--with-zshcompletiondir=DIR], [Zsh completions directory]),
+       [],
+       [with_zshcompletiondir=${datadir}/zsh/site-functions])
+AC_SUBST([zshcompletiondir], [$with_zshcompletiondir])
+
 #####################################################################
 # --enable-
 #####################################################################
index b4b6c5cde0e8f6ea4dedb1d7a5c331def0ffb9f0..f45fb9bc83290fa558316869da9d662369870850 100644 (file)
@@ -191,16 +191,45 @@ if moduledir == ''
 endif
 cdata.set_quoted('MODULE_DIRECTORY', moduledir)
 
-bashcompletiondir = get_option('bashcompletiondir')
-if bashcompletiondir == ''
-  bashcompletion = dependency('bash-completion', required : false)
-  if bashcompletion.found()
-    bashcompletiondir = bashcompletion.get_variable(pkgconfig : 'completionsdir')
-  else
-    bashcompletiondir = join_paths(get_option('prefix'), get_option('datadir'),
-                                   'bash-completion/completions')
+_completiondirs = [
+  ['bashcompletiondir', 'bash-completion', 'bash-completion/completions', 'shell-completion/bash/@0@'],
+  ['fishcompletiondir', 'fish',            'fish/vendor_functions.d',     'shell-completion/fish/@0@.fish'],
+  ['zshcompletiondir',  '',                'zsh/site-functions',          'shell-completion/zsh/_@0@'],
+]
+
+foreach tuple : _completiondirs
+  dir_option = tuple[0]
+  pkg_dep = tuple[1]
+  def_path = tuple[2]
+  ins_path = tuple[3]
+
+  completiondir = get_option(dir_option)
+  if completiondir == ''
+    completion = dependency(pkg_dep, required : false)
+    if completion.found()
+      completiondir = completion.get_variable(pkgconfig : 'completionsdir')
+    else
+      completiondir = join_paths(get_option('prefix'), get_option('datadir'),
+                                 def_path)
+    endif
   endif
-endif
+
+  _completions = [
+    'lsmod',
+  ]
+
+  if completiondir != 'no'
+    foreach comp : _completions
+      install_data(
+        files(ins_path.format(comp)),
+        install_dir : completiondir,
+      )
+    endforeach
+  endif
+
+  # NEEDED solely for bash/kmod below
+  set_variable(dir_option, completiondir)
+endforeach
 
 if bashcompletiondir != 'no'
   install_data(
index 62d97ceb0cd6ea22a0373bdd2449ea10742772e9..4a25fb0ea08480c4dfaa0fd8b0c8888698106fcf 100644 (file)
@@ -18,6 +18,18 @@ option(
   description : 'Bash completions directory. Use "no" to disable.',
 )
 
+option(
+  'fishcompletiondir',
+  type : 'string',
+  description : 'Fish completions directory. Use "no" to disable.',
+)
+
+option(
+  'zshcompletiondir',
+  type : 'string',
+  description : 'Zsh completions directory. Use "no" to disable.',
+)
+
 # Compression options
 option(
   'zstd',
diff --git a/shell-completion/bash/lsmod b/shell-completion/bash/lsmod
new file mode 100644 (file)
index 0000000..09d516e
--- /dev/null
@@ -0,0 +1,30 @@
+# lsmod(8) completion                                       -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+#
+# Formatted using:
+# shfmt --language-dialect bash --indent 4 --func-next-line
+
+_lsmod()
+{
+    # long/short opt pairs
+    local -A opts=(
+        ['syslog']='s'
+        ['verbose']='v'
+        ['version']='V'
+        ['help']='h'
+    )
+
+    local cur="${COMP_WORDS[COMP_CWORD]}"
+
+    if [[ $cur == --* ]]; then
+        COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
+    elif [[ $cur == -* ]]; then
+        if (( ${#cur} != 2 )); then
+            COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
+        fi
+        COMPREPLY+=($(compgen -P '-' -W "${opts[*]}" -- "${cur:1}"))
+    fi
+} &&
+    complete -F _lsmod lsmod
diff --git a/shell-completion/fish/lsmod.fish b/shell-completion/fish/lsmod.fish
new file mode 100644 (file)
index 0000000..de0e071
--- /dev/null
@@ -0,0 +1,12 @@
+# lsmod(8) completion                                       -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+
+# globally disable file completions
+complete -c lsmod -f
+
+complete -c lsmod -s s -l syslog  -d 'print to syslog, not stderr'
+complete -c lsmod -s v -l verbose -d 'enables more messages'
+complete -c lsmod -s V -l version -d 'show version'
+complete -c lsmod -s h -l help    -d 'show this help'
diff --git a/shell-completion/zsh/_lsmod b/shell-completion/zsh/_lsmod
new file mode 100644 (file)
index 0000000..5a05038
--- /dev/null
@@ -0,0 +1,12 @@
+#compdef lsmod
+
+# lsmod(8) completion                                       -*- shell-script -*-
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# SPDX-FileCopyrightText: 2024 Emil Velikov <emil.l.velikov@gmail.com>
+
+_arguments \
+    {-s,--syslog}'[print to syslog, not stderr]' \
+    {-v,--verbose}'[enables more messages]' \
+    {-V,--version}'[show version]' \
+    {-h,--help}'[show this help]'