OILS / devtools / completion-demo.bash View on Github | oilshell.org

54 lines, 23 significant
1#!/bin/bash
2#
3# Demo of bash completion fallback. Hm. -D is not chained, and neither is -F
4# -F.
5#
6# Usage:
7# $ bash --norc --noprofile
8# $ . completion-demo.bash
9
10# NOOP
11fnone() {
12 echo -n ''
13}
14
15f12() {
16 COMPREPLY=(f1 f2)
17}
18
19f34() {
20 COMPREPLY=(f3 f4)
21}
22
23complete-file() {
24 local cur="${COMP_WORDS[COMP_CWORD]}"
25 # Hm no trailing slash here.
26 COMPREPLY=( $(compgen -A file -- "${cur}") )
27}
28
29# Use -X to filter
30complete-sh() {
31 local cur="${COMP_WORDS[COMP_CWORD]}"
32 # Hm no trailing slash here.
33 COMPREPLY=( $(compgen -A file -X '!*.sh' -o plusdirs -- "${cur}") )
34}
35
36# default completion
37complete -F f12 -F f34 -D
38
39# empty completion
40# Oops, does NOT fall back on f34
41complete -F f12 -F f34 -F fnone -E
42
43# Directory names will be completed with trailing slash; this is default readline behavior.
44complete -A file foo
45
46# Hm no trailing slash here. Lame.
47complete -F complete-sh bar
48
49# Aha! This adds trailing slash. The problem is that if you are completing
50# with -D, you may or may not be completing with a file! Need to use comopt?
51complete -F complete-sh -o filenames -o bashdefault barf
52
53echo 'Installed completions'
54