-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeth.zsh
173 lines (151 loc) · 4.7 KB
/
peth.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# local script_dir=${0:a:h}
PATH_ETHIC_CONFIG="$HOME/.path-ethic"
PATH_ETHIC_DEFAULT_PRESET_NAME="default"
PATH_ETHIC_DEFAULT_PRESET_PATH="$PATH_ETHIC_CONFIG/$PATH_ETHIC_DEFAULT_PRESET_NAME.preset"
PATH_ETHIC_CURRENT_PRESET_NAME="$PATH_ETHIC_DEFAULT_PRESET_NAME"
PATH_ETHIC_DEFAULT_PATH="" # see load_path_ethic
source "$PATH_ETHIC_HOME/lib.zsh"
source "$PATH_ETHIC_HOME/peth-edit.zsh"
source "$PATH_ETHIC_HOME/peth-presets.zsh"
# Prints help message
function __pe_help() {
__pe_log "Usage:
peth [command]
peth command [arguments]
Available Command:
show - shows the effective PATH and your current session settings
list - lists all effective PATH elements in the current session
push <path> - pushs an element to the begining of the current session PATH
append <path> - appends an element to the end of the current session PATH
rm <path> - finds and removes an element from the session PATH
flip - flips the order of your set prefix and suffix in the current
session
reset - strips any set prefix and suffix from the current session PATH
save [name] - saves the current session settings to disk for later recall
If the optional name argument is provided settings are saved
as a preset under that name
load [name] - loads previously saved settings into the current session
If a name argument is provided attempts to load a saved preset
rmp [name] - removes a previously saved preset
listp - lists all saved presets
update - updates the plugin from github
help - displays this help message
path-ethic on Github: https://github.com/sha1n/path-ethic
Oh-My-Zsh on Github: https://github.com/ohmyzsh/ohmyzsh
"
}
# Attempts to run self update
function __pe_self_update() {
if [[ ! -d "$PATH_ETHIC_HOME/.git" ]] && [[ ! -f "$PATH_ETHIC_HOME/.git" ]]; then
__pe_log_error "The plugin directory is not a git clone"
__pe_log_error "Update failed!"
return
fi
__pe_log "\nPulling latest changes from remote repository..."
if git -C "$PATH_ETHIC_HOME" pull origin master; then
__pe_log "Update successful!"
else
__pe_log_error "Update failed!"
fi
}
# Main command interpreter and dispatcher function
function peth() {
if [[ "$#" == "0" ]]; then
__pe_show
return
fi
while [[ $# -gt 0 ]]; do
command="$1"
case $command in
push)
__pe_add_path_element "push" "${@:2}"
return
;;
append)
__pe_add_path_element "append" "${@:2}"
return
;;
rm)
__pe_remove "${@:2}"
return
;;
reset)
__pe_reset
return
;;
save | commit)
__pe_save "${@:2}"
return
;;
load | reload)
__pe_load "${@:2}"
return
;;
rmp)
__pe_remove_preset "${@:2}"
return
;;
listp)
__pe_list_presets
return
;;
show)
__pe_show
return
;;
list)
__pe_list
return
;;
flip)
__pe_flip
return
;;
update)
__pe_self_update
return
;;
help)
__pe_help
return
;;
*) # ignore unknown
__pe_log_error "unsupported command: '$@'"
__pe_help
return
;;
esac
done
}
# Loads user commited path prefix/suffix and re-exports the shell PATH.
function load_path_ethic() {
PATH_ETHIC_DEFAULT_PATH="$PATH"
# remove the hook - it is only needed to run once per session
add-zsh-hook -d precmd load_path_ethic
# migrate previous version persistent data to latest
if [[ -f "$HOME/.path-ethic" ]]; then
local new_path=$(__pe_strip_original_path)
source "$HOME/.path-ethic"
export PATH="$(__pe_rebuild_path_with $new_path)"
rm "$HOME/.path-ethic"
mkdir -p "$PATH_ETHIC_CONFIG"
__pe_save
fi
mkdir -p "$PATH_ETHIC_CONFIG"
if [[ -f "$PATH_ETHIC_DEFAULT_PRESET_PATH" ]]; then
__pe_load $PATH_ETHIC_DEFAULT_PRESET_NAME
fi
# this covers .pethrc in ~
__pe_load_pethrc
}
function __pe_load_pethrc() {
local tmp=
if [[ -f "./.pethrc" ]]; then
while read line; do
tmp=$(echo -e "${line}" | tr -d '[:space:]')
if [[ "$line" != "" ]]; then
peth load "$line"
fi
done <"./.pethrc"
fi
}