Skip to content

Commit 379c3d2

Browse files
committed
Merge commit pull request huyng#14 from upstream.
1 parent 342169a commit 379c3d2

File tree

1 file changed

+72
-54
lines changed

1 file changed

+72
-54
lines changed

bashmarks.sh

+72-54
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ if [ ! -n "$SDIRS" ]; then
3838
fi
3939
touch $SDIRS
4040

41-
RED="0;31m"
42-
GREEN="0;33m"
43-
4441
# save current directory to bookmarks
4542
function s {
4643
check_help $1
@@ -54,51 +51,66 @@ function s {
5451

5552
# jump to bookmark
5653
function g {
57-
check_help $1
58-
source $SDIRS
59-
target="$(eval $(echo echo $(echo \$DIR_$1)))"
60-
if [ -d "$target" ]; then
61-
cd "$target"
62-
elif [ ! -n "$target" ]; then
63-
echo -e "\033[${RED}WARNING: '${1}' bashmark does not exist\033[00m"
64-
else
65-
echo -e "\033[${RED}WARNING: '${target}' does not exist\033[00m"
66-
fi
54+
_bashmarks_check_help $@ || _bashmarks_go $@
6755
}
6856

6957
# print bookmark
7058
function p {
71-
check_help $1
72-
source $SDIRS
73-
echo "$(eval $(echo echo $(echo \$DIR_$1)))"
59+
_bashmarks_check_help $@ || _bashmarks_print $@
7460
}
7561

7662
# delete bookmark
7763
function d {
78-
check_help $1
79-
_bookmark_name_valid "$@"
80-
if [ -z "$exit_message" ]; then
81-
_purge_line "$SDIRS" "export DIR_$1="
82-
unset "DIR_$1"
83-
fi
64+
_bashmarks_check_help $@ || _bashmarks_delete $@
65+
}
66+
67+
# list bookmarks with dirname
68+
function l {
69+
_bashmarks_check_help $@ || _bashmarks_list $@
8470
}
8571

8672
# print out help for the forgetful
87-
function check_help {
73+
function _bashmarks_check_help {
8874
if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then
8975
echo ''
9076
echo 's <bookmark_name> - Saves the current directory as "bookmark_name"'
9177
echo 'g <bookmark_name> - Goes (cd) to the directory associated with "bookmark_name"'
9278
echo 'p <bookmark_name> - Prints the directory associated with "bookmark_name"'
9379
echo 'd <bookmark_name> - Deletes the bookmark'
9480
echo 'l - Lists all available bookmarks'
95-
kill -SIGINT $$
81+
return 0
9682
fi
83+
return 1
9784
}
9885

99-
# list bookmarks with dirnam
100-
function l {
101-
check_help $1
86+
function _bashmarks_save {
87+
_bashmarks_bookmark_name_valid "$@"
88+
if [ -z "$_bashmarks_exit_message" ]; then
89+
_bashmarks_purge_line "$SDIRS" "export DIR_$1="
90+
CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g")
91+
echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS
92+
fi
93+
}
94+
95+
function _bashmarks_go {
96+
source $SDIRS
97+
cd "$(eval $(echo echo $(echo \$DIR_$1)))"
98+
}
99+
100+
function _bashmarks_print {
101+
source $SDIRS
102+
echo "$(eval $(echo echo $(echo \$DIR_$1)))"
103+
}
104+
105+
function _bashmarks_delete {
106+
_bashmarks_bookmark_name_valid "$@"
107+
if [ -z "$_bashmarks_exit_message" ]; then
108+
_bashmarks_purge_line "$SDIRS" "export DIR_$1="
109+
unset "DIR_$1"
110+
fi
111+
}
112+
113+
function _bashmarks_list {
102114
source $SDIRS
103115

104116
# if color output is not working for you, comment out the line below '\033[1;32m' == "red"
@@ -107,63 +119,69 @@ function l {
107119
# uncomment this line if color output is not working with the line above
108120
# env | grep "^DIR_" | cut -c5- | sort |grep "^.*="
109121
}
122+
110123
# list bookmarks without dirname
111-
function _l {
124+
function _bashmarks_list_without_dirname {
112125
source $SDIRS
113126
env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "="
114127
}
115128

116129
# validate bookmark name
117-
function _bookmark_name_valid {
118-
exit_message=""
130+
function _bashmarks_bookmark_name_valid {
131+
_bashmarks_exit_message=""
119132
if [ -z $1 ]; then
120-
exit_message="bookmark name required"
121-
echo $exit_message
133+
_bashmarks_exit_message="bookmark name required"
134+
echo $_bashmarks_exit_message
122135
elif [ "$1" != "$(echo $1 | sed 's/[^A-Za-z0-9_]//g')" ]; then
123-
exit_message="bookmark name is not valid"
124-
echo $exit_message
136+
_bashmarks_exit_message="bookmark name is not valid"
137+
echo $_bashmarks_exit_message
125138
fi
126139
}
127140

128141
# completion command
129-
function _comp {
142+
function bashmarks_comp {
130143
local curw
131144
COMPREPLY=()
132145
curw=${COMP_WORDS[COMP_CWORD]}
133-
COMPREPLY=($(compgen -W '`_l`' -- $curw))
146+
COMPREPLY=($(compgen -W '`_bashmarks_list_without_dirname`' -- $curw))
134147
return 0
135148
}
136149

137150
# ZSH completion command
138-
function _compzsh {
139-
reply=($(_l))
151+
function bashmarks_compzsh {
152+
reply=($(_bashmarks_list_without_dirname))
140153
}
141154

142155
# safe delete line from sdirs
143-
function _purge_line {
156+
function _bashmarks_purge_line {
144157
if [ -s "$1" ]; then
145158
# safely create a temp file
146-
t=$(mktemp -t bashmarks.XXXXXX) || exit 1
147-
trap "/bin/rm -f -- '$t'" EXIT
159+
t=$(mktemp -t bashmarks.XXXXXX)
160+
161+
if [ $? -eq 0 ]; then
162+
trap "rm -f -- '$t'" EXIT
148163

149-
# purge line
150-
sed "/$2/d" "$1" > "$t"
151-
/bin/mv "$t" "$1"
164+
# purge line
165+
sed "/$2/d" "$1" > "$t"
166+
mv "$t" "$1"
152167

153-
# cleanup temp file
154-
/bin/rm -f -- "$t"
155-
trap - EXIT
168+
# cleanup temp file
169+
rm -f -- "$t"
170+
trap - EXIT
171+
else
172+
echo "Failed to create temporary file." >&2
173+
fi
156174
fi
157175
}
158176

159-
# bind completion command for g,p,d to _comp
177+
# bind completion command for g,p,d to bashmarks_comp
160178
if [ $ZSH_VERSION ]; then
161-
compctl -K _compzsh g
162-
compctl -K _compzsh p
163-
compctl -K _compzsh d
179+
compctl -K bashmarks_compzsh g
180+
compctl -K bashmarks_compzsh p
181+
compctl -K bashmarks_compzsh d
164182
else
165183
shopt -s progcomp
166-
complete -F _comp g
167-
complete -F _comp p
168-
complete -F _comp d
184+
complete -F bashmarks_comp g
185+
complete -F bashmarks_comp p
186+
complete -F bashmarks_comp d
169187
fi

0 commit comments

Comments
 (0)