-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoto.sh
executable file
·262 lines (222 loc) · 6.74 KB
/
goto.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
# Copyright (c) 2018 Pooya Kamel <pooyakamel@gmail.com>
# by Pooya Kamel
#
# name Goto command function
# version 1.0.0
# description Goto cmd for OSX Terminal
# homepageURL https://github.com/sirpooya/goto-bash
# supportURL https://github.com/sirpooya/goto-bash/issues
# author Pooya Kamel
# license GPL
uplink() {
#T0DO : uplink <source name> <destination name> -- makes hard link
#/usr/local/bin/goto
link_file=.link
if [[ ! -f $link_file ]]; then
echo "No Link file found"
else
item=`grep "→" ".link"`;
if [[ -z $item ]]; then
echo 'No items found to Link'
else
file=`echo "$item" | cut -d\→ -f1`
link=`echo "$item" | cut -d\→ -f2`
ln -vif "$file" "$link"
chmod +x "$link"
#. ~/.bash_profile
#export PATH=$PATH":$HOME/bin"
fi
fi
}
cecho() {
arg=$1;
arg2=$2;
if [[ -z $arg2 ]] ; then
color=$(tput setaf 4);
else
while [ $2 -gt 0 ] ; do
case $arg2 in
"1" ) #red
color=$(tput setaf 1);
break ;;
"2" ) #green
color=$(tput setaf 2);
break ;;
"3" ) #yellow
color=$(tput setaf 3);
break ;;
"4" ) #blue
color=$(tput setaf 4);
break ;;
"5" ) #magenta
color=$(tput setaf 5);
break ;;
"6" ) #cyan
color=$(tput setaf 6);
break ;;
esac
done
fi
bold=$(tput bold);
reset=$(tput sgr0);
echo $bold$color$arg$reset;
}
opent() {
# T0DO : open directories inside : opent bin --> opne ./bin
if [ $# -eq 0 ] ; then url=`pwd`; parent="${PWD##*/}";
else url=($1); parent="${url##*/}"; fi;
osascript -e 'tell application "Finder"' -e 'activate' -e 'tell application "System Events"' -e 'keystroke "t" using command down' -e 'end tell' -e 'set target of front Finder window to ("'$url'" as POSIX file)' -e 'end tell' -e '--say "'$parent'"' ; cecho "🙂 Opening \"$parent\" ..." 5;
}
bookmarks_file=~/.bookmarks
# Create bookmarks_file it if it doesn't exist
if [[ ! -f $bookmarks_file ]]; then
touch $bookmarks_file
fi
bookmark (){
bookmark_name=$1
if [[ -z $bookmark_name ]]; then
cecho '💩 Please type a valid name for your bookmark.' 3;
else
bookmark="`pwd`|$bookmark_name" # Store the bookmark as folder|name
if [[ -z `grep "|$bookmark_name" $bookmarks_file` ]]; then
echo $bookmark >> $bookmarks_file
cecho "✅ Bookmark '$bookmark_name' saved" 2;
else
cecho "🐵 Bookmark '$bookmark_name' already exists. Replace it? (y or n)" 5;
while read replace
do
if [[ $replace = "y" ]]; then
# Delete existing bookmark
sed "/.*|$bookmark_name/d" $bookmarks_file > ~/.tmp && mv ~/.tmp $bookmarks_file
# Save new bookmark
echo $bookmark >> $bookmarks_file
cecho "✅ Bookmark '$bookmark_name' saved" 2;
break
elif [[ $replace = "n" ]]; then
break
else
cecho "🙈 Please type 'y' or 'n' :" 5;
fi
done
fi
fi
}
# Delete the named bookmark from the list
deletemark (){
bookmark_name=$1
if [[ -z $bookmark_name ]]; then
cecho '👊 Type bookmark name to delete.' 3;
else
bookmark=`grep "|$bookmark_name$" "$bookmarks_file"`
if [[ -z $bookmark ]]; then
cecho '🙈 Bookmark name is invalid.' 3;
else
cat $bookmarks_file | grep -v "|$bookmark_name$" $bookmarks_file > bookmarks_temp && mv bookmarks_temp $bookmarks_file
cecho "❌ Bookmark '$bookmark_name' deleted" 1;
fi
fi
}
# Show a list of the bookmarks
showmarks (){
yellow=$(tput setaf 3); normal=$(tput sgr0);
cat $bookmarks_file | awk '{ printf "👉 '${yellow}'%-10s'${normal}'%s\n",$2,$1}' FS=\|
#cat $bookmarks_file | awk '{ printf "%-40s%-40s%s\n",$1,$2,$3}' FS=\|
}
gotodir(){
bookmark_name=$1
bookmark=`grep "|$bookmark_name$" "$bookmarks_file"`
if [[ -z $bookmark ]]; then
cecho '🙈 Bookmark not found!' 3;
else
dir=`echo "$bookmark" | cut -d\| -f1`
cd "$dir"
fi
}
goback() {
cd $OLDPWD;
}
cpydir() {
adr=$PWD;
echo -n $adr | pbcopy;
}
goto() {
if [ $# -eq 0 ] ; then
showmarks;
fi
while [ $# -gt 0 ]; do
arg=$1;
case $arg in
"-ver" | "--version" | "-v" )
showVersion;
break ;;
"-cp" )
cpydir;
break;;
"-s" | "-b" )
bookmark $2;
break ;;
"-d" )
deletemark $2;
break ;;
"-list" | "-all" | "-l" )
showmarks;
break ;;
"help" )
showHelp;
break ;;
* )
if [ $# != 1 ]; then
cecho "🙈 Whaaaat?!!" 3;
else
gotodir $1;
fi
break ;;
esac
done
}
showVersion () {
echo "goto v1.0.0";
}
showHelp () {
echo " USAGE:";
echo;
echo " Goto <command>";
#echo " `basename $0` <command>";
echo;
echo " COMMANDS:";
echo;
echo " opent # (Mac Only) Open current directory in new Finder Tab.";
echo " opent <location> # (Mac Only) Open location in new Finder Tab.";
echo;
echo " goto # Shows help.";
echo " goto /User/ ./Home ~/help # Goes to directory.";
echo " goto -all | -list # Shows all bookmarks.";
echo " goto <bookmark name> # Goes to bookmarked directory.";
echo " goto -s <bookmark name> # Saves current directory to bookmarks with given name";
echo " goto back # Goes back in history";
echo " goto -cp # Copy address to clipboard";
echo " goto -d # Deletes bookmark";
echo;
#echo " mkals # Makes Finder Alias";
#echo " search # Searchs for a keyword in files & folders";
#echo " own # Change file ownership as root & executable.";
echo;
echo " help # show help file.";
echo " -ver # Show version.";
echo;
echo;
}
#if [ $# = 0 ]; then
#showHelp;
#fi
# T0DO : add shorcuts to this function
# $ goto -a apps /Applications
# $ goto -a
# apps docs sites
# $ goto apps
# $ goto photos
# Shortcut not found
# T0D0 : version-ing be like:
# $ goto --version
# Goto v1.0.0