-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-bookmarks.sh
executable file
·145 lines (116 loc) · 4.48 KB
/
shell-bookmarks.sh
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
#!/bin/sh
################################
################################
###### ┌─┐┬ ┬┌─┐┬ ┬ #####
###### └─┐├─┤├┤ │ │ #####
###### └─┘┴ ┴└─┘┴─┘┴─┘ #####
###### ┌┐ ┌─┐┌─┐┬┌─ #####
###### ├┴┐│ ││ │├┴┐ #####
###### └─┘└─┘└─┘┴ ┴ #####
###### ┌┬┐┌─┐┬─┐┬┌─┌─┐ #####
###### │││├─┤├┬┘├┴┐└─┐ #####
###### ┴ ┴┴ ┴┴└─┴ ┴└─┘ #####
################################
##################emceelamb#####
#### BOOKMARK FILE LOCATION ####
bookmarkfile="$HOME/.shellbookmarks"
#### BASH SIMPLE CURSES ####
curseswindow="$(dirname $(readlink -f '$0'))/bash-curses/bookmark_window.sh"
### ANSI COLOR CODES ###
RED="1;31m"
GREEN="1;32m"
NORMAL="0m"
if [ ! -e "$bookmarkfile" ]
then
touch $bookmarkfile
echo -e "Bookmark location: \e[${RED}$bookmarkfile\e[${NORMAL}"
fi
function bmk() {
# readarray requires GNU Bash 4.0 <=
# readarray -t saved_bookmarks < $bookmarkfile
saved_bookmarks=()
while IFS= read -r line; do
saved_bookmarks+=("$line")
done < $bookmarkfile
##### REMOVE ALL BOOK MARKS #####
if [[ $1 == 'clear' ]]; then
mv $bookmarkfile ${bookmarkfile}.bak # Backup bookmarks
touch $bookmarkfile
echo "Cleared all bookmarks."
######## LIST BOOKMARKS #######
##### CHECK IF NO BOOKMARK EXISTS #####
elif [[ -z "$1" ]] && [[ $(cat $bookmarkfile | wc -l) -eq 0 ]] || [[ $1 == 'l' ]] && [[ $(cat $bookmarkfile | wc -l) -eq 0 ]]; then
echo -e "\e[${RED}No bookmarks found!\e[${NORMAL}"
##### IF BOOKMARK EXISTS #####
elif [[ -z "$1" ]] || [[ $1 == 'l' ]]; then
$curseswindow
### SIMPLE BOOKMARKS ###
# counter=0
# for i in "${saved_bookmarks[@]}"
# do
# bookmark=($i)
# printf '[%d] /%s\n' "$counter" "${bookmark[1]}"
# (( counter++))
# done
######## GOTO BOOKMARK #######
##### CHECK IF NO BOOKMARK EXISTS #####
elif [[ $1 == 'g' && $(cat $bookmarkfile | wc -l) -eq 0 ]]; then
echo -e "\e[${RED}Bookmark not found!\e[${NORMAL}"
##### IF BOOKMARK EXISTS #####
elif [[ $1 == 'g' && $(cat $bookmarkfile | wc -l) > 0 ]]; then
tput clear
bookmark=( ${saved_bookmarks[$2]} )
cd ${bookmark[0]}
echo -e "\e[${GREEN}Go to ${bookmark[1]}.\e[${NORMAL}"
######## SAVE BOOKMARK #######
elif [[ $1 == 's' ]]; then
current_dir=$(pwd)
basename=$(basename $current_dir)
echo $current_dir $basename >> $bookmarkfile
echo -e "Saved \e[${RED}$basename\e[${NORMAL} to bookmarks."
####### DELETE BOOKMARK ######
##### CHECK IF NO BOOKMARK EXISTS #####
elif [[ $1 == 'd' && $(cat $bookmarkfile | wc -l) -eq 0 ]]; then
echo -e "\e[${RED}Bookmark not found!\e[${NORMAL}"
##### IF BOOKMARK EXISTS #####
elif [[ $1 == 'd' ]]; then
bookmark=( ${saved_bookmarks[$2]} )
toDelete=$(($2 + 1))
sed -i "${toDelete}d" $bookmarkfile
echo -e "Deleted \e[${RED}${bookmark[1]}\e[${NORMAL} from bookmarks."
##############################
######## EDGE CASES #######
##############################
####### IF INVALID ARG #######
elif [[ $1 != [0-9] ]]; then
echo -e "\e[${RED}Bookmark command not recognized!\e[${NORMAL}"
echo -e "View saved bookmarks with 'bmk l'"
echo -e "Save location to bookmark with 'bmk s'"
echo -e "Delete saved bookmark with 'bmk d <bookmark>'"
echo -e "Goto saved bookmark with 'bmk g <bookmark>'"
echo -e "Clear all saved bookmark with 'bmk clear'"
####################################################
###### IF 'G' LEFT OUT BUT BOOKMARK EXISTS #######
################ GO TO BOOKMARK ##################
####################################################
##### CHECK IF NO BOOKMARK EXISTS #####
elif [[ $1 == '0' && $(cat $bookmarkfile | wc -l) -eq 0 ]]; then
echo -e "\e[${RED}Bookmark not found!\e[${NORMAL}"
##### OTHERWISE GOTO BOOKMARK #####
elif [[ $1 -ge 0 ]] && [[ $1 -le ${#saved_bookmarks[@]} ]]; then
tput clear
bookmark=( ${saved_bookmarks[$1]} )
cd ${bookmark[0]}
echo -e "\e[${GREEN}Go to ${bookmark[1]}.\e[${NORMAL}"
elif [[ $1 -gt ${#saved_bookmarks[@]} ]]; then
echo -e "\e[${RED}Bookmark not found!\e[${NORMAL}"
else:
echo -e "\e[${RED}Bookmark not found!\e[${NORMAL}"
fi
# readarray requires GNU Bash 4.0 <=
# readarray -t saved_bookmarks < $bookmarkfile
saved_bookmarks=()
while IFS= read -r line; do
saved_bookmarks+=("$line")
done < $bookmarkfile
}