ozsh/libozsh.zsh

97 lines
2.0 KiB
Bash
Raw Normal View History

2021-02-21 04:37:27 +00:00
export OZSH_HOME="$HOME/.ozsh"
2021-03-08 10:04:11 +00:00
export OZSH_MODULES="$OZSH_HOME/modules"
export OZSH_LASTUPDATE_FILE="$OZSH_HOME/last_update"
2021-02-21 04:37:27 +00:00
2021-03-08 10:04:11 +00:00
#
# 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..."
2021-03-08 10:24:23 +00:00
git -C "$OZSH_MODULES" clone "$2" "$1"
2021-02-21 04:37:27 +00:00
fi
2021-03-08 10:04:11 +00:00
}
2021-02-21 04:37:27 +00:00
2021-03-08 10:04:11 +00:00
#
# load plugin
# $1 = plugin type
# $2 = plugin name
# $3 = plugin url <ignored for plugin_omz>
#
function ozsh_load {
2021-02-21 04:37:27 +00:00
if [ "$1" = 'plugin' ]; then
2021-03-08 10:04:11 +00:00
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"
2021-02-21 04:37:27 +00:00
elif [ "$1" = 'plugin_omz' ]; then
2021-03-08 10:04:11 +00:00
ozsh_clone_plugin ohmyzsh "https://github.com/ohmyzsh/ohmyzsh"
source "$OZSH_MODULES/ohmyzsh/plugins/$2/$2.plugin.zsh"
2021-02-21 04:37:27 +00:00
else
echo "Invalid ozsh_load arguments: $@"
fi
}
2021-03-08 11:21:16 +00:00
function ozsh_write_update_date {
echo $(date +%Y-%m-%d) > "$OZSH_LASTUPDATE_FILE"
}
2021-02-21 04:37:27 +00:00
function ozsh_update {
git -C $OZSH_HOME stash
2021-03-08 10:24:23 +00:00
git -C $OZSH_HOME pull --rebase
2021-02-21 04:37:27 +00:00
git -C $OZSH_HOME stash pop
2021-03-08 10:04:11 +00:00
# check for updates in modules
2021-03-08 10:20:10 +00:00
for d in $OZSH_MODULES/*/ ;
2021-03-08 10:04:11 +00:00
do
2021-03-08 10:24:23 +00:00
echo "Updating $d..."
git -C $d pull --rebase
2021-03-08 10:04:11 +00:00
done
2021-03-08 11:21:16 +00:00
ozsh_write_update_date
2021-03-08 10:04:11 +00:00
}
function ozsh_autoupdate {
2021-03-08 11:12:30 +00:00
local timediff=$(( ( $(strftime -r %Y-%m-%d $(date +%Y-%m-%d)) - $(strftime -r %Y-%m-%d $(cat $OZSH_LASTUPDATE_FILE)) ) / 86400 ))
2021-03-08 11:21:16 +00:00
2021-03-08 11:23:12 +00:00
echo "last cmd is $?"
2021-03-08 11:21:16 +00:00
if [ ! $? -eq 0 ]; then
# recreate the file if the command fails
ozsh_write_update_date
fi
2021-03-08 10:08:52 +00:00
if [ "$timediff" -ge "7" ]; then
2021-03-08 10:04:11 +00:00
vared -p 'Would you like to update ozsh? [y/N]: ' -c answer
2021-03-08 10:34:14 +00:00
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
2021-03-08 10:04:11 +00:00
ozsh_update
fi
fi
2021-02-21 04:37:27 +00:00
}
2021-03-08 10:04:11 +00:00
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
2021-03-08 11:21:16 +00:00
ozsh_write_update_date
2021-03-08 10:04:11 +00:00
fi
2021-03-08 11:12:30 +00:00
# load the datetime module
zmodload zsh/datetime
2021-03-08 10:04:11 +00:00
ozsh_autoupdate
}
# init
ozsh_init
# undefine functions
2021-03-08 11:18:33 +00:00
unset -f ozsh_init
unset -f ozsh_autoupdate