From afa8263fff59108aa69d2c5c13cb87046134f64e Mon Sep 17 00:00:00 2001 From: abhishek Date: Fri, 4 Mar 2011 17:59:31 +1300 Subject: [PATCH 1/3] do not exit the shell if mktemp fails --- bashmarks.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/bashmarks.sh b/bashmarks.sh index dff401c..9186306 100644 --- a/bashmarks.sh +++ b/bashmarks.sh @@ -133,16 +133,21 @@ function _compzsh { function _purge_line { if [ -s "$1" ]; then # safely create a temp file - t=$(mktemp -t bashmarks.XXXXXX) || exit 1 - trap "rm -f -- '$t'" EXIT + t=$(mktemp -t bashmarks.XXXXXX) - # purge line - sed "/$2/d" "$1" > "$t" - mv "$t" "$1" + if [ $? -eq 0 ]; then + trap "rm -f -- '$t'" EXIT - # cleanup temp file - rm -f -- "$t" - trap - EXIT + # purge line + sed "/$2/d" "$1" > "$t" + mv "$t" "$1" + + # cleanup temp file + rm -f -- "$t" + trap - EXIT + else + echo "Failed to create temporary file." >&2 + fi fi } From 689d46f39f94e7ef00e30b0bd989ce87f5a91ab0 Mon Sep 17 00:00:00 2001 From: abhishek Date: Fri, 4 Mar 2011 18:22:59 +1300 Subject: [PATCH 2/3] add prefixes to private and public function and variable names, only leaving interactive function and configurable variable names unprefixed --- bashmarks.sh | 62 ++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/bashmarks.sh b/bashmarks.sh index 9186306..a5b4d47 100644 --- a/bashmarks.sh +++ b/bashmarks.sh @@ -40,10 +40,10 @@ touch $SDIRS # save current directory to bookmarks function s { - check_help $1 - _bookmark_name_valid "$@" - if [ -z "$exit_message" ]; then - _purge_line "$SDIRS" "export DIR_$1=" + _bashmarks_check_help $1 + _bashmarks_bookmark_name_valid "$@" + if [ -z "$_bashmarks_exit_message" ]; then + _bashmarks_purge_line "$SDIRS" "export DIR_$1=" CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS fi @@ -51,30 +51,30 @@ function s { # jump to bookmark function g { - check_help $1 + _bashmarks_check_help $1 source $SDIRS cd "$(eval $(echo echo $(echo \$DIR_$1)))" } # print bookmark function p { - check_help $1 + _bashmarks_check_help $1 source $SDIRS echo "$(eval $(echo echo $(echo \$DIR_$1)))" } # delete bookmark function d { - check_help $1 - _bookmark_name_valid "$@" - if [ -z "$exit_message" ]; then - _purge_line "$SDIRS" "export DIR_$1=" + _bashmarks_check_help $1 + _bashmarks_bookmark_name_valid "$@" + if [ -z "$_bashmarks_exit_message" ]; then + _bashmarks_purge_line "$SDIRS" "export DIR_$1=" unset "DIR_$1" fi } # print out help for the forgetful -function check_help { +function _bashmarks_check_help { if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then echo '' echo 's - Saves the current directory as "bookmark_name"' @@ -88,7 +88,7 @@ function check_help { # list bookmarks with dirnam function l { - check_help $1 + _bashmarks_check_help $1 source $SDIRS # if color output is not working for you, comment out the line below '\033[1;32m' == "red" @@ -98,39 +98,39 @@ function l { # env | grep "^DIR_" | cut -c5- | sort |grep "^.*=" } # list bookmarks without dirname -function _l { +function _bashmarks_l { source $SDIRS env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "=" } # validate bookmark name -function _bookmark_name_valid { - exit_message="" +function _bashmarks_bookmark_name_valid { + _bashmarks_exit_message="" if [ -z $1 ]; then - exit_message="bookmark name required" - echo $exit_message + _bashmarks_exit_message="bookmark name required" + echo $_bashmarks_exit_message elif [ "$1" != "$(echo $1 | sed 's/[^A-Za-z0-9_]//g')" ]; then - exit_message="bookmark name is not valid" - echo $exit_message + _bashmarks_exit_message="bookmark name is not valid" + echo $_bashmarks_exit_message fi } # completion command -function _comp { +function bashmarks_comp { local curw COMPREPLY=() curw=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=($(compgen -W '`_l`' -- $curw)) + COMPREPLY=($(compgen -W '`_bashmarks_l`' -- $curw)) return 0 } # ZSH completion command -function _compzsh { - reply=($(_l)) +function bashmarks_compzsh { + reply=($(_bashmarks_l)) } # 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) @@ -151,14 +151,14 @@ function _purge_line { fi } -# bind completion command for g,p,d to _comp +# bind completion command for g,p,d to bashmarks_comp if [ $ZSH_VERSION ]; then - compctl -K _compzsh g - compctl -K _compzsh p - compctl -K _compzsh d + compctl -K bashmarks_compzsh g + compctl -K bashmarks_compzsh p + compctl -K bashmarks_compzsh d else shopt -s progcomp - complete -F _comp g - complete -F _comp p - complete -F _comp d + complete -F bashmarks_comp g + complete -F bashmarks_comp p + complete -F bashmarks_comp d fi From fbfb2bf137a6d43fc69c9e5b3e8d3e52be7a9faf Mon Sep 17 00:00:00 2001 From: abhishek Date: Fri, 4 Mar 2011 18:33:39 +1300 Subject: [PATCH 3/3] refactor with interactive functions defining strategy and private functions implementing commands; also eschews need for kill in help function --- bashmarks.sh | 65 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/bashmarks.sh b/bashmarks.sh index a5b4d47..7266532 100644 --- a/bashmarks.sh +++ b/bashmarks.sh @@ -40,37 +40,27 @@ touch $SDIRS # save current directory to bookmarks function s { - _bashmarks_check_help $1 - _bashmarks_bookmark_name_valid "$@" - if [ -z "$_bashmarks_exit_message" ]; then - _bashmarks_purge_line "$SDIRS" "export DIR_$1=" - CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") - echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS - fi + _bashmarks_check_help $@ || _bashmarks_save $@ } # jump to bookmark function g { - _bashmarks_check_help $1 - source $SDIRS - cd "$(eval $(echo echo $(echo \$DIR_$1)))" + _bashmarks_check_help $@ || _bashmarks_go $@ } # print bookmark function p { - _bashmarks_check_help $1 - source $SDIRS - echo "$(eval $(echo echo $(echo \$DIR_$1)))" + _bashmarks_check_help $@ || _bashmarks_print $@ } # delete bookmark function d { - _bashmarks_check_help $1 - _bashmarks_bookmark_name_valid "$@" - if [ -z "$_bashmarks_exit_message" ]; then - _bashmarks_purge_line "$SDIRS" "export DIR_$1=" - unset "DIR_$1" - fi + _bashmarks_check_help $@ || _bashmarks_delete $@ +} + +# list bookmarks with dirname +function l { + _bashmarks_check_help $@ || _bashmarks_list $@ } # print out help for the forgetful @@ -82,13 +72,39 @@ function _bashmarks_check_help { echo 'p - Prints the directory associated with "bookmark_name"' echo 'd - Deletes the bookmark' echo 'l - Lists all available bookmarks' - kill -SIGINT $$ + return 0 fi + return 1 } -# list bookmarks with dirnam -function l { - _bashmarks_check_help $1 +function _bashmarks_save { + _bashmarks_bookmark_name_valid "$@" + if [ -z "$_bashmarks_exit_message" ]; then + _bashmarks_purge_line "$SDIRS" "export DIR_$1=" + CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") + echo "export DIR_$1=\"$CURDIR\"" >> $SDIRS + fi +} + +function _bashmarks_go { + source $SDIRS + cd "$(eval $(echo echo $(echo \$DIR_$1)))" +} + +function _bashmarks_print { + source $SDIRS + echo "$(eval $(echo echo $(echo \$DIR_$1)))" +} + +function _bashmarks_delete { + _bashmarks_bookmark_name_valid "$@" + if [ -z "$_bashmarks_exit_message" ]; then + _bashmarks_purge_line "$SDIRS" "export DIR_$1=" + unset "DIR_$1" + fi +} + +function _bashmarks_list { source $SDIRS # if color output is not working for you, comment out the line below '\033[1;32m' == "red" @@ -97,8 +113,9 @@ function l { # uncomment this line if color output is not working with the line above # env | grep "^DIR_" | cut -c5- | sort |grep "^.*=" } + # list bookmarks without dirname -function _bashmarks_l { +function _bashmarks_list_without_dirname { source $SDIRS env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "=" }