]> git.g-eek.se Git - users/gustav/scripts.git/commitdiff
[repo-encrypt] Bash completion added
authorGustav Eek <gustav@fripost.org>
Sat, 18 Apr 2015 21:45:05 +0000 (23:45 +0200)
committerGustav Eek <gustav@fripost.org>
Sat, 18 Apr 2015 22:28:25 +0000 (00:28 +0200)
For the repo-encrypt script, some bash completion is added. The
Makefile is adjusted for to install these scripts. Also the README is
updated with instructions.

Makefile
README
bash_completion.d/repo-encrypt [new file with mode: 0644]

index f7893925bfd5e1148a2b784149df6ccf84233bc5..db29e8d5d9616b98c8228543d3f8df6ea111444c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,15 +8,25 @@ scripts = \
 msmtp-notify \
 repo-encrypt \
 
+bash-completion = \
+repo-encrypt \
+
 dir := $(abspath $(wildcard $(INSTALL)))
+dir-completion := $(dir)/bash_completion.d
 
 default: help
 
-install: $(foreach s, $(scripts), $(dir)/$(s))
+install: $(dir) $(dir-completion) \
+$(foreach f, $(scripts), $(dir)/$(f)) \
+$(foreach f, $(bash-completion), $(dir-completion)/$(f)) \
 
 $(dir)/%: %
        cp -f $< $@
        chmod +x $@
+$(dir-completion)/%: bash_completion.d/%
+       cp -f $< $@
+$(dir) $(dir-completion):
+       mkdir -p $@
 
 help:
        @echo "Run make install or see README."
diff --git a/README b/README
index 70cac425e5ab5bd4225589cb764b5580555c4bf1..a7b3912d841c885137d4d4c52c7c4bd62ffc35ec 100644 (file)
--- a/README
+++ b/README
@@ -3,9 +3,11 @@ This repository holds cusomisation scripts of Gustav.
 Please send patches, bug reports and comments to Gustav Eek
 <gustav@fripost.org>
 
-# Licence
 
-Copyright 2014 Gustav Eek.
+License
+=======
+
+Copyright (c) 2014--2015 Gustav Eek <gustav@fripost.org>.
 
 The script set in this repository is free software: you can
 redistribute it and/or modify it under the terms of the GNU General
@@ -21,22 +23,36 @@ You should have received a copy of the GNU General Public License
 along with this script set. If not, see
 <http://www.gnu.org/licenses/>.
 
-# Configuration
-
-Configural parameters are installation directory. Open Makefile
 
-# Compilation
+Configuration
+=============
 
-No compilation is needed.
+Configurable parameters are installation directory. Edit `Makefile`
+directly.
 
-# Installation
+Installation
+============
 
 For installation type
 
-    $ make install
+       $ make install
+
+For Bash autocompletion on some of the commands, put the following
+lines in your `~/.bash_completion` or `~/.bashrc`
+
+       DIR="$HOME/bin/bash_completion.d"
+       if [[ -d "$DIR" ]]; then
+               for f in $(ls "$DIR"); do
+                       if [[ -f "$DIR/$f" ]]; then
+                               source "$DIR/$f"
+                       fi
+                       done
+               fi
+       unset DIR
 
-# Help
+Help
+====
 
 Some scripts include help command, in that case type
 
-    $ <script> --help
+       $ <script> --help
diff --git a/bash_completion.d/repo-encrypt b/bash_completion.d/repo-encrypt
new file mode 100644 (file)
index 0000000..7d3c81e
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/bash
+#
+# This file provides programmable completion for repo-encrypt under
+# the bash shell
+
+function _repo-encrypt {
+
+    local cmd cur prev line subcmds opts
+    COMPREPLY=()
+    cmd="${1##*/}"
+    cur="${COMP_WORDS[COMP_CWORD]}" # called 'cur' by convention [1]
+    prev="${COMP_WORDS[COMP_CWORD-1]}"
+    line="${COMP_LINE}"
+    subcmds="encrypt decrypt"
+    opts="--help"
+
+    if [[ "$COMP_CWORD" == 1 ]]; then
+       COMPREPLY=($(compgen -W "${subcmds} ${opts}" -- "${cur}"))
+       return 0
+    elif [[ ${COMP_WORDS[1]} == "--help" ]]; then
+       COMPREPLY=(" ") # none empty
+       >&2 printf "\n${cmd}: No completion on '--help'"
+       return 0
+    fi
+
+    return 0
+}
+
+complete -o filenames -o default -F _repo-encrypt repo-encrypt # There are details [2]
+
+# [1]: http://tldp.org/LDP/abs/html/tabexpansion.html
+# [2]: http://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion