Skip to content

Commit

Permalink
Extract escaped_id to a function
Browse files Browse the repository at this point in the history
As this is now used in multiple places.
  • Loading branch information
erikw committed Nov 4, 2021
1 parent 76f9917 commit 4ec2da5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cd-bookmark
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function _cdbookmark_print_error() {
echo "Try \`-h' option for more information." 1>&2
}

function _cdbookmark_escape_id() {
echo $1 | sed -e 's/[^a-zA-Z0-9<>]/\\&/g'
}

function _cdbookmark_delete_bookmark() {
local bookmark_id="$1"
local bookmark_dir="$(_cdbookmark_get_bookmark $bookmark_id)"
Expand All @@ -60,7 +64,7 @@ function _cdbookmark_delete_bookmark() {
return 4
fi

local escaped_id="$(echo $bookmark_id | sed -e 's/[^a-zA-Z0-9<>]/\\&/g')"
local escaped_id="$(_cdbookmark_escape_id $1)"
# To be compatiable with both GNU sed and the old BSD sed that macOS uses, need to set and inplace backup extension and then remove the file.
# Reference: https://unix.stackexchange.com/a/131940/19909
sed -i.bak -e "/^${escaped_id}|/d" "$BOOKMARK_FILE" && rm "$BOOKMARK_FILE.bak"
Expand All @@ -84,7 +88,7 @@ function _cdbookmark_list_bookmark_id() {
}

function _cdbookmark_get_bookmark() {
local escaped_id="$(echo $1 | sed -e 's/[^a-zA-Z0-9<>]/\\&/g')"
local escaped_id="$(_cdbookmark_escape_id $1)"
cat "$BOOKMARK_FILE" | grep -E "^${escaped_id}\\|" | cut -d '|' -f 2 | head -n 1
}

Expand Down

0 comments on commit 4ec2da5

Please sign in to comment.