]> Sergey Matveev's repositories - zsh-autoenv.git/commitdiff
Add autoenv-edit function to edit current env files (#68)
authorDaniel Hahler <github@thequod.de>
Fri, 11 Aug 2017 12:15:46 +0000 (14:15 +0200)
committerGitHub <noreply@github.com>
Fri, 11 Aug 2017 12:15:46 +0000 (14:15 +0200)
README.md
autoenv.zsh
tests/autoenv-edit.t [new file with mode: 0644]

index e6cbaf7594ab577b8d3ff62ecb3022d884da538b..fc86547bfda26fad387928b6203e8626940abf37 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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
index c7a2999be5ea2f4d8348dcdf960f2e55b3605631..195c0da95c600aef18a2f979b894b74315be1611 100644 (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"
diff --git a/tests/autoenv-edit.t b/tests/autoenv-edit.t
new file mode 100644 (file)
index 0000000..52c3d07
--- /dev/null
@@ -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..