Skip to content

Commit 6464f53

Browse files
elboulangerokeszybz
authored andcommitted
Add basic bash completion
Signed-off-by: Arnaud Rebillout <[email protected]>
1 parent eafc628 commit 6464f53

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ if udevrulesdir == ''
181181
udevrulesdir = join_paths(udev.get_pkgconfig_variable('udevdir'), 'rules.d')
182182
endif
183183

184+
subdir('shell-completion/bash')
184185
subdir('doc')
185186

186187
############################################################

meson_options.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ option('oss-fuzz', type : 'boolean', value : 'false',
2222
description : 'build against oss-fuzz')
2323
option('llvm-fuzz', type : 'boolean', value : 'false',
2424
description : 'build against LLVM libFuzzer')
25+
26+
option('bashcompletiondir', type : 'string',
27+
description : 'directory for bash completion scripts ["no" disables]')

shell-completion/bash/casync

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# casync(1) completion -*- shell-script -*-
2+
# vim: et sts=4 sw=4
3+
4+
# SPDX-License-Identifier: LGPL-2.1+
5+
6+
in_array() {
7+
local i
8+
for i in "${@:2}"; do
9+
[[ $1 = "$i" ]] && return
10+
done
11+
}
12+
13+
_casync() {
14+
# Assigned variable by _init_completion:
15+
# cur Current argument.
16+
# prev Previous argument.
17+
# words Argument array.
18+
# cword Argument array size.
19+
local cur prev words cword
20+
_init_completion -n = || return
21+
22+
# Commands and options
23+
local cmds=(digest extract gc list make mkdev mount mtree stat)
24+
local opts=(--help --version)
25+
26+
# Check if a command was entered already
27+
local command i
28+
for (( i=0; i < ${#words[@]}-1; i++ )); do
29+
if in_array "${words[i]}" "${cmds[@]}"; then
30+
command=${words[i]}
31+
break
32+
fi
33+
done
34+
35+
# Completion per command (TODO)
36+
if [[ -n $command ]]; then
37+
compopt -o default
38+
COMPREPLY=()
39+
return 0
40+
fi
41+
42+
# Initial completion
43+
case "$cur" in
44+
-*)
45+
COMPREPLY=($(compgen -W "${opts[*]}" -- "$cur"))
46+
return 0
47+
;;
48+
*)
49+
COMPREPLY=($(compgen -W "${cmds[*]}" -- "$cur"))
50+
return 0
51+
;;
52+
esac
53+
54+
} && \
55+
complete -F _casync casync

shell-completion/bash/meson.build

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-License-Identifier: LGPL-2.1+
2+
3+
bashcompletiondir = get_option('bashcompletiondir')
4+
if bashcompletiondir == ''
5+
bash_completion = dependency('bash-completion', required : false)
6+
if bash_completion.found()
7+
bashcompletiondir = bash_completion.get_pkgconfig_variable('completionsdir')
8+
else
9+
bashcompletiondir = join_paths(datadir, 'bash-completion/completions')
10+
endif
11+
endif
12+
13+
if bashcompletiondir != 'no'
14+
install_data('casync', install_dir : bashcompletiondir)
15+
endif

0 commit comments

Comments
 (0)