-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeth-edit.zsh
125 lines (98 loc) · 2.97 KB
/
peth-edit.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
source "$PATH_ETHIC_HOME/lib.zsh"
# Removes any set prefix and suffix and re-exports PATH
function __pe_reset() {
local stripped_path=$(__pe_strip_original_path)
PATH_ETHIC_HEAD=
PATH_ETHIC_TAIL=
__pe_reexport_path "$stripped_path"
}
# Prepends the specified path element re-exports PATH
function __pe_push() {
local original_path=$(__pe_strip_original_path)
PATH_ETHIC_HEAD=$(__pe_normalize_path "$1:$PATH_ETHIC_HEAD")
__pe_reexport_path "$original_path"
}
# Appends the specified path element re-exports PATH
function __pe_append() {
local original_path=$(__pe_strip_original_path)
PATH_ETHIC_TAIL=$(__pe_normalize_path "$PATH_ETHIC_TAIL:$1")
__pe_reexport_path "$original_path"
}
function __pe_add_path_element() {
if [[ "$2" == "" ]]; then
__pe_log_error "please provide a path to append to PATH"
__pe_help
return
fi
if __pe_is_directory $2 ; then
if [[ "$1" == "push" ]]; then
__pe_push "$2"
elif [[ "$1" == "append" ]]; then
__pe_append "$2"
else
__pe_log_error "unsupported add command '$1'"
fi
else
__pe_log_error "path '$2' doesn't exist"
fi
}
function __pe_filter() {
local elements=("${(s/:/)1}")
local match="$2"
local out=
for e in "${elements[@]}"
do
if [[ "$e" != "$match" ]]; then
out="$out:$e"
fi
done
echo $(__pe_normalize_path "$out")
}
# Searches for the specified element in the current PATH and removes it if found
function __pe_remove() {
if [[ "$1" == "" ]]; then
__pe_log_error "please provide a path to remove from PATH"
__pe_help
return
fi
local find="$1"
local target_path=$(__pe_strip_original_path)
target_path=$(__pe_filter "$target_path" "$find")
PATH_ETHIC_HEAD=$(__pe_filter "$PATH_ETHIC_HEAD" "$find")
PATH_ETHIC_TAIL=$(__pe_filter "$PATH_ETHIC_TAIL" "$find")
__pe_reexport_path "$target_path"
}
# Shows the current path elements
function __pe_show() {
echo "effective ➤ $fg[magenta]$PATH$reset_color
prefix ➤ $fg[green]$PATH_ETHIC_HEAD$reset_color
suffix ➤ $fg[blue]$PATH_ETHIC_TAIL$reset_color"
}
# Lists all path elements
function __pe_list() {
if [[ "$PATH_ETHIC_HEAD" != "" ]]; then
for e in "${"${(s/:/)PATH_ETHIC_HEAD}"[@]}"
do
echo "$fg[green]$e$reset_color"
done
fi
local orig=$(__pe_strip_original_path)
for e in "${"${(s/:/)orig}"[@]}"
do
echo "$e"
done
if [[ "$PATH_ETHIC_TAIL" != "" ]]; then
for e in "${"${(s/:/)PATH_ETHIC_TAIL}"[@]}"
do
echo "$fg[blue]$e$reset_color"
done
fi
}
# Flips the prefix and suffix to change elements priority
function __pe_flip() {
PATH=$(__pe_strip_original_path)
local tmp=$PATH_ETHIC_HEAD
PATH_ETHIC_HEAD=$PATH_ETHIC_TAIL
PATH_ETHIC_TAIL=$tmp
__pe_reexport_path
}