Add extra checks to bookmark names

This commit is contained in:
Alexander Rosenberg 2022-11-08 22:15:32 -08:00
parent 2c6de4d2ae
commit 6433a7bdbf
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
2 changed files with 8 additions and 2 deletions

View File

@ -6,12 +6,16 @@
function __zsh_list_bookmarks { function __zsh_list_bookmarks {
command ls "${ZSH_BOOKMARK_DIR}" command ls "${ZSH_BOOKMARK_DIR}"
} }
function __zsh_check_bookmark_name {
! [[ "${1}" == *'/'* ]]
}
# 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 }
[ -e "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No such bookmark: '${1}'!"; return 1 } [ -e "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No such bookmark: '${1}'!"; 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
@ -31,6 +35,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 }
if [ -e "${ZSH_BOOKMARK_DIR}/${bm_name}" ]; then if [ -e "${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
@ -45,6 +50,7 @@ function bmadd {
# bmrm <name> # bmrm <name>
function bmrm { function bmrm {
[ "${#}" -eq 0 ] && { echo "usage: bmrm <name>"; return 1 } [ "${#}" -eq 0 ] && { echo "usage: bmrm <name>"; return 1 }
__zsh_check_bookmark_name "${1}" || { echo "No bookmark with the name '${1}' exists!"; return 1 }
[ -e "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No bookmark with the name '${1}' exists!"; return 1 } [ -e "${ZSH_BOOKMARK_DIR}/${1}" ] || { echo "No bookmark with the name '${1}' exists!"; return 1 }
local reply local reply
read -q 'reply?Really delete? [y/N] ' read -q 'reply?Really delete? [y/N] '

View File

@ -9,8 +9,8 @@ function load_plugin {
source "${ZSH_PLUGIN_DIR}/${1}/${1}.plugin.zsh" source "${ZSH_PLUGIN_DIR}/${1}/${1}.plugin.zsh"
} }
__ZSH_EARLY_LOCAL_FILE="${ZSH_CONFIG_DIR}/local-early.zsh" ZSH_EARLY_LOCAL_FILE="${ZSH_CONFIG_DIR}/local-early.zsh"
[ -e "${__ZSH_EARLY_LOCAL_FILE}" ] && source "${__ZSH_EARLY_LOCAL_FILE}" [ -e "${ZSH_EARLY_LOCAL_FILE}" ] && source "${ZSH_EARLY_LOCAL_FILE}"
# Some options # Some options
setopt autocd extendedglob rm_star_silent setopt autocd extendedglob rm_star_silent