Просмотр исходного кода

Auto completion (#20)

* adding auto cmpletion

* Update auto-complete

Co-authored-by: Dastan <dastan@Das.Das>
Dastan-glitch 4 лет назад
Родитель
Сommit
19842c3bdf
2 измененных файлов с 92 добавлено и 0 удалено
  1. 6 0
      README.md
  2. 86 0
      auto-complete

+ 6 - 0
README.md

@@ -54,6 +54,12 @@ $ mkdir -p ~/.config/darkfi
 $ cp -f /usr/local/share/doc/darkfi/*.toml ~/.config/darkfi
 ```
 
+## Bash Completion
+This will add the options auto completion of `drk` and `darkfid`.
+```
+echo source $(pwd)'/auto-complete' >> ~/.bashrc
+```
+
 ## Usage
 
 After the installation, you should have `drk` and `darkfid`

+ 86 - 0
auto-complete

@@ -0,0 +1,86 @@
+#!/usr/bin/env bash
+
+_drk()
+{
+    local cur prev opts
+    COMPREPLY=()
+    cur="${COMP_WORDS[COMP_CWORD]}"
+    prev="${COMP_WORDS[COMP_CWORD-1]}"
+    # don't complet =
+    _init_completion -n = || return
+
+    opts="-h"
+    opts+=" -c "
+    opts+=" -v "
+    opts+=" -V"
+
+    opts+=" deposit"
+    opts+=" features"
+    opts+=" hello"
+    opts+=" help"
+    opts+=" id"
+    opts+=" transfer"
+    opts+=" wallet"
+    opts+=" withdraw"
+
+    # Prevent word reuse
+    lim=$((COMP_CWORD - 1))
+    for i in $( seq 0 $lim )
+    do
+        if [[ $opts == *"${COMP_WORDS[i]}"* ]]; then
+          opts=${opts//${COMP_WORDS[i]}/}
+          opts=${opts//--${COMP_WORDS[i]}/}
+        fi
+    done
+
+#    case $prev in
+#      -v)
+#        opts=$(./drk --verbose | sed -e '$d')
+#        COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
+#        return 0
+#        ;;
+#    esac
+#
+#    case $prev in
+#      --V)
+#        opts=$(./drk --version | sed -e '$d')
+#        COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
+#        return 0
+#        ;;
+#    esac
+
+
+    COMPREPLY=( $(compgen -W '$(_parse_help "$1")' -- "$cur") $(compgen -W "${opts}" -- ${cur}) )
+    [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
+}
+
+
+complete -F _drk drk
+
+_darkfid()
+{
+    local cur prev opts
+    COMPREPLY=()
+    cur="${COMP_WORDS[COMP_CWORD]}"
+    prev="${COMP_WORDS[COMP_CWORD-1]}"
+    _init_completion -n = || return
+
+    opts="-h"
+    opts+=" -c "
+    opts+=" -v "
+    opts+=" -V"
+
+    lim=$((COMP_CWORD - 1))
+    for i in $( seq 0 $lim )
+    do
+        if [[ $opts == *"${COMP_WORDS[i]}"* ]]; then
+          opts=${opts//${COMP_WORDS[i]}/}
+          opts=${opts//--${COMP_WORDS[i]}/}
+        fi
+    done
+
+    COMPREPLY=( $(compgen -W '$(_parse_help "$1")' -- "$cur") $(compgen -W "${opts}" -- ${cur}) )
+    [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
+}
+
+complete -F _darkfid darkfid