Actually add sandbox plugin

This commit is contained in:
Kenneth Benzie 2018-08-20 10:09:55 +01:00
parent b61ebc4f7a
commit e13e70de6b
3 changed files with 156 additions and 4 deletions

View File

@ -6,8 +6,7 @@
- brew:
- zsh
- command:
-
install: sudo chsh $USER -s `which zsh`
- install: sudo chsh $USER -s `which zsh`
remove: sudo chsh $USER -s `which bash`
- symlink:
- {src: zlogin, dst: ~/.zlogin}
@ -15,9 +14,10 @@
- {src: zprofile, dst: ~/.zprofile}
- {src: zshenv, dst: ~/.zshenv}
- {src: zshrc, dst: ~/.zshrc}
-
src: prompt_fresh_setup
- src: prompt_fresh_setup
dst: ~/.local/share/zsh/site-functions/prompt_fresh_setup
- src: sandbox/_sandbox
dst: ~/.local/share/zsh/site-functions/_sandbox
- repo:
- https://github.com/zsh-users/zsh-autosuggestions.git
- https://github.com/zsh-users/zsh-history-substring-search.git

49
sandbox/_sandbox Normal file
View File

@ -0,0 +1,49 @@
#compdef sandbox
__get_sandboxes() {
/bin/ls $SANDBOX_ROOT 2> /dev/null
}
__sandboxes() {
local -a sandboxes
sandboxes=(${(fo)"$(__get_sandboxes)"})
_describe 'in' sandboxes
}
_sandbox_cmds() {
local commands; commands=(
'create:Create a new sandbox'
'rename:Rename an existing sandbox'
'destroy:Destroy an existing sandbox'
'list:Show all existing sandboxes'
'enable:Enable an existing sandbox'
'disable:Disable the current sandbox'
)
_describe -t commands 'sandbox command' commands "$@"
}
_sandbox() {
local context curcontext="$curcontext" state line
typeset -A opt_args
_arguments -C \
'1: :_sandbox_cmds' \
'*::arg:->args'
case $state in
(args)
curcontext="${curcontext%:*:*}:sandbox-cmd-$words[1]:"
case $line[1] in
(create|list|disable)
;;
(enable|destroy)
_arguments -C '1:: :__sandboxes'
;;
(rename)
_arguments -C '1:: :__sandboxes'
;;
esac
esac
}
_sandbox "$@"

103
sandbox/sandbox.plugin.zsh Normal file
View File

@ -0,0 +1,103 @@
if [[ "" == $SANDBOX_ENV_IN_FILE ]]; then
export SANDBOX_ENV_IN_FILE=$AUTOENV_IN_FILE
fi
if [[ "" == $SANDBOX_ENV_OUT_FILE ]]; then
export SANDBOX_ENV_OUT_FILE=$AUTOENV_OUT_FILE
fi
if [[ "" == $SANDBOX_ROOT ]]; then
export SANDBOX_ROOT=$HOME/Sandbox
fi
sandbox() {
local usage="usage: sandbox {create,destroy,enable,disable} [name]"
if [[ "" == $1 ]]; then
echo $usage
return 1
fi
case $1 in
create)
if [[ "" == $2 ]]; then
echo $usage
return 1
fi
local sandbox=$SANDBOX_ROOT/$2
if [[ -d $sandbox ]]; then
echo "Sandbox '$2' already exists"
return 2
fi
mkdir -p $sandbox &> /dev/null
begin=$PWD
cd $sandbox
echo "SANDBOX_HOME=\$(dirname -- "\$0:a")" >> $SANDBOX_ENV_IN_FILE
echo "SANDBOX_NAME=$2" >> $SANDBOX_ENV_IN_FILE
echo "unset SANDBOX_NAME" >> $SANDBOX_ENV_OUT_FILE
echo "unset SANDBOX_HOME" >> $SANDBOX_ENV_OUT_FILE
git init &> /dev/null
cd $begin
cd $sandbox
;;
rename)
if [[ "" == $2 || "" == $3 ]]; then
echo $usage
return 1
fi
mv $SANDBOX_ROOT/$2 $SANDBOX_ROOT/$3
sed -i "" "s/$2/$3/g" $SANDBOX_ROOT/$3/.env
;;
destroy)
if [[ "" == $2 ]]; then
echo $usage
return 1
fi
local sandbox=$SANDBOX_ROOT/$2
if [[ ! -d $sandbox ]]; then
echo "Sandbox '$2' does not exist"
return 2
fi
cd -
if [[ "${SANDBOX_ROOT##$PWD}" = "${SANDBOX_ROOT}" ]]; then
cd $HOME
fi
rm -rf $sandbox
;;
list)
/bin/ls -1 $SANDBOX_ROOT
;;
enable)
if [[ "" == $2 ]]; then
echo $usage
return 1
fi
local sandbox=$SANDBOX_ROOT/$2
if [[ ! -d $sandbox ]]; then
echo "Sandbox '$2' does not exist"
return 2
fi
export SANDBOX_RETURN=$PWD
cd $sandbox
;;
disable)
if [[ -z $SANDBOX_RETURN ]]; then
echo "Sandbox is not currently active"
return 2
fi
cd $SANDBOX_RETURN
unset $SANDBOX_RETURN
;;
esac
}