Overhaul bookmark.zsh completions
This commit is contained in:
parent
3d25e8136c
commit
29fb2c96d0
23
bookmark.zsh
23
bookmark.zsh
@ -4,27 +4,30 @@
|
|||||||
# Create bookmark directory if it does not exist
|
# Create bookmark directory if it does not exist
|
||||||
[ ! -d "${ZSH_BOOKMARK_DIR}" ] && mkdir -p "${ZSH_BOOKMARK_DIR}"
|
[ ! -d "${ZSH_BOOKMARK_DIR}" ] && mkdir -p "${ZSH_BOOKMARK_DIR}"
|
||||||
function __zsh_list_bookmarks {
|
function __zsh_list_bookmarks {
|
||||||
command ls "${ZSH_BOOKMARK_DIR}"
|
/bin/ls "${ZSH_BOOKMARK_DIR}"
|
||||||
}
|
}
|
||||||
function __zsh_check_bookmark_name {
|
function __zsh_check_bookmark_name {
|
||||||
! [[ "${1}" == *'/'* ]]
|
! [[ "${1}" == *'/'* ]]
|
||||||
}
|
}
|
||||||
|
function _bookmarks {
|
||||||
|
for file in "${ZSH_BOOKMARK_DIR}/"*; do
|
||||||
|
compadd "${file#"${ZSH_BOOKMARK_DIR}/"}"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
# bm <bookmark>
|
# bm <bookmark>
|
||||||
function bm {
|
function bm {
|
||||||
# With no args, list all bookmarks
|
# With no args, list all bookmarks
|
||||||
[ "${#}" -eq 0 ] && { __zsh_list_bookmarks; return }
|
[ "${#}" -eq 0 ] && { __zsh_list_bookmarks; return }
|
||||||
# Otherwise, goto the bookmark in argv[1]
|
# Otherwise, goto the bookmark in argv[1]
|
||||||
__zsh_check_bookmark_name "${1}" || { echo "No such bookmark: '${1}'!"; return 1 }
|
__zsh_check_bookmark_name "${1}" || { echo "No such bookmark: '${1}'!"; return 1 }
|
||||||
[ -L "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No such bookmark: '${1}'!"; return 1 }
|
[ -L "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No such bookmark: '${1}'!"; return 1 }
|
||||||
[ -e "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "The bookmark '${1}' refers to a non-existant file!"; return 1 }
|
[ -e "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "The bookmark '${1}' refers to a non-existant file!"; return 1 }
|
||||||
cd -P "${ZSH_BOOKMARK_DIR}/${1}"
|
cd -P "${ZSH_BOOKMARK_DIR}/${1}"
|
||||||
[[ -v ZSH_BOOKMARK_LS ]] && "${ZSH_BOOKMARK_LS}" && ls
|
[[ -v ZSH_BOOKMARK_LS ]] && "${ZSH_BOOKMARK_LS}" && ls
|
||||||
}
|
}
|
||||||
function _bm {
|
function _bm {
|
||||||
for file in "${ZSH_BOOKMARK_DIR}/"*; do
|
_arguments '::bookmark:_bookmarks'
|
||||||
compadd "$(basename "${file}")"
|
|
||||||
done
|
|
||||||
}
|
}
|
||||||
compdef _bm bm
|
compdef _bm bm
|
||||||
|
|
||||||
@ -36,7 +39,7 @@ function bmadd {
|
|||||||
[ "${#}" -eq 1 ] && { bmadd "${1}" "${PWD}"; return }
|
[ "${#}" -eq 1 ] && { bmadd "${1}" "${PWD}"; return }
|
||||||
local real_path="$(realpath "${2}")"
|
local real_path="$(realpath "${2}")"
|
||||||
local bm_name="${1}"
|
local bm_name="${1}"
|
||||||
__zsh_check_bookmark_name "${bm_name}" || { echo "Invalid bookmark: '${1}'!"; return 1 }
|
__zsh_check_bookmark_name "${bm_name}" || { echo "Invalid bookmark: '${1}'!"; return 1 }
|
||||||
if [ -L "${ZSH_BOOKMARK_DIR}/${bm_name}" ]; then
|
if [ -L "${ZSH_BOOKMARK_DIR}/${bm_name}" ]; then
|
||||||
echo "A bookmark with the name '${bm_name}' already exists!"
|
echo "A bookmark with the name '${bm_name}' already exists!"
|
||||||
local reply
|
local reply
|
||||||
@ -47,6 +50,10 @@ function bmadd {
|
|||||||
ln -nfs "${real_path}" "${ZSH_BOOKMARK_DIR}/${bm_name}"
|
ln -nfs "${real_path}" "${ZSH_BOOKMARK_DIR}/${bm_name}"
|
||||||
echo "Created bookmark '${bm_name}'"
|
echo "Created bookmark '${bm_name}'"
|
||||||
}
|
}
|
||||||
|
function _bmadd {
|
||||||
|
_arguments ':name' '::directory:_directories'
|
||||||
|
}
|
||||||
|
compdef _bmadd bmadd
|
||||||
|
|
||||||
# bmrm <name>
|
# bmrm <name>
|
||||||
function bmrm {
|
function bmrm {
|
||||||
@ -57,10 +64,10 @@ function bmrm {
|
|||||||
read -q 'reply?Really delete? [y/N] '
|
read -q 'reply?Really delete? [y/N] '
|
||||||
printf '\n'
|
printf '\n'
|
||||||
[[ "${reply}" =~ '[yY]' ]] || return 1
|
[[ "${reply}" =~ '[yY]' ]] || return 1
|
||||||
command rm "${ZSH_BOOKMARK_DIR}/${1}"
|
/bin/rm "${ZSH_BOOKMARK_DIR}/${1}"
|
||||||
echo "Deleted bookmark '${1}'"
|
echo "Deleted bookmark '${1}'"
|
||||||
}
|
}
|
||||||
function _bmrm {
|
function _bmrm {
|
||||||
_bm
|
_arguments ':bookmark:_bookmarks'
|
||||||
}
|
}
|
||||||
compdef _bmrm bmrm
|
compdef _bmrm bmrm
|
||||||
|
Loading…
x
Reference in New Issue
Block a user