aboutsummaryrefslogtreecommitdiff
path: root/bash_completion.in
blob: 8a931efcc21331f698b445544167d30399806d09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
#
# bash completion script for ctct
#
# Copyright 2015 - 2018 Einhard Leichtfuß
#
# This file is part of ctct.
#
# ctct is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ctct is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with ctct.  If not, see <https://www.gnu.org/licenses/>.
#

_ctct()
{
	## DEFAULT SETTINGS:
	local sysconfdir="@sysconfdir_expanded@"
	local user_config_file="@default_user_config_file@"
	local datadir="@default_datadir@"
	
	local temp
	
	## USER SETTINGS:
	test -f "$sysconfdir/ctct_config" \
		&& source "$sysconfdir/ctct_config"
	test -f "$user_config_file" \
		&& source "$user_config_file"

	local cur action opts
	typeset -i i
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	test $COMP_CWORD -gt 1 && action="${COMP_WORDS[1]}"

	if [ $COMP_CWORD -eq 1 ] && [[ "$cur" =~ ^- ]]
	then
		COMPREPLY=( $(compgen -W \
			"-h -l -s -S -e -d \
			--help --list-all --search-by-name --search-by-data --edit --delete \
			--rename --version" \
		-- "${cur}") )
	elif [ $COMP_CWORD -eq 1 ] \
		|| ( [ $COMP_CWORD -eq 2 ] && \
				 ( [ "$action" = "-e" ] \
				|| ( [[ "--edit" =~ ^"$action" ]] && [ ${#action} -ge 3 ] ) \
				|| ( [[ "--rename" =~ ^"$action" ]] && [ ${#action} -ge 3 ] ))) \
		|| [ "$action" = "-d" ] \
		|| ( [[ "--delete" =~ ^"$action" ]] && [ ${#action} -ge 3 ] )
	then
		i=0
		shopt -s nullglob
		
		for file in "$HOME/.ctct"/"$cur"*
		do
			COMPREPLY[i++]="${file##*/}"
		done
		
		if [[ "$cur" =~ \. ]]
		then
			cur=( ${cur/\./ } )
			for file in "$HOME/.ctct"/"${cur[1]}"*."${cur[0]}"
			do
				file="${file##*/}"
				COMPREPLY[i++]="${cur[0]}.${file%.*}"
			done
		else
			for file in "$HOME/.ctct"/*."$cur"*
			do
				file="${file##*/}"
				file=( ${file/\./ } )
				COMPREPLY[i++]="${file[1]}.${file[0]}"
			done
		fi
		
		shopt -u nullglob
	fi

	return 0
}

complete -F _ctct ctct

# vi: ts=2 sw=2