1
0
mirror of https://github.com/Tarrasch/zsh-autoenv.git synced 2024-11-22 07:20:59 +02:00

Add autoenv-edit function to edit current env files (#68)

This commit is contained in:
Daniel Hahler 2017-08-11 14:15:46 +02:00 committed by GitHub
parent aa140ac2a0
commit 0f92983d1e
3 changed files with 108 additions and 0 deletions

View File

@ -135,6 +135,13 @@ Enable debugging. Multiple levels are supported (max 2).
Default: `0`
## Usage
zsh-autoenv works automatically once installed.
You can use ``autoenv-edit`` to edit the nearest/current autoenv files.
It will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.
## Recipes
### Automatically activate Python virtualenvs

View File

@ -355,6 +355,37 @@ _autoenv_get_file_upwards() {
done
}
autoenv-edit() {
local env_file
local -a files
local -A check
check[enter]=$AUTOENV_FILE_ENTER
if [[ "$AUTOENV_FILE_ENTER" != "$AUTOENV_FILE_LEAVE" ]]; then
check[leave]=$AUTOENV_FILE_LEAVE
fi
local f t
for t f in ${(kv)check}; do
env_file="$f"
if ! [[ -f $env_file ]]; then
env_file=$(_autoenv_get_file_upwards . $f)
if [[ -z $env_file ]]; then
echo "No $f file found ($t)." >&2
continue
fi
if ! [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
echo "Note: found $env_file, but AUTOENV_LOOK_UPWARDS is disabled."
fi
fi
files+=($env_file)
done
if [[ -z "$files" ]]; then
return 1
fi
echo "Editing $files.."
local editor
editor="${${AUTOENV_EDITOR:-$EDITOR}:-vim}"
eval $editor "$files"
}
_autoenv_chpwd_handler() {
_autoenv_debug "Calling chpwd handler: PWD=$PWD"

70
tests/autoenv-edit.t Normal file
View File

@ -0,0 +1,70 @@
$ source $TESTDIR/setup.zsh || return 1
$ set | sort > before
$ export EDITOR=echo
$ autoenv-edit
No .autoenv.zsh file found (enter).
No .autoenv_leave.zsh file found (leave).
[1]
$ touch .autoenv.zsh
$ autoenv-edit
No .autoenv_leave.zsh file found (leave).
Editing .autoenv.zsh..
.autoenv.zsh
$ AUTOENV_FILE_LEAVE=$AUTOENV_FILE_ENTER
$ autoenv-edit
Editing .autoenv.zsh..
.autoenv.zsh (glob)
$ mkdir sub
$ cd -q sub
$ autoenv-edit
Editing ../.autoenv.zsh..
../.autoenv.zsh
Supports command with args for EDITOR.
$ export EDITOR='printf file:%s\\n'
$ autoenv-edit
Editing ../.autoenv.zsh..
file:../.autoenv.zsh
Supports alias for EDITOR.
$ alias myeditor_alias='printf file:%s'
$ export EDITOR=myeditor_alias
$ autoenv-edit
Editing ../.autoenv.zsh..
file:../.autoenv.zsh (no-eol)
Falls back to "vim" for EDITOR.
$ alias vim='printf vim_file:%s'
$ unset EDITOR
$ autoenv-edit
Editing ../.autoenv.zsh..
vim_file:../.autoenv.zsh (no-eol)
Note with AUTOENV_LOOK_UPWARDS=0
$ EDITOR=true
$ AUTOENV_LOOK_UPWARDS=0
$ autoenv-edit
Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled.
Editing ../.autoenv.zsh..
$ AUTOENV_FILE_LEAVE=.autoenv_leave.zsh
$ touch ../$AUTOENV_FILE_LEAVE
$ autoenv-edit
Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled.
Note: found ../.autoenv_leave.zsh, but AUTOENV_LOOK_UPWARDS is disabled.
Editing ../.autoenv.zsh ../.autoenv_leave.zsh..
$ touch $AUTOENV_FILE_LEAVE
$ autoenv-edit
Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled.
Editing ../.autoenv.zsh .autoenv_leave.zsh..