Compare commits
	
		
			1 Commits
		
	
	
		
			2e5b8aef8a
			...
			19e4a2b3c8
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 19e4a2b3c8 | 
@ -1,9 +1,7 @@
 | 
				
			|||||||
# Automatically update the environment when the current working directory
 | 
					# Automatically update the environment when the current working directory
 | 
				
			||||||
# changes, this is a reimplementation of the ideas found in the repository
 | 
					# changes, this is a reimplementation of the ideas found in the repository
 | 
				
			||||||
# https://github.com/Tarrasch/zsh-autoenv stripped down to bare essentials.
 | 
					# https://github.com/Tarrasch/zsh-autoenv stripped down to suit my specific
 | 
				
			||||||
#
 | 
					# needs.
 | 
				
			||||||
# The secret sauce can be found at the bottom of this file, where the `chpwd`
 | 
					 | 
				
			||||||
# hook function `_autoenv_chpwd` is added.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Global entered directories array.
 | 
					# Global entered directories array.
 | 
				
			||||||
_autoenv_entered=()
 | 
					_autoenv_entered=()
 | 
				
			||||||
@ -11,19 +9,18 @@ _autoenv_entered=()
 | 
				
			|||||||
# Check if the given file is authorized, if not prompt the user to authorize,
 | 
					# Check if the given file is authorized, if not prompt the user to authorize,
 | 
				
			||||||
# ignore, or view the file. Authorized files and their modified times are
 | 
					# ignore, or view the file. Authorized files and their modified times are
 | 
				
			||||||
# stored in the ~/.cache/autoenv/authorized file to make authorization
 | 
					# stored in the ~/.cache/autoenv/authorized file to make authorization
 | 
				
			||||||
# persistent.
 | 
					# persistant.
 | 
				
			||||||
_autoenv_authorized() {
 | 
					_autoenv_authorized() {
 | 
				
			||||||
  local file=$1
 | 
					  local file=$1
 | 
				
			||||||
  # If autoenv cache directory does not exist, create it.
 | 
					  # If autoenv cache directory does not exist, create it.
 | 
				
			||||||
  local cache_dir=~/.cache/autoenv
 | 
					  local cache_dir=~/.config/autoenv
 | 
				
			||||||
  ! [ -d $cache_dir ] && mkdir -p $cache_dir
 | 
					  ! [ -d $cache_dir ] && mkdir -p $cache_dir
 | 
				
			||||||
  local authorized_file=$cache_dir/authorized
 | 
					  local authorized_file=$cache_dir/authorized
 | 
				
			||||||
  # Define a map to hold authorized key value pairs.
 | 
					  # Define the associative array to hold authorized key value pairs.
 | 
				
			||||||
 | 
					  # If the authorized file exists, load it into an associative array.
 | 
				
			||||||
  typeset -A authorized=()
 | 
					  typeset -A authorized=()
 | 
				
			||||||
  # If the authorized file exists, load it into the map.
 | 
					 | 
				
			||||||
  [ -f $authorized_file ] && typeset -A authorized=(`cat $authorized_file`)
 | 
					  [ -f $authorized_file ] && typeset -A authorized=(`cat $authorized_file`)
 | 
				
			||||||
  # If the given file has been authorized, i.e. the modified time matches that
 | 
					  # If the given file has been authorized, return.
 | 
				
			||||||
  # held in the authorized file, return.
 | 
					 | 
				
			||||||
  local modified_time=`zstat +mtime $file`
 | 
					  local modified_time=`zstat +mtime $file`
 | 
				
			||||||
  [ "$authorized[$file]" = "$modified_time" ] && return
 | 
					  [ "$authorized[$file]" = "$modified_time" ] && return
 | 
				
			||||||
  # Prompt to authorize file.
 | 
					  # Prompt to authorize file.
 | 
				
			||||||
@ -68,18 +65,18 @@ _autoenv_exit() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
# 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
 | 
				
			||||||
# starting in the current directory.
 | 
					# starting in the current directory.
 | 
				
			||||||
_autoenv_find_enter_directories() {
 | 
					_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 ] && echo $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
 | 
				
			||||||
    # Go up one directory and make the path absolute.
 | 
					    # Go up one directory and make the path absolute.
 | 
				
			||||||
    local next=$current/..; local next=${next:A}
 | 
					    local next=$current/..; local next=${next:A}
 | 
				
			||||||
    # If an enter script is found in the current directory, return it.
 | 
					 | 
				
			||||||
    [ -f $next/.enter ] && echo $next
 | 
					 | 
				
			||||||
    # 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.
 | 
				
			||||||
 | 
					    [ -f $current/.enter ] && echo $current
 | 
				
			||||||
  done
 | 
					  done
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -87,16 +84,16 @@ _autoenv_find_enter_directories() {
 | 
				
			|||||||
# setup local environments for directory and its subdirectories.
 | 
					# setup local environments for directory and its subdirectories.
 | 
				
			||||||
_autoenv_chpwd() {
 | 
					_autoenv_chpwd() {
 | 
				
			||||||
  local entered
 | 
					  local entered
 | 
				
			||||||
  # Loop over the reversed entered directory array.
 | 
					  # Loop over the entered directory stack.
 | 
				
			||||||
  for entered in ${(aO)_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.
 | 
				
			||||||
    ! [[ $PWD/ == $entered/* ]] && _autoenv_exit $entered
 | 
					    ! [[ $PWD/ == $entered/* ]] && _autoenv_exit $entered
 | 
				
			||||||
  done
 | 
					  done
 | 
				
			||||||
  # Find all enter script directories, store them in an array.
 | 
					  # Find the nearest enter script directory.
 | 
				
			||||||
  local enter_dirs=(`_autoenv_find_enter_directories`)
 | 
					  local enter=`_autoenv_find_enter`
 | 
				
			||||||
  # Loop over reversed enter script directories array, so enter scripts found
 | 
					  # If the enter directory exists, enter it.
 | 
				
			||||||
  # last are sourced first, then source all enter scripts.
 | 
					  [ -d "$enter" ] && _autoenv_enter $enter
 | 
				
			||||||
  for enter in ${(aO)enter_dirs}; do _autoenv_enter $enter; done
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Register the autoenv chpwd hook.
 | 
					# Register the autoenv chpwd hook.
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user