bash-completion.sh
branchv_0
changeset 5 accf73de6778
child 7 ff69af3c67a3
equal deleted inserted replaced
4:a0689654b3c2 5:accf73de6778
       
     1 # Relational pipes
       
     2 # Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     3 #
       
     4 # This program is free software: you can redistribute it and/or modify
       
     5 # it under the terms of the GNU General Public License as published by
       
     6 # the Free Software Foundation, version 3 of the License.
       
     7 #
       
     8 # This program is distributed in the hope that it will be useful,
       
     9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    11 # GNU General Public License for more details.
       
    12 #
       
    13 # You should have received a copy of the GNU General Public License
       
    14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    15 
       
    16 _relpipe_in_xmltable_completion() {
       
    17 	local w0 w1 w2 w3
       
    18 
       
    19 	COMPREPLY=()
       
    20 	w0=${COMP_WORDS[COMP_CWORD]}
       
    21 	w1=${COMP_WORDS[COMP_CWORD-1]}
       
    22 	w2=${COMP_WORDS[COMP_CWORD-2]}
       
    23 	w3=${COMP_WORDS[COMP_CWORD-3]}
       
    24 
       
    25 	DATA_TYPE=(
       
    26 		"string"
       
    27 		"integer"
       
    28 		"boolean"
       
    29 	)
       
    30 
       
    31 	# FIXME: user must type " and then press TAB otherwise the completion is broken due to the : colon
       
    32 	#
       
    33 	# can be fixed by global modification of environment variable:
       
    34 	#	COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
       
    35 	# but it will affect other completions (where : is a separator)
       
    36 	#
       
    37 	# these functions should help:
       
    38 	#	_get_comp_words_by_ref -n : cur
       
    39 	#	__ltrim_colon_completions "$cur"
       
    40 	# but was not working (despite w0 renamed to cur)
       
    41 	XMLNS=(
       
    42 		"tag:globalcode.info,2018:relpipe"
       
    43 		"http://www.w3.org/1999/xhtml"
       
    44 		"http://www.w3.org/2000/svg"
       
    45 		"http://www.w3.org/2005/Atom"
       
    46 	)
       
    47 
       
    48 
       
    49 	if   [[ "$w1" == "--relation"      && "x$w0" == "x" ]];    then COMPREPLY=("''")
       
    50 	elif [[ "$w1" == "--relation"      && "x$w0" == "x" ]];    then COMPREPLY=("''")
       
    51 	elif [[ "$w1" == "--records"       && "x$w0" == "x" ]];    then COMPREPLY=("'/'")
       
    52 	elif [[ "$w1" == "--attribute"     && "x$w0" == "x" ]];    then COMPREPLY=("''")
       
    53 	elif [[ "$w2" == "--attribute"                      ]];    then COMPREPLY=($(compgen -W "${DATA_TYPE[*]}" -- "$w0"))
       
    54 	elif [[ "$w3" == "--attribute"     && "x$w0" == "x" ]];    then COMPREPLY=("''")
       
    55 	elif [[ "$w1" == "--namespace"     && "x$w0" == "x" ]];    then COMPREPLY=("''")
       
    56 	elif [[ "$w2" == "--namespace"                      ]];    then COMPREPLY=($(compgen -W "${XMLNS[*]}" -- "$w0"))
       
    57 	else
       
    58 		OPTIONS=(
       
    59 			"--namespace"
       
    60 			"--relation"
       
    61 			"--records"
       
    62 			"--name-is-xpath"
       
    63 			"--attribute"
       
    64 		)
       
    65 		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
       
    66 	fi
       
    67 }
       
    68 
       
    69 complete -F _relpipe_in_xmltable_completion relpipe-in-xmltable