ozsh/libozsh.zsh
2021-03-08 06:18:33 -05:00

85 lines
1.9 KiB
Bash

export OZSH_HOME="$HOME/.ozsh"
export OZSH_MODULES="$OZSH_HOME/modules"
export OZSH_LASTUPDATE_FILE="$OZSH_HOME/last_update"
#
# clone the plugin if not exist
# $1 = plugin name
# $2 = git URL
#
function ozsh_clone_plugin {
if [ ! -d "$OZSH_MODULES/$1" ]; then
echo "Installing plugin $1..."
git -C "$OZSH_MODULES" clone "$2" "$1"
fi
}
#
# load plugin
# $1 = plugin type
# $2 = plugin name
# $3 = plugin url <ignored for plugin_omz>
#
function ozsh_load {
if [ "$1" = 'plugin' ]; then
ozsh_clone_plugin "$2" "$3"
source "$OZSH_MODULES/$2/$2.plugin.zsh"
elif [ "$1" = 'theme' ]; then
ozsh_clone_plugin "$2" "$3"
source "$OZSH_MODULES/$2/$2.zsh-theme"
elif [ "$1" = 'plugin_omz' ]; then
ozsh_clone_plugin ohmyzsh "https://github.com/ohmyzsh/ohmyzsh"
source "$OZSH_MODULES/ohmyzsh/plugins/$2/$2.plugin.zsh"
else
echo "Invalid ozsh_load arguments: $@"
fi
}
function ozsh_update {
git -C $OZSH_HOME stash
git -C $OZSH_HOME pull --rebase
git -C $OZSH_HOME stash pop
# check for updates in modules
for d in $OZSH_MODULES/*/ ;
do
echo "Updating $d..."
git -C $d pull --rebase
done
echo $(date +%Y-%m-%d) > "$OZSH_LASTUPDATE_FILE"
}
function ozsh_autoupdate {
local timediff=$(( ( $(strftime -r %Y-%m-%d $(date +%Y-%m-%d)) - $(strftime -r %Y-%m-%d $(cat $OZSH_LASTUPDATE_FILE)) ) / 86400 ))
if [ "$timediff" -ge "7" ]; then
vared -p 'Would you like to update ozsh? [y/N]: ' -c answer
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
ozsh_update
fi
fi
}
function ozsh_init {
# check modules directory
if [ ! -d $OZSH_MODULES ]; then
mkdir -p $OZSH_MODULES
fi
# create lastupdate file if not exist
if [ ! -f $OZSH_LASTUPDATE_FILE ]; then
echo $(date +%Y-%m-%d) > $OZSH_LASTUPDATE_FILE
fi
# load the datetime module
zmodload zsh/datetime
ozsh_autoupdate
}
# init
ozsh_init
# undefine functions
unset -f ozsh_init
unset -f ozsh_autoupdate