All of lore.kernel.org
 help / color / mirror / Atom feed
* [maintainer-tools PATCH 1/8] completion: require bash completion package and use it
@ 2016-02-18 16:20 Jani Nikula
  2016-02-18 16:20 ` [maintainer-tools PATCH 2/8] dim: add list-branches subcommand to list nightly branches Jani Nikula
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Jani Nikula @ 2016-02-18 16:20 UTC (permalink / raw)
  To: intel-gfx; +Cc: jani.nikula

The bash completion package makes life a whole lot easier than using the
builtin bash completion features. It's quite likely anyone using
completion in bash already has it installed.

Signed-off-by: Jani Nikula <jani.nikula@intel.com>
---
 bash_completion | 62 ++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 35 insertions(+), 27 deletions(-)

diff --git a/bash_completion b/bash_completion
index e44e5fc844b4..6a3a88cc80f8 100644
--- a/bash_completion
+++ b/bash_completion
@@ -11,7 +11,21 @@ dim ()
 
 _dim ()
 {
-	local cur cmds opts i
+	local args arg cur prev words cword split
+	local cmds
+
+	# require bash-completion with _init_completion
+	type -t _init_completion >/dev/null 2>&1 || return
+
+	_init_completion || return
+
+	COMPREPLY=()
+
+	# arg = subcommand
+	_get_first_arg
+
+	# args = number of arguments
+	_count_args
 
 	if [ -f ~/linux/drm-intel-rerere/nightly.conf ] ; then
 		local nightly_branches=`(source ~/linux/drm-intel-rerere/nightly.conf ; echo $nightly_branches) | \
@@ -35,27 +49,21 @@ _dim ()
 	cmds="$cmds create-branch remove-branch create-workdir for-each-workdirs fw"
 	cmds="$cmds tag-next checker"
 
-	opts="-d -f -i"
-
-	i=1
-
-	COMPREPLY=()   # Array variable storing the possible completions.
-	cur=${COMP_WORDS[COMP_CWORD]}
-
-	for comp in "${COMP_WORDS[@]}" ; do
-		for opt in $opts ; do
-			if [[ $opt = $comp ]] ; then
-				i=$((i+1))
-			fi
-		done
-	done
-
-	if [[ $COMP_CWORD == "$i" ]] ; then
-		COMPREPLY=( $( compgen -W "$cmds $opts" -- $cur ) )
+	if [ -z "${arg}" ]; then
+		# top level completion
+		case "${cur}" in
+			-*)
+				local opts="-d -f -i"
+				COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+				;;
+			*)
+				COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
+				;;
+		esac
 		return 0
 	fi
 
-	case "${COMP_WORDS[i]}" in
+	case "${arg}" in
 		push-branch)
 			COMPREPLY=( $( compgen -W "-f $nightly_branches" -- $cur ) )
 			;;
@@ -69,7 +77,7 @@ _dim ()
 			COMPREPLY=( $( compgen -W "-s" -- $cur ) )
 			;;
 		magic-patch|mp)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -o nospace -W "-a" -- $cur ) )
 			fi
 			;;
@@ -80,34 +88,34 @@ _dim ()
 			# FIXME needs a git sha1
 			;;
 		pull-request)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
-			elif [[ $COMP_CWORD == "$((i+2))" ]] ; then
+			elif [[ $args == 3 ]]; then
 				COMPREPLY=( $( compgen -W "$upstream_branches" -- $cur ) )
 			fi
 			;;
 		pull-request-next|pull-request-fixes|pull-request-next-fixes)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$upstream_branches" -- $cur ) )
 			fi
 			;;
 		create-branch)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -o nospace -W "drm- topic/" -- $cur ) )
 			fi
 			;;
 		checkout|co)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
 			fi
 			;;
 		remove-branch)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches" -- $cur ) )
 			fi
 			;;
 		create-workdir)
-			if [[ $COMP_CWORD == "$((i+1))" ]] ; then
+			if [[ $args == 2 ]]; then
 				COMPREPLY=( $( compgen -W "$nightly_branches all" -- $cur ) )
 			fi
 			;;
-- 
2.1.4

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-02-19 14:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-18 16:20 [maintainer-tools PATCH 1/8] completion: require bash completion package and use it Jani Nikula
2016-02-18 16:20 ` [maintainer-tools PATCH 2/8] dim: add list-branches subcommand to list nightly branches Jani Nikula
2016-02-18 16:20 ` [maintainer-tools PATCH 3/8] dim: add list-upstreams subcommand to list upstream branches Jani Nikula
2016-02-18 16:20 ` [maintainer-tools PATCH 4/8] completion: use the dim helpers to complete nightly and " Jani Nikula
2016-02-18 16:20 ` [maintainer-tools PATCH 5/8] dim: add list-commands subcommand to list all subcommands Jani Nikula
2016-02-18 16:20 ` [maintainer-tools PATCH 6/8] dim: rename alias subcommand to list-aliases Jani Nikula
2016-02-18 16:20 ` [maintainer-tools PATCH 7/8] completion: use the dim helpers to complete subcommands and aliases Jani Nikula
2016-02-18 16:20 ` [maintainer-tools PATCH 8/8] completion: complete aliases like the actual command Jani Nikula
2016-02-19 14:31 ` [maintainer-tools PATCH 1/8] completion: require bash completion package and use it Jani Nikula

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.