Skip to content

Commit

Permalink
Add delete command -d
Browse files Browse the repository at this point in the history
The missing piece for this shell program: easily delete bookmarkas as
well without having to being faced with all bookmarks with the -e edit
command. When one know the BOOKMARK_ID, it should be as easy as

$ cd-bookmark -d BOOKMAK_ID

to remove it!

Fixes mollifier#5
  • Loading branch information
erikw committed Nov 4, 2021
1 parent c5dc6b6 commit 75c01fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ In the 3rd form, list current bookmark.
* `-a` <var>[BOOKMARK\_ID]</var> add current directory to bookmark<br />
with no BOOKMARK\_ID, automatically use free ID number as BOOKMARK\_ID
* `-c` <var>BOOKMARK\_ID</var> change directory which is identified by BOOKMARK\_ID
* `-d` <var>BOOKMARK\_ID</var> delete directory which is identified by BOOKMARK\_ID
* `-l` list bookmark
* `-e` edit bookmark file
* `-p` <var>BOOKMARK\_ID</var> display bookmark real path for BOOKMARK\_ID
Expand Down Expand Up @@ -119,5 +120,8 @@ work|/home/mollifier/work
# To edit bookmark, run cd-bookmark with -e option.
% cd-bookmark -e
# Open bookmark file with $EDITOR (vim, emacs, etc.), so you can edit bookmark.
# To delete a bookmark, run cd-bookmark with -d option.
% cd-bookmark -d work
```

2 changes: 1 addition & 1 deletion _cd-bookmark
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare -a opts args
args=(
'(:)-a[add current directory to bookmark]:new_bookmark_id:_cd-bookmark_current_directory_name'
'-c[change directory which is identified by BOOKMARK_ID]:bookmark_id:->bookmark_id_or_path'
'-d[delete directory which is identified by BOOKMARK_ID]:bookmark_id:->bookmark_id_or_path'
'-l[list bookmark]'
'-e[edit bookmark file]'
'-p[display bookmark real path for BOOKMARK_ID]:bookmark_id:_cd-bookmark_bookmark_id'
Expand Down Expand Up @@ -82,4 +83,3 @@ return $ret
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

23 changes: 22 additions & 1 deletion cd-bookmark
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ For example if 'cd-bookmark work' command will change current directory to '~/wo
-a [BOOKMARK_ID] add current directory to bookmark
with no BOOKMARK_ID, automatically use free ID number as BOOKMARK_ID
-c BOOKMARK_ID change directory which is identified by BOOKMARK_ID
-d BOOKMARK_ID delete directory which is identified by BOOKMARK_ID
-l list bookmark
-e edit bookmark file
-p BOOKMARK_ID display bookmark real path for BOOKMARK_ID
Expand All @@ -50,6 +51,19 @@ function _cdbookmark_print_error() {
echo "Try \`-h' option for more information." 1>&2
}

function _cdbookmark_delete_bookmark() {
local bookmark_id="$1"
local bookmark_dir="$(_cdbookmark_get_bookmark $bookmark_id)"

if [ -z "$bookmark_dir" ]; then
_cdbookmark_print_error "$bookmark_id is not in bookmark"
return 4
fi

local escaped_id="$(echo $bookmark_id | sed -e 's/[^a-zA-Z0-9<>]/\\&/g')"
sed -i -e "/^${escaped_id}|/d" "$BOOKMARK_FILE"
}

function _cdbookmark_edit_bookmark() {
# open BOOKMARK_FILE with EDITOR.
${EDITOR:-vi} "$BOOKMARK_FILE"
Expand Down Expand Up @@ -145,7 +159,7 @@ function _cdbookmark_main() {
local bookmark_id=""

local option OPTARG OPTIND
while getopts ':ac:lep:h' option; do
while getopts ':ac:d:lep:h' option; do
case $option in
a)
mode="add"
Expand All @@ -154,6 +168,10 @@ function _cdbookmark_main() {
bookmark_id="$OPTARG"
mode="cd"
;;
d)
bookmark_id="$OPTARG"
mode="delete"
;;
l)
mode="list"
;;
Expand Down Expand Up @@ -208,6 +226,9 @@ function _cdbookmark_main() {
cd)
_cdbookmark_cd_to_bookmark "$bookmark_id"
;;
delete)
_cdbookmark_delete_bookmark "$bookmark_id"
;;
list)
_cdbookmark_list_bookmark
;;
Expand Down

0 comments on commit 75c01fc

Please sign in to comment.