Compare commits

..

1 Commits

Author SHA1 Message Date
8fbb496b0d Add autoenv, automatic environment setup
Using a hook, automatically detect when `.enter` and `.exit` scripts
and source them when changing the shells current directory.
2018-04-22 12:14:29 +01:00

View File

@ -11,9 +11,9 @@ _autoenv_entered=()
_autoenv_enter() { _autoenv_enter() {
local entered=$1 local entered=$1
# If entered has already been sourced, return. # If entered has already been sourced, return.
(( ${+_autoenv_entered[$entered]} )) && return (( ${+_autoenv_entered[${_autoenv_entered[(i)$entered]}]} )) && return
# If the enter script is not authorized, return. # If the enter script is not authorized, return.
_autoenv_authorized $entered/.enter || return # TODO: _autoenv_authorized $entered/.enter || return
# Source the enter script. # Source the enter script.
source $entered/.enter source $entered/.enter
# Add the entered directory to the global entered array. # Add the entered directory to the global entered array.
@ -25,11 +25,11 @@ _autoenv_enter() {
_autoenv_exit() { _autoenv_exit() {
local entered=$1 local entered=$1
# If the exit script is not authorized, return. # If the exit script is not authorized, return.
_autoenv_authorized $entered/.exit || return # TODO: _autoenv_authorized $entered/.exit || return
# Source the exit script. # Source the exit script.
source $entered/.exit source $entered/.exit
# Remove the entered directory from the global entered array. # Remove the entered directory from the global entered array.
_autoenv_entered-=$entered _autoenv_entered[${_autoenv_entered[(i)$entered]}]=()
} }
# Find a directory containing a .enter file by searching up the directory tree # Find a directory containing a .enter file by searching up the directory tree
@ -37,7 +37,7 @@ _autoenv_exit() {
_autoenv_find_enter() { _autoenv_find_enter() {
local current=$PWD local current=$PWD
# If an enter script is found in the current directory, return it. # If an enter script is found in the current directory, return it.
[ -f $current/.enter ] && return $current [ -f "$current/.enter" ] && echo $current
# Loop until an enter script or the root directory is found. # Loop until an enter script or the root directory is found.
while true; do while true; do
# Walk up one directory and make the path absolute. # Walk up one directory and make the path absolute.
@ -45,7 +45,7 @@ _autoenv_find_enter() {
# If the current directory equals the next directory, return. # If the current directory equals the next directory, return.
[[ $current == $next ]] && return || local current=$next [[ $current == $next ]] && return || local current=$next
# If an enter script is found in the current directory, return it. # If an enter script is found in the current directory, return it.
[ -f $current/.enter ] && return $current [ -f $current/.enter ] && echo $current
done done
} }
@ -54,21 +54,20 @@ _autoenv_find_enter() {
_autoenv_chpwd() { _autoenv_chpwd() {
local entered local entered
# Loop over the entered directory stack. # Loop over the entered directory stack.
for entered in _autoenv_entered; do for entered in $_autoenv_entered; do
# If the the current directory was previously entered then exit. # If the the current directory was previously entered then exit.
# TODO: Verify what this condition expression actually does. # TODO: Verify what this condition expression actually does.
! [[ $PWD/ == $entered/* ]] && _autoenv_exit $entered ! [[ $PWD/ == $entered/* ]] && _autoenv_exit $entered
done done
# Find the nearest enter script directory. # Find the nearest enter script directory.
local enter=`_autoenv_find_enter` local enter=`_autoenv_find_enter`
# If the enter file doesn't exist, return. # If the enter directory exists, enter it.
! [ -f $enter ] $enter && return [ -d "$enter" ] && _autoenv_enter $enter
_autoenv_enter $enter
} }
# Register the autoenv chpwd hook. # Register the autoenv chpwd hook.
autoload -U add-zsh-hook autoload -U add-zsh-hook
add-zsh-hook _autoenv_chpwd add-zsh-hook chpwd _autoenv_chpwd
# Ensure autoenv is activated in the current directory on first load. # Ensure autoenv is activated in the current directory on first load.
_autoenv_chpwd _autoenv_chpwd