Mercurial > dotfiles
annotate .shell.d/00.path_manipulation.sh @ 526:852565046ed0 default tip
zsh: fidget with screen/tmux message
This should speed things up very slightly by avoiding some `grep` action
in the common case of no detached screens/tmuxes.
author | Augie Fackler <raf@durin42.com> |
---|---|
date | Mon, 14 Nov 2022 11:02:35 -0500 |
parents | b0fa9e7cadac |
children |
rev | line source |
---|---|
0 | 1 # Functions for manipulating $PATH. Split out so I can use them in .zshenv if I want. |
2 | |
3 function insert_path_element() { | |
135
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
4 insert_element_into_var $1 PATH |
0 | 5 } |
6 | |
7 function remove_path_element() { | |
149
7dfb7799567f
path_manipulation: Fix accidental recursion.
Augie Fackler <durin42@gmail.com>
parents:
135
diff
changeset
|
8 remove_element_from_var $1 PATH |
135
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
9 } |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
10 |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
11 function insert_element_into_var () { |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
12 eval varcontents=\$"$2" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
13 eval varname="$2" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
14 newcomp="$1" |
525
b0fa9e7cadac
shell: speed things up by golfing out grep
Augie Fackler <raf@durin42.com>
parents:
490
diff
changeset
|
15 if [[ "$varcontents" =~ "${newcomp}:" ]] ; then |
135
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
16 true |
525
b0fa9e7cadac
shell: speed things up by golfing out grep
Augie Fackler <raf@durin42.com>
parents:
490
diff
changeset
|
17 elif [[ -z "$varcontents" ]] ; then |
135
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
18 export $varname="$newcomp" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
19 else |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
20 export $varname="$newcomp:$varcontents" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
21 fi |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
22 } |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
23 |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
24 function remove_element_from_var() { |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
25 eval varcontents=\$"$2" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
26 eval varname="$2" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
27 newcomp="$1" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
28 if echo "$varcontents" | grep "$newcomp:" >> /dev/null; then |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
29 export $varname="`echo $varcontents | sed s%$newcomp:%%`" |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
30 elif [[ "$varcontents" == "$newcomp" ]] ; then |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
31 unset $varname |
de6d0a9a7e3f
path_manipulation: more flexible so it can be used on non-PATH items
Augie Fackler <durin42@gmail.com>
parents:
39
diff
changeset
|
32 fi |
0 | 33 } |
484
c85072e620a8
append_path_element: new function that I've had for a while
Augie Fackler <raf@durin42.com>
parents:
149
diff
changeset
|
34 |
c85072e620a8
append_path_element: new function that I've had for a while
Augie Fackler <raf@durin42.com>
parents:
149
diff
changeset
|
35 function append_path_element() { |
c85072e620a8
append_path_element: new function that I've had for a while
Augie Fackler <raf@durin42.com>
parents:
149
diff
changeset
|
36 export PATH="${PATH}:$1" |
c85072e620a8
append_path_element: new function that I've had for a while
Augie Fackler <raf@durin42.com>
parents:
149
diff
changeset
|
37 } |