summaryrefslogtreecommitdiff
path: root/zscripts/zplug.zsh
blob: 27dc70f4259bf937dae4d188300e0c7b0f4ce83d (plain)
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
#!/usr/bin/env zsh

local home=~
local zplugs="${ZDOTDIR:-$home/.}${ZDOTDIR:+/}zplugs" # Declare all variables using `local` to keep them away from the interactive shell
local zplugins="${ZPLUGINSDIR:-$home/.local/share/zsh/zplugins}"
#[[ $EUID != 0 ]] && [ -z "$ZPLUGNOROOT" ] && local sudo=${SUDO-sudo}

function __zplugInstall() {
	touch "$zplugs"
	local pluglist=( `\grep -v '#' "$zplugs"` )
	local confirm plug
	shift
	for plug in "$@"; do; pluglist+=(${plug}); done

	$sudo mkdir -p "$zplugins"

	for plug in ${pluglist[@]}; do
		[ -d "$zplugins/$(basename -s'.git' "$plug")" ] && continue
		if ! `$sudo git -C "$zplugins" clone "$plug" "$(basename -s'.git' "$plug")"`; then
			# Runs if git has an error
			printf "Remove \"$plug\"? [y|n]: "
			read confirm
			case $confirm in
				y|yes) pluglist=(${pluglist#$plug}) && echo "Repo Deleted" ;;
				*) echo "Repo Kept" ;;
			esac
		fi
		echo
	done
	for plug in ${pluglist[@]}; do echo "$plug"; done | sort -u > "$zplugs"
	echo "All plugins installed and saved to $zplugs!\nDone"
}

function __zplugUninstall() {
	local pluglist=( `cat "$zplugs"` ) pluglistbn=()
	local confirm i plug plugids plugig

	[ -z "$pluglist" ] && echo "No packages are installed!" && return

	$sudo mkdir -p "$zplugins"
	echo "Listing all packages in $zplugins ..."
	for i in {1..${#pluglist[@]}}; do
		pluglistbn[$i]="$(basename -s'.git' "${pluglist[$i]}")"
		printf '%3d) %s\n' $i "${pluglistbn[$i]}"
	done

	echo
	printf "Choose zsh plugin(s) to remove: "
	read plugids
	[ -z "$plugids" ] && echo "No plugins selected\nAborting" && return

	echo "\nListing selected plugins:"
	for plugid in $=plugids; do
		if [ -z ${pluglist[$plugid]} ]; then
			echo "Error: input \"$plugid\" is not a valid plugin identifier"
			return
		fi
		echo "\t${pluglistbn[$plugid]}"
	done

	printf "\nReally remove all of these plugins? [y|n]: "
	read confirm
	case $confirm in
		y|yes) ;;
		n|no) echo "Aborted" && return ;;
		*) echo "Input not understood\nAborted" && return ;;
	esac
	for plugid in $=plugids; do
		printf '\t'
		$sudo rm -vrf "$zplugins/${pluglistbn[$plugid]}"
		pluglist[$plugid]=""
	done
	for plug in ${pluglist[@]}; do echo "$plug"; done | sort -u > "$zplugs"
	echo "\nDone"
}

function __zplugUpgrade() {
	local plugdir
	$sudo mkdir -p "$zplugins"
	echo "Upgrading all packages...\n"
	for plugdir in "$zplugins"/*; do;
		basename "$plugdir"
		$sudo git -C "$plugdir" pull;
		echo
	done
	echo "Done"
}

function __zplugList() {
	mkdir -p "$zplugins"
	echo "Listing all packages...\n"
	ls "$zplugins" | nl -s') ' -w3
	echo "\nDone"
}

function __zplugHelp() {
	echo "Usage: zplug [install/add/i|uninstall/remove/r|upgrade/update/u|list/l|help/h]
	install, add, i [packages]  : Install plugins from config file & add any new plugins
	uninstall, remove, r        : Interactively remove plugins
	upgrade, update, u          : Update all installed plugins
	list, l                     : List all installed plugins
	help, h                     : Show this help menu

	All plugins are listed in $zplugs
	All plugins are installed in $zplugins

	To install a plugin, just put it in the zplug file and run \"zplug install\"
	or just run \"zplug install [plugin]\" where [plugin] is a link to download the
	plugin using git

	For instance, \"https://github.com/zdharma-continuum/fast-syntax-highlighting\" is
	a valid plugin link for the fast-syntax-highlighting plugin.

	To use your plugins, just put \"zplugInitialize\" as the last line of your zshrc!"
}

function zplug() {
	local cmd=${1:-h}
	case $cmd in
		install|add|i) __zplugInstall $@ ;;
		uninstall|remove|r) __zplugUninstall ;;
		upgrade|update|u) __zplugUpgrade ;;
		list|l) __zplugList ;;
		help|h) __zplugHelp ;;
		*) echo "zplug: input \"$1\" not recognized!" && __zplugHelp ;;
	esac
}

function zplugInitialize() {
	local plugin
	if ! [ -d "$zplugins" ] && [ -f "$zplugs" ]; then
		echo "zplug installing plugins..."
		__zplugInstall 0  # Put in some value since there is a shift
	fi
	for plugin in "$zplugins"/*/*.zsh; do source "$plugin"; done 2>/dev/null
}