autoenv: Automatically update autoenv during edit

Fixes #18 by exiting the autoenv before invoking vim for editing the
`.enter` and `.exit` scripts, then enters the autoenv after vim exits.
This reduces the number of steps required to update the current autoenv
state.
This commit is contained in:
Kenneth Benzie 2019-05-30 19:19:40 +01:00
parent 0da54801e4
commit 7a0c73dabb

View File

@ -23,37 +23,48 @@ commands:
deinit remove .enter and .exit scripts in current directory" deinit remove .enter and .exit scripts in current directory"
;; ;;
init) # Create enter and exit scripts in current directory. init) # Create .enter and .exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
echo '.enter or .exit already exists'; return 1 echo '.enter or .exit already exists'; return 1
fi fi
# Create then edit enter and exit scripts. # Create then edit .enter and .exit scripts.
touch .enter .exit && autoenv edit touch .enter .exit && autoenv edit
# If enter script exists, authorize it. # If enter script exists, authorize it.
[ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes [ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes
# If exit script exists, authorize it. # If exit script exists, authorize it.
[ -f $PWD/.exit ] && _autoenv_authorized $PWD/.exit yes [ -f $PWD/.exit ] && _autoenv_authorized $PWD/.exit yes
# Enter the new autoenv. # Enter the autoenv.
_autoenv_enter $PWD ;; _autoenv_enter $PWD ;;
edit) # Edit enter and exit scripts in current directory. edit) # Edit .enter and .exit scripts in current directory.
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
echo '.enter or .exit not found'; return 1 echo '.enter or .exit not found'; return 1
fi fi
# If vim exists, edit enter and exit scripts. # If vim exists, edit enter and exit scripts.
if which vim &> /dev/null; then if which vim &> /dev/null; then
vim -p $PWD/.enter $PWD/.exit # Exit the autoenv before editing.
_autoenv_exit $PWD
if vim -p $PWD/.enter $PWD/.exit; then
# If enter script exists, authorize it.
[ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes
# If exit script exists, authorize it.
[ -f $PWD/.exit ] && _autoenv_authorized $PWD/.exit yes
fi
# Enter the autoenv.
_autoenv_enter $PWD
else else
echo 'vim not found'; return 1 echo 'vim not found'; return 1
fi ;; fi ;;
deinit) # Remove enter and exit scripts in current directory. deinit) # Remove .enter and .exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
# Prompt user to confirm removal of enter and exit scripts. # Prompt user to confirm removal of enter and exit scripts.
while true; do while true; do
read "answer?Are you sure [y/N]? " read "answer?Are you sure [y/N]? "
case "$answer" in case "$answer" in
y|Y|yes) y|Y|yes)
# Exit the autoenv.
_autoenv_exit $PWD
# Remove enter and exit scripts if they exist. # Remove enter and exit scripts if they exist.
[ -f $PWD/.enter ] && rm $PWD/.enter [ -f $PWD/.enter ] && rm $PWD/.enter
[ -f $PWD/.exit ] && rm $PWD/.exit [ -f $PWD/.exit ] && rm $PWD/.exit