-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-mgr.zsh
154 lines (119 loc) · 4.54 KB
/
zsh-mgr.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/zsh
if [ "$ZSH_MGR_ZSH" != yes ]; then
ZSH_MGR_ZSH=yes
else
return 0
fi
export REPO_URL="https://github.com/"
export PRIVATE_REPO_URL="[email protected]:"
PLUGIN_LIST=() # Empty array for plugins
source "$ZSH_CONFIG_DIR/zsh-mgr/zsh-common-variables.zsh"
source "$ZSH_CONFIG_DIR/zsh-mgr/generic-auto-updater.zsh"
source "$ZSH_CONFIG_DIR/zsh-mgr/zsh-mgr-common-functions.zsh"
# Adds a plugin and updates periodically.
# $1: user/plugin. If it is a private repo, input the whole URL
# $2 (optional): extra git params (like --depth)
add_plugin() {
local -r PARAMS="${2:-}"
local -r URL="$REPO_URL"
_generic_add_plugin "$1" "$URL" "$PARAMS"
}
# Adds a plugin from a private repo and updates it periodically.
# $1: user/plugin. If it is a private repo, input the whole URL
# $2 (optional): extra git params (like --depth)
add_plugin_private(){
local -r PARAMS="${2:-}"
local -r URL="$PRIVATE_REPO_URL"
_generic_add_plugin "$1" "$URL" "$PARAMS"
}
# Updates a plugin given as input.
# $1: The name of the plugin. It must be set before calling the function.
# pre: The plugin must be installed or else the function will error out.
_update_plugin(){
local -r PLUGIN_NAME="$1"
_generic_updater "$PLUGIN_NAME" "$ZSH_PLUGIN_DIR/$PLUGIN_NAME"
}
#\\033\[0;?[0-9]*m to find ansi escape codes
# Updates all loaded plugins
update_plugins(){
if [ "${#PLUGIN_LIST[@]}" -ne "0" ]; then
local i
for i in "${PLUGIN_LIST[@]}"
do
_update_plugin "$i"
done
unset i
else
echo -e "${RED}No plugins loaded/installed${NO_COLOR}"
fi
}
# date -d @1679524012 "+%d-%m-%Y %H:%M:%S"
# $1 (Optional yes/no): Output legend? Default: yes
check_plugins_update_date() {
if [ "${#PLUGIN_LIST[@]}" -ne "0" ]; then
local RAW_TABLE=("${(@f)$(_plugin_update_to_table no)}")
local COLORED_TABLE=("${(@f)$(_plugin_update_to_table yes)}")
RAW_TABLE=("Plugin name#Next update" "${RAW_TABLE[@]}")
COLORED_TABLE=("Plugin name#Next update" "${COLORED_TABLE[@]}")
RAW_TABLE=$(printf "%s\n" "${RAW_TABLE[@]}")
COLORED_TABLE=$(printf "%s\n" "${COLORED_TABLE[@]}")
_create_table "$RAW_TABLE" "#" "${CYAN}" "$COLORED_TABLE"
[ "${1:-yes}" = "yes" ] && _display_color_legend
else
echo -e "${RED}No plugins loaded/installed${NO_COLOR}"
fi
}
# Displays a colored table with the next update date for the plugin manager.
# $1 (Optional yes/no): Display legend? Default: yes
check_mgr_update_date(){
# local raw_msg="zsh-mgr#$(date -d @"$(( $(cat "$ZSH_PLUGIN_DIR"/.zsh-mgr) + MGR_TIME_THRESHOLD ))" "+%d-%m-%Y %H:%M:%S" )"
# local colored_msg=$(_color_row_on_date "zsh-mgr#$(cat "$ZSH_PLUGIN_DIR"/.zsh-mgr)" "#" "$MGR_TIME_THRESHOLD")
# _create_table "Manager#Next update\n$raw_msg" "#" "${GREEN}" "Manager#Next update\n$colored_msg"
# [ "${1:-yes}" = "yes" ] && _display_color_legend
# return 0
_check_comp_update_date "zsh-mgr" "$ZSH_CONFIG_DIR/zsh-mgr" "$MGR_TIME_THRESHOLD" "Manager#Next update" "${GREEN}" "${1:-yes}"
}
# Displays a colored table with the next update date for the plugins and the plugin manager itself.
# $1 (Optional yes/no): Display legend? Default: yes
ck_mgr_plugin(){
check_mgr_update_date no
check_plugins_update_date no
[ "${1:-yes}" = "yes" ] && _display_color_legend
}
# Makes a table of every plugin and its update date.
# $1 (yes/no): Color the output?
_plugin_update_to_table(){
if [ "${#PLUGIN_LIST[@]}" -ne "0" ]; then
# Unique to zsh
local next_date
local i
for i in "${PLUGIN_LIST[@]}"
do
if [ "$1" = yes ];
then
_color_row_on_date "$i#$(cat "$ZSH_PLUGIN_DIR"/."$i")" "#" "$TIME_THRESHOLD"
else
next_date=$(( $(cat "$ZSH_PLUGIN_DIR"/."$i") + TIME_THRESHOLD ))
next_date=$(date -d @"$next_date" "+%d-%m-%Y %H:%M:%S")
echo "$i#$next_date"
fi
done
unset i
fi
}
# Manually updates the plugin manager
update_mgr(){
_generic_updater "zsh-mgr" "$ZSH_CONFIG_DIR/zsh-mgr"
}
# Auto-updater for the plugin manager
_auto_update_mgr(){
_generic_auto_updater "zsh-mgr" "$ZSH_CONFIG_DIR/zsh-mgr" "$MGR_TIME_THRESHOLD"
}
# Recreates plugin directory if it does not exist anymore
_check_plugin_dir_exists(){
[ ! -d "$ZSH_PLUGIN_DIR" ] && mkdir "$ZSH_PLUGIN_DIR"
}
# Checks if the plugin directory exists
_check_plugin_dir_exists
# Calls the auto-updater for the plugin manager
_auto_update_mgr