Compare commits
1 Commits
51f3f7fab4
...
19dda8187c
Author | SHA1 | Date | |
---|---|---|---|
19dda8187c |
@ -1,6 +1,5 @@
|
|||||||
#compdef autoenv
|
#compdef autoenv
|
||||||
|
|
||||||
# Completion for the autoenv command.
|
|
||||||
_autoenv() {
|
_autoenv() {
|
||||||
local ret=1 context curcontext="$curcontext" state line
|
local ret=1 context curcontext="$curcontext" state line
|
||||||
typeset -A opt_args
|
typeset -A opt_args
|
||||||
|
@ -5,11 +5,38 @@
|
|||||||
# The secret sauce can be found at the bottom of this file, where the `chpwd`
|
# The secret sauce can be found at the bottom of this file, where the `chpwd`
|
||||||
# hook function `_autoenv_chpwd` is added.
|
# hook function `_autoenv_chpwd` is added.
|
||||||
|
|
||||||
# The autoenv command provides a convenient way to create, edit, and remove
|
|
||||||
# enter and exit scripts in the current directory.
|
|
||||||
autoenv() {
|
autoenv() {
|
||||||
local cmd=$1
|
local cmd=$1
|
||||||
case "$cmd" in
|
case "$cmd" in
|
||||||
|
init) # Create enter and exit scripts in current directory.
|
||||||
|
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
|
||||||
|
echo '.enter or .exit already exists'; return 1
|
||||||
|
fi
|
||||||
|
touch .enter .exit
|
||||||
|
autoenv edit ;;
|
||||||
|
edit) # Edit enter and exit scripts in current directory.
|
||||||
|
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
|
||||||
|
echo '.enter or .exit not found'; return 1
|
||||||
|
fi
|
||||||
|
if which vim &> /dev/null; then
|
||||||
|
vim -P .enter .exit
|
||||||
|
else
|
||||||
|
echo 'vim not found'; return 1
|
||||||
|
fi ;;
|
||||||
|
deinit) # Remove enter and exit scripts in current directory.
|
||||||
|
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
|
||||||
|
while true; do
|
||||||
|
read "answer?Are you sure [y/N]? "
|
||||||
|
case "$answer" in
|
||||||
|
y|Y|yes)
|
||||||
|
[ -f $PWD/.enter ] && rm $PWD/.enter
|
||||||
|
[ -f $PWD/.exit ] && rm $PWD/.exit ;;
|
||||||
|
*) break ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo '.enter and .exit not found'; return 1
|
||||||
|
fi ;;
|
||||||
-h|--help) # Display help.
|
-h|--help) # Display help.
|
||||||
echo "\
|
echo "\
|
||||||
usage: autoenv [-h] {init,edit,deinit}
|
usage: autoenv [-h] {init,edit,deinit}
|
||||||
@ -20,72 +47,17 @@ options:
|
|||||||
commands:
|
commands:
|
||||||
init add .enter and .exit scripts in current directory
|
init add .enter and .exit scripts in current directory
|
||||||
edit edit .enter and .exit scripts in current directory
|
edit edit .enter and .exit scripts in current directory
|
||||||
deinit remove .enter and .exit scripts in current directory"
|
deinit remove .enter and .exit scripts in current directory
|
||||||
;;
|
";;
|
||||||
|
*) # Invalid arguments.
|
||||||
init) # Create enter and exit scripts in current directory.
|
|
||||||
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
|
|
||||||
echo '.enter or .exit already exists'; return 1
|
|
||||||
fi
|
|
||||||
# Create then edit enter and exit scripts.
|
|
||||||
touch .enter .exit && autoenv edit
|
|
||||||
# Load the authorized file into a map of authorized key value pairs.
|
|
||||||
typeset -A authorized=(`cat ~/.cache/autoenv/authorized`)
|
|
||||||
# If enter script exists, authorize it.
|
|
||||||
[ -f $PWD/.enter ] && \
|
|
||||||
authorized[$PWD/.enter]=`zstat +mtime $PWD/.enter`
|
|
||||||
# If exit script exists, authorize it.
|
|
||||||
[ -f $PWD/.exit ] && \
|
|
||||||
authorized[$PWD/.exit]=`zstat +mtime $PWD/.exit`
|
|
||||||
# Store authorized map in authorized file.
|
|
||||||
echo ${(kv)authorized} > ~/.cache/autoenv/authorized
|
|
||||||
# Enter the new autoenv.
|
|
||||||
_autoenv_enter $PWD
|
|
||||||
;;
|
|
||||||
|
|
||||||
edit) # Edit enter and exit scripts in current directory.
|
|
||||||
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
|
|
||||||
echo '.enter or .exit not found'; return 1
|
|
||||||
fi
|
|
||||||
# If vim exists, edit enter and exit scripts.
|
|
||||||
if which vim &> /dev/null; then
|
|
||||||
vim -p .enter .exit
|
|
||||||
else
|
|
||||||
echo 'vim not found'; return 1
|
|
||||||
fi ;;
|
|
||||||
|
|
||||||
deinit) # Remove enter and exit scripts in current directory.
|
|
||||||
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
|
|
||||||
# Prompt user to confirm removal of enter and exit scripts.
|
|
||||||
while true; do
|
|
||||||
read "answer?Are you sure [y/N]? "
|
|
||||||
case "$answer" in
|
|
||||||
y|Y|yes)
|
|
||||||
# Remove enter and exit scripts.
|
|
||||||
[ -f $PWD/.enter ] && rm $PWD/.enter
|
|
||||||
[ -f $PWD/.exit ] && rm $PWD/.exit
|
|
||||||
;;
|
|
||||||
*) break ;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
else
|
|
||||||
echo '.enter and .exit not found'; return 1
|
|
||||||
fi ;;
|
|
||||||
|
|
||||||
*) # Invalid arguments, show help then error.
|
|
||||||
echo "invalid arguments: $@"
|
echo "invalid arguments: $@"
|
||||||
autoenv --help
|
autoenv --help
|
||||||
return 1 ;;
|
return 1 ;;
|
||||||
|
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
# Global entered directories array.
|
# Global entered directories array.
|
||||||
_autoenv_entered=()
|
_autoenv_entered=()
|
||||||
# If autoenv cache directory does not exist, create it.
|
|
||||||
! [ -d ~/.cache/autoenv ] && mkdir -p ~/.cache/autoenv
|
|
||||||
# If the authorized file does not exist, create it.
|
|
||||||
! [ -f ~/.cache/autoenv/authorized ] && touch ~/.cache/autoenv/authorized
|
|
||||||
|
|
||||||
# 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
|
||||||
@ -93,8 +65,14 @@ _autoenv_entered=()
|
|||||||
# persistent.
|
# persistent.
|
||||||
_autoenv_authorized() {
|
_autoenv_authorized() {
|
||||||
local file=$1
|
local file=$1
|
||||||
# Load the authorized file into a map of authorized key value pairs.
|
# If autoenv cache directory does not exist, create it.
|
||||||
typeset -A authorized=(`cat ~/.cache/autoenv/authorized`)
|
local cache_dir=~/.cache/autoenv
|
||||||
|
! [ -d $cache_dir ] && mkdir -p $cache_dir
|
||||||
|
local authorized_file=$cache_dir/authorized
|
||||||
|
# Define a map to hold authorized key value pairs.
|
||||||
|
typeset -A authorized=()
|
||||||
|
# If the authorized file exists, load it into the map.
|
||||||
|
[ -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, i.e. the modified time matches that
|
||||||
# held in the authorized file, return.
|
# held in the authorized file, return.
|
||||||
local modified_time=`zstat +mtime $file`
|
local modified_time=`zstat +mtime $file`
|
||||||
@ -109,7 +87,7 @@ _autoenv_authorized() {
|
|||||||
v|V|view) cat $file ;; # View the file.
|
v|V|view) cat $file ;; # View the file.
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
# Store authorized map in authorized file.
|
# Store authorized associative array in authorized file.
|
||||||
echo ${(kv)authorized} > $authorized_file
|
echo ${(kv)authorized} > $authorized_file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user