mirror of
https://github.com/ohmybash/oh-my-bash.git
synced 2024-05-11 05:55:37 +00:00
plugins/bashmarks: Refactor function names and variable names
This commit is contained in:
@@ -45,11 +45,29 @@
|
||||
# bm -d [TAB] - tab completion is available
|
||||
# bm -l - list all bookmarks
|
||||
|
||||
# setup file to store bookmarks
|
||||
if [[ ! $SDIRS ]]; then
|
||||
SDIRS=~/.sdirs
|
||||
# Default configurations
|
||||
if [[ ! ${BASHMARKS_SDIRS-} ]]; then
|
||||
BASHMARKS_SDIRS=${SDIRS:-$HOME/.sdirs}
|
||||
fi
|
||||
|
||||
# Deprecated interfaces (2023-10-12)
|
||||
_omb_deprecate_declare 20000 SDIRS BASHMARKS_SDIRS sync
|
||||
_omb_deprecate_function 20000 _echo_usage _bashmarks_usage
|
||||
_omb_deprecate_function 20000 _save_bookmark _bashmarks_save
|
||||
_omb_deprecate_function 20000 _delete_bookmark _bashmarks_delete
|
||||
_omb_deprecate_function 20000 _goto_bookmark _bashmarks_goto
|
||||
_omb_deprecate_function 20000 _list_bookmark _bashmarks_list
|
||||
_omb_deprecate_function 20000 _print_bookmark _bashmarks_print
|
||||
_omb_deprecate_function 20000 _l _bashmarks_list_names
|
||||
_omb_deprecate_function 20000 _bookmark_name_valid _bashmarks_is_valid_bookmark_name
|
||||
_omb_deprecate_function 20000 _comp _bashmarks_comp_cmd_bm
|
||||
_omb_deprecate_function 20000 _compzsh _bashmarks_compzsh_cmd_bm
|
||||
_omb_deprecate_function 20000 _purge_line _bashmarks_purge_line
|
||||
|
||||
# setup file to store bookmarks
|
||||
if [[ ! -e $BASHMARKS_SDIRS ]]; then
|
||||
touch "$BASHMARKS_SDIRS"
|
||||
fi
|
||||
touch "$SDIRS"
|
||||
|
||||
# main function
|
||||
function bm {
|
||||
@@ -57,48 +75,48 @@ function bm {
|
||||
case $option in
|
||||
# save current directory to bookmarks [ bm -a BOOKMARK_NAME ]
|
||||
-a)
|
||||
_save_bookmark "$2"
|
||||
_bashmarks_save "$2"
|
||||
;;
|
||||
# delete bookmark [ bm -d BOOKMARK_NAME ]
|
||||
-d)
|
||||
_delete_bookmark "$2"
|
||||
_bashmarks_delete "$2"
|
||||
;;
|
||||
# jump to bookmark [ bm -g BOOKMARK_NAME ]
|
||||
-g)
|
||||
_goto_bookmark "$2"
|
||||
_bashmarks_goto "$2"
|
||||
;;
|
||||
# print bookmark [ bm -p BOOKMARK_NAME ]
|
||||
-p)
|
||||
_print_bookmark "$2"
|
||||
_bashmarks_print "$2"
|
||||
;;
|
||||
# show bookmark list [ bm -l ]
|
||||
-l)
|
||||
_list_bookmark
|
||||
_bashmarks_list
|
||||
;;
|
||||
# help [ bm -h ]
|
||||
-h)
|
||||
_echo_usage
|
||||
_bashmarks_usage
|
||||
;;
|
||||
*)
|
||||
if [[ $1 == -* ]]; then
|
||||
# unrecognized option. echo error message and usage [ bm -X ]
|
||||
echo "Unknown option '$1'"
|
||||
_echo_usage
|
||||
_bashmarks_usage
|
||||
kill -SIGINT $$
|
||||
exit 1
|
||||
elif [[ $1 == "" ]]; then
|
||||
# no args supplied - echo usage [ bm ]
|
||||
_echo_usage
|
||||
_bashmarks_usage
|
||||
else
|
||||
# non-option supplied as first arg. assume goto [ bm BOOKMARK_NAME ]
|
||||
_goto_bookmark "$1"
|
||||
_bashmarks_goto "$1"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# print usage information
|
||||
function _echo_usage {
|
||||
function _bashmarks_usage {
|
||||
echo 'USAGE:'
|
||||
echo "bm -h - Prints this usage info"
|
||||
echo 'bm -a <bookmark_name> - Saves the current directory as "bookmark_name"'
|
||||
@@ -109,29 +127,25 @@ function _echo_usage {
|
||||
}
|
||||
|
||||
# save current directory to bookmarks
|
||||
function _save_bookmark {
|
||||
local exit_message
|
||||
_bookmark_name_valid "$@"
|
||||
if [[ ! $exit_message ]]; then
|
||||
_purge_line "$SDIRS" "export DIR_$1="
|
||||
function _bashmarks_save {
|
||||
if _bashmarks_is_valid_bookmark_name "$@"; then
|
||||
_bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1="
|
||||
CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g")
|
||||
echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS
|
||||
echo "export DIR_$1=\"$CURDIR\"" >> "$BASHMARKS_SDIRS"
|
||||
fi
|
||||
}
|
||||
|
||||
# delete bookmark
|
||||
function _delete_bookmark {
|
||||
local exit_message
|
||||
_bookmark_name_valid "$@"
|
||||
if [[ ! $exit_message ]]; then
|
||||
_purge_line "$SDIRS" "export DIR_$1="
|
||||
function _bashmarks_delete {
|
||||
if _bashmarks_is_valid_bookmark_name "$@"; then
|
||||
_bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1="
|
||||
unset "DIR_$1"
|
||||
fi
|
||||
}
|
||||
|
||||
# jump to bookmark
|
||||
function _goto_bookmark {
|
||||
source "$SDIRS"
|
||||
function _bashmarks_goto {
|
||||
source "$BASHMARKS_SDIRS"
|
||||
local target_varname=DIR_$1
|
||||
local target=${!target_varname-}
|
||||
if [[ -d $target ]]; then
|
||||
@@ -144,8 +158,8 @@ function _goto_bookmark {
|
||||
}
|
||||
|
||||
# list bookmarks with dirname
|
||||
function _list_bookmark {
|
||||
source "$SDIRS"
|
||||
function _bashmarks_list {
|
||||
source "$BASHMARKS_SDIRS"
|
||||
# if color output is not working for you, comment out the line below '\033[1;32m' == "red"
|
||||
env | sort | awk '/^DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}'
|
||||
# uncomment this line if color output is not working with the line above
|
||||
@@ -153,48 +167,49 @@ function _list_bookmark {
|
||||
}
|
||||
|
||||
# print bookmark
|
||||
function _print_bookmark {
|
||||
source "$SDIRS"
|
||||
function _bashmarks_print {
|
||||
source "$BASHMARKS_SDIRS"
|
||||
echo "$(eval $(echo echo $(echo \$DIR_$1)))"
|
||||
}
|
||||
|
||||
# list bookmarks without dirname
|
||||
function _l {
|
||||
source "$SDIRS"
|
||||
function _bashmarks_list_names {
|
||||
source "$BASHMARKS_SDIRS"
|
||||
env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "="
|
||||
}
|
||||
|
||||
# validate bookmark name
|
||||
# @var[out] exit_message
|
||||
function _bookmark_name_valid {
|
||||
exit_message=""
|
||||
function _bashmarks_is_valid_bookmark_name {
|
||||
local exit_message=""
|
||||
if [[ ! $1 ]]; then
|
||||
exit_message="bookmark name required"
|
||||
echo "$exit_message"
|
||||
echo "$exit_message" >&2
|
||||
return 1
|
||||
elif [[ $1 == *[!A-Za-z0-9_]* ]]; then
|
||||
exit_message="bookmark name is not valid"
|
||||
echo "$exit_message"
|
||||
echo "$exit_message" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# completion command
|
||||
function _comp {
|
||||
local curw
|
||||
function _bashmarks_comp_cmd_bm {
|
||||
COMPREPLY=()
|
||||
if ((COMP_CWORD >= 2)) && [[ ${COMP_WORDS[1]} == -[gpd] ]]; then
|
||||
local cur=${COMP_WORDS[COMP_CWORD]}
|
||||
COMPREPLY=($(compgen -W '$(_l)' -- "$cur"))
|
||||
COMPREPLY=($(compgen -W '$(_bashmarks_list_names)' -- "$cur"))
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# ZSH completion command
|
||||
function _compzsh {
|
||||
reply=($(_l))
|
||||
function _bashmarks_compzsh_cmd_bm {
|
||||
reply=($(_bashmarks_list_names))
|
||||
}
|
||||
|
||||
# safe delete line from sdirs
|
||||
function _purge_line {
|
||||
function _bashmarks_purge_line {
|
||||
if [[ -s $1 ]]; then
|
||||
# safely create a temp file
|
||||
t=$(mktemp -t bashmarks.XXXXXX) || exit 1
|
||||
@@ -210,20 +225,22 @@ function _purge_line {
|
||||
fi
|
||||
}
|
||||
|
||||
# bind completion command for g,p,d to _comp
|
||||
# bind completion command for g,p,d to _bashmarks_comp_cmd_bm
|
||||
if [[ ${ZSH_VERSION-} ]]; then
|
||||
compctl -K _compzsh bm -g
|
||||
compctl -K _compzsh bm -p
|
||||
compctl -K _compzsh bm -d
|
||||
compctl -K _compzsh g
|
||||
compctl -K _compzsh p
|
||||
compctl -K _compzsh d
|
||||
compctl -K _bashmarks_compzsh_cmd_bm bm -g
|
||||
compctl -K _bashmarks_compzsh_cmd_bm bm -p
|
||||
compctl -K _bashmarks_compzsh_cmd_bm bm -d
|
||||
compctl -K _bashmarks_compzsh_cmd_bm g
|
||||
compctl -K _bashmarks_compzsh_cmd_bm p
|
||||
compctl -K _bashmarks_compzsh_cmd_bm d
|
||||
else
|
||||
shopt -s progcomp
|
||||
complete -F _comp bm
|
||||
complete -F _comp g
|
||||
complete -F _comp p
|
||||
complete -F _comp d
|
||||
complete -F _bashmarks_comp_cmd_bm bm
|
||||
complete -F _bashmarks_comp_cmd_bm bm
|
||||
complete -F _bashmarks_comp_cmd_bm bm
|
||||
complete -F _bashmarks_comp_cmd_bm g
|
||||
complete -F _bashmarks_comp_cmd_bm p
|
||||
complete -F _bashmarks_comp_cmd_bm d
|
||||
fi
|
||||
|
||||
alias s='bm -a' # Save a bookmark [bookmark_name]
|
||||
|
Reference in New Issue
Block a user