Browse Source

Improve an update tab completion for sfdoc

Alois Mahdal 5 years ago
parent
commit
a11b72dea4
1 changed files with 76 additions and 6 deletions
  1. 76
    6
      src/complete.bash

+ 76
- 6
src/complete.bash View File

@@ -1,25 +1,95 @@
1 1
 
2 2
 __sfpath() {
3
-    local cur prev opts
3
+    local cur       # current word
4
+    local prev      # previous word
5
+    local lopts     # long options
4 6
     COMPREPLY=()
5 7
     cur="${COMP_WORDS[COMP_CWORD]}"
6 8
     prev="${COMP_WORDS[COMP_CWORD-1]}"
7
-    opts="--version --version-semver"
9
+    lopts="--version --version-semver"
8 10
     COMPREPLY=(
9
-        $(compgen -W "$opts" -- ${cur})
11
+        $(compgen -W "$lopts" -- "$cur")
10 12
     )
11 13
 }
12 14
 
13 15
 __sfdoc() {
14
-    local cur prev opts
16
+    local cur       # current word
17
+    local prev      # previous word
18
+    local opts      # options
19
+    local rest      # rest of options
20
+    local cmds      # commands
21
+    local crtext    # text for COMPREPLY
15 22
     COMPREPLY=()
16 23
     cur="${COMP_WORDS[COMP_CWORD]}"
17 24
     prev="${COMP_WORDS[COMP_CWORD-1]}"
18
-    args="--help --debug --include --ls --lsmod --lsfun --lsvar --export --name"
25
+    opts="-d -O -a -I -o --debug --object --all --include --only-from --encoding --name"
26
+    cmds="-l -L -s -e --ls --lsvar --lsfun --which --lsmod --src --export"
27
+    rest="${opts/$prev/}"
28
+    crtext=$(
29
+        case $prev in
30
+            sfdoc)                  echo "$opts $cmds"; __sfdoc_compgen_M2 ;;
31
+            -d|--debug)             echo "$rest $cmds"; __sfdoc_compgen_M3 ;;
32
+            -O|--object)            echo "$rest $cmds"; __sfdoc_compgen_M3 ;;
33
+            -a|--all)               echo "$rest $cmds"; __sfdoc_compgen_M3 ;;
34
+            -I|--include)           compgen -o nospace -d -- "$cur" ;;
35
+            -o|--only-from)         compgen -o nospace -d -- "$cur" ;;
36
+            --encoding)             iconv -l | tr '[:upper:]' '[:lower:]' | sed s://$:: ;;
37
+            --name)                 echo "" ;;
38
+            -e|--export)            echo "markdown manpage pod" ;;
39
+            -s|--src)               __sfdoc_compgen_M3 ;;
40
+            -l|--ls)                __sfdoc_compgen_M2 ;;
41
+            --lsfun|--lsvar)        __sfdoc_compgen_M2 ;;
42
+            --which)                sfdoc --lsmod ;;
43
+            markdown|manpage|pod)   __sfdoc_cwhas "-e" "--export" \
44
+                                      && __sfdoc_compgen_M3  ;;
45
+        esac
46
+    )
47
+    {
48
+        declare -p cur
49
+        declare -p prev
50
+        declare -p COMP_CWORD
51
+        declare -p COMP_WORDS
52
+        declare -p crtext
53
+    } >/tmp/__sfdoc__compgen
19 54
     COMPREPLY=(
20
-        $(compgen -W "$args" -- ${cur})
55
+        $(compgen -W "$crtext" -- "$cur")
21 56
     )
22 57
 }
23 58
 
59
+__sfdoc_compgen_M2() {
60
+    #
61
+    # Produce list of possible MODULE values
62
+    #
63
+    case $cur in
64
+        */*)    compgen -f -o nospace -- "$cur" ;;
65
+        *)      sfdoc --lsmod ;;
66
+    esac
67
+}
68
+
69
+__sfdoc_cwhas() {
70
+    #
71
+    # True if COMP_WORDS already has word $1 or $2...
72
+    #
73
+    local word
74
+    local want
75
+    for want in "$@"; do
76
+        for word in "${COMP_WORDS[@]}"; do
77
+            test "$word" == "$want" && return 0
78
+        done
79
+    done
80
+    return 1
81
+}
82
+
83
+__sfdoc_compgen_M3() {
84
+    #
85
+    # Produce list of possible MODULE values
86
+    #
87
+    if __sfdoc_cwhas "-O" "--object"; then
88
+        sfdoc --ls | cut -d: -f2
89
+    else
90
+        __sfdoc_compgen_M2
91
+    fi
92
+}
93
+
24 94
 complete -F __sfpath sfpath
25 95
 complete -F __sfdoc sfdoc