Skip to content

Commit

Permalink
feat(erase): guess unspecified type + scope; list if multiple matches
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed Apr 7, 2020
1 parent 8149669 commit 1976228
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
9 changes: 4 additions & 5 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Key:
- [ ] stronger tests?
- [ ] log message about what change was made
- [ ] include --quiet option
- [ ] erase should not require correct scope and type flags.
- If only one abbreviation match found, delete it regardless of flags
- [x] erase does not require correct scope and type flags.
- Look for any matches that do not violate specified type and/or scope
- If only one match found, erase it
- If multiple matches found, either
- list them and hint what flags to use
- or provide an interactive list (numbered? or a native zsh menu selection?)

Chrome

Expand All @@ -34,5 +34,4 @@ More idiomatic zsh
- [x] rework variable values to support using (( ${+var} )) instead of [[ -n "$var" ]]
- [x] no `if [[ $var == true ]]` where just `if $var` would work
- [x] any other places to tighten up boolean checks
- [ ] maybe use zparseopts?
- [ ] split abbr into its own file, and autoload it
- [ ] maybe split abbr into its own file, and autoload it
60 changes: 42 additions & 18 deletions zsh-abbr.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ _zsh_abbr() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:erase"

local abbreviation
local abbreviations_set
local abbreviations_sets
local message

if [[ $# > 1 ]]; then
_zsh_abbr:util_error " erase: Expected one argument"
Expand All @@ -79,41 +80,64 @@ _zsh_abbr() {
fi

abbreviation=$1
abbreviations_sets=()

if [[ $scope == 'session' ]]; then
if [[ $type == 'global' ]]; then
if [[ $scope != 'user' ]]; then
if [[ $type != 'regular' ]]; then
if (( ${+GLOBAL_SESSION_ABBREVIATIONS[$abbreviation]} )); then
abbreviations_set=GLOBAL_SESSION_ABBREVIATIONS
(( ZSH_ABBR_DEBUG )) && echo " Found a global session abbreviation"
abbreviations_sets+=( GLOBAL_SESSION_ABBREVIATIONS )
fi
elif (( ${+REGULAR_SESSION_ABBREVIATIONS[$abbreviation]} )); then
abbreviations_set=REGULAR_SESSION_ABBREVIATIONS
fi
else
if [[ $type == 'global' ]]; then

if [[ $type != 'global' ]]; then
if (( ${+REGULAR_SESSION_ABBREVIATIONS[$abbreviation]} )); then
(( ZSH_ABBR_DEBUG )) && echo " Found a regular session abbreviation"
abbreviations_sets+=( REGULAR_SESSION_ABBREVIATIONS )
fi
fi
fi

if [[ $scope != 'session' ]]; then
if [[ $type != 'regular' ]]; then
source "${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations"

if (( ${+GLOBAL_USER_ABBREVIATIONS[$abbreviation]} )); then
abbreviations_set=REGULAR_SESSION_ABBREVIATIONS
(( ZSH_ABBR_DEBUG )) && echo " Found a global user abbreviation"
abbreviations_sets+=( GLOBAL_USER_ABBREVIATIONS )
fi
else
fi

if [[ $type != 'global' ]]; then
source "${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations"

if (( ${+REGULAR_USER_ABBREVIATIONS[$abbreviation]} )); then
abbreviations_set=REGULAR_USER_ABBREVIATIONS
(( ZSH_ABBR_DEBUG )) && echo " Found a regular user abbreviation"
abbreviations_sets+=( REGULAR_USER_ABBREVIATIONS )
fi
fi
fi

if ! [ $abbreviations_set ]; then
if ! (( ${#abbreviations_sets} )); then
_zsh_abbr:util_error " erase: No ${type:-regular} ${scope:-user} abbreviation \`$abbreviation\` found"
elif (( dry_run )); then
echo "Erase ${type:-regular} ${scope:-user} abbreviation \`$abbreviation\`"
else
unset "${abbreviations_set}[${(b)abbreviation}]" # quotation marks required
elif [[ ${#abbreviations_sets} == 1 ]]; then
if (( dry_run )); then
echo "Erase ${type:-regular} ${scope:-user} abbreviation \`$abbreviation\`"
else
unset "${abbreviations_sets}[${(b)abbreviation}]" # quotation marks required

if [[ $abbreviations_set =~ USER ]]; then
_zsh_abbr:util_sync_user
if [[ $abbreviations_sets =~ USER ]]; then
_zsh_abbr:util_sync_user
fi
fi
else
message=" erase: Multiple abbreviations \`$abbreviation\`. Please specify one of\\n"

for abbreviations_set in ${abbreviations_sets[@]}; do
message+=" ${${${abbreviations_set:l}//_/ }//abbreviations/}\\n"
done

_zsh_abbr:util_error $message
fi
}

Expand Down

0 comments on commit 1976228

Please sign in to comment.