1#! /bin/bash
2# Bash completion setup for Eclat commands.
3# Copyright (C) 2012-2018 Sergey Poznyakoff
4#
5# Eclat is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3, or (at your option)
8# any later version.
9#
10# Eclat is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Eclat.  If not, see <http://www.gnu.org/licenses/>.
17
18comp_eclat() {
19        # Obtain a list of command line options.  Each option occupies
20	# a single line and is followed by an equals sign if it takes
21	# an argument.
22	local optlist=$(eclat --usage|sed '1s/^[^[]*//' |
23                          tr -d '\n' |
24                          tr -s ' ' |
25                          sed 's/\] \[/:/g;' |
26                          tr -d '[]' |
27                          awk -F: '
28                           function optproc(s,   i,n,a) {
29                         	if (match(s, /^-[a-zA-Z][a-zA-Z]/)) {
30                         	  n = split(s, a, "");
31                         	  for (i = 2; i <= n; i++)
32                         	    print "-" a[i];
33                         	} else if (match(s, /^-[a-zA-Z] ./))
34                         	  print substr(s,1,2) "=";
35                         	else {
36                                   if (match(s, /=/)) {
37                         	    opt="="
38                         	    sub(/=.*/,"",s)
39                         	  } else
40                         	    opt=""
41                         	  n = split(s, a, " ");
42                                   for (i = 1; i <= n; i++) {
43                         	    sub(/,$/,"",a[i])
44                         	    print a[i] opt
45                         	  }
46                         	}
47                           }
48                           { for (i = 1; i < NF; i++) optproc($i) }')
49
50	# Bail out if we're sitting on a word starting with a dash, i.e.
51	# an option.  Otherwise, make sure no command appears in the command
52	# line to the left of the current word.  If not, we're trying to
53	# complete a command, so proceed safely.
54	COMPREPLY=()
55	local opt arg=${COMP_WORDS[$COMP_CWORD]}
56	case $arg in
57	-*) return
58	esac
59	for i in $(seq $(($COMP_CWORD - 1)) -1 1)
60	do
61		opt=${COMP_WORDS[$i]}
62		case $opt in
63		-*) if test -n "$arg"; then
64		      if echo "$optlist" | grep -q "^$opt.*="; then
65		        test $i -eq $(($COMP_CWORD - 1)) && return
66		        arg=
67		      else
68		        return
69		      fi
70		    fi
71		    ;;
72		*)  if test -n "$arg" && test $i -lt $(($COMP_CWORD - 1)); then
73		      return
74		    else
75		      arg=$opt
76		    fi
77		esac
78	done
79	# Finally, find matching commands.
80	COMPREPLY=( $(eclat --match-commands ${COMP_WORDS[$COMP_CWORD]}) )
81}
82complete -F comp_eclat -o default eclat
83