-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwestchange.zsh
62 lines (56 loc) · 1.63 KB
/
westchange.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
WC_DIRECTORIES=${0:A:h}/directories
# TODO: should auto install fzf
# check if the user has the fzf utility
if [ ! -f $WC_DIRECTORIES ]; then
touch $WC_DIRECTORIES
fi
# switch between the defined directories.
function switch_directory() {
DIRS=$(cat $WC_DIRECTORIES)
DIR=""; DIR=$(echo $DIRS | cut -d'=' -f1 | fzf)
if [ "$DIR" = "" ]; then
echo "No directory selected. Did not switch."
return
fi
FULLPATH=$(echo $DIRS | grep $DIR | head -n 1 | cut -d'=' -f2)
if [ -d $FULLPATH ]; then
cd "$FULLPATH"
else
echo "'${FULLPATH}' doesn't exist."
fi
}
# add the current directory to the working directories list
function add_directory() {
# TODO: check if the name picked doesn't actually exist.
read "DIR_NAME?What should i call this directory? "
if [ -z "$DIR_NAME" ]; then
echo "ERROR: enter a valid directory name"
return
fi
DIR=$(pwd)
FULLPATH="${DIR_NAME}=${DIR}"
echo $FULLPATH >> $WC_DIRECTORIES
}
# remove current directory from the working directory list
function remove_directory() {
zle -I
if [ -z $WC_DIRECTORIES ]; then
# exit if the are no working directories
clear
echo "No working directories"
return
fi
DIR=$(cat $WC_DIRECTORIES | cut -d'=' -f1 | fzf)
FULLPATH=$(cat $WC_DIRECTORIES | grep $DIR)
PATTERN_TO_REMOVE="/$(echo $FULLPATH | sed 's/\//\\\//g')/d"
while true; do
read "yn?Remove '$FULLPATH' [y/n]: "
case $yn in
[Yy]*) echo "Deleted" ; break;;
[Nn]*) echo "Aborted" ; return 1;;
esac
done
sed -i $PATTERN_TO_REMOVE $WC_DIRECTORIES
}
# load the hotkeys to quickly change directory
source ${0:A:h}/hotkeys.sh