Skip to content

Commit

Permalink
feat(list commands): support listing regulars only
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed Mar 7, 2020
1 parent c0cdce2 commit a5cab16
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 76 deletions.
62 changes: 37 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,22 @@ abbr <OPTION> <SCOPE> <ANY OPTION ARGUMENTS>
[(--session | -S) | (--user | -U)]
```

A given abbreviation can be made available in the current zsh session (i.e. in the current terminal) —these are called *session* abbreviations— or to all terminals —these are called *user* abbreviations.
A given abbreviation can be made available in the current zsh session (i.e. in the current terminal) —these are called *session* abbreviations— or to all terminals —these are called *user* abbreviations. Select options take **scope** as an argument.

Newly added user abbreviations are available to all open sessions immediately.

Default is user.

### Type

```shell
[(--global | -g) | (--regular | -r)]
```

zsh-abbr supports regular abbreviations, which match the word at the start of the command line, and global abbreviations, which match any word on the line. Select options take **type** as an argument.

Default is regular.

### Options

```shell
Expand All @@ -156,7 +166,7 @@ Default is user.
| --import-fish [(--global | -g)] [--dry-run] arg
| --import-git-aliases [--dry-run]
| (--list | -l)
| (--list-commands | -L | -s) [(--session | -S) | (--user | -U)] [(--global | -g)]
| (--list-commands | -L | -s) [(--session | -S) | (--user | -U)] [(--global | -g) | (--regular | -r)]
| (--rename | -R ) [(--global | -g)] [--dry-run] args
]
```
Expand Down Expand Up @@ -404,6 +414,31 @@ a
b
```

#### List Commands

```shell
abbr [(--list-commands | -L | -s)] [(--session | -S) | (--user | -U)] [(--global | -g)]
```

List all the abbreviations available in the current session as commands. Regular abbreviations follow global abbreviations. Session abbreviations follow user abbreviations.

Use the **--session** or **-S** flag to list only a session abbreviations.

Use the **--global** or **-g** flag to list only global abbreviations.

Use the **--regular** or **-r** flag to list only global abbreviations.

```shell
% abbr --add gcm="git checkout master"
% abbr --add --session a="apple"
% abbr --list-commands # or `abbr` with no arguments
abbr gcm="git checkout master"
abbr -S a="apple"
% source ~/.zshrc
% abbr --list-commands
abbr gcm="git checkout master"
```

#### Rename

```shell
Expand Down Expand Up @@ -431,29 +466,6 @@ Use `--dry-run` to see what would result, without making any actual changes..

Abbreviations can also be manually renamed in the `ZSH_USER_ABBREVIATIONS_PATH`.

#### List Commands

```shell
abbr [(--list-commands | -L | -s)] [(--session | -S) | (--user | -U)] [(--global | -g)]
```

List all the abbreviations available in the current session as commands. Session abbreviations are marked `-S` and follow user abbreviations.

Use the **--session** or **-S** flag to list only a session abbreviations.

Use the **--global** flag to list only global abbreviations.

```shell
% abbr --add gcm git checkout master
% abbr --add --session a apple
% abbr --list-commands # or `abbr` with no arguments
abbr gcm="git checkout master"
abbr -S a="apple"
% source ~/.zshrc
% abbr --list-commands
abbr gcm="git checkout master"
```

## Configuration

### Storage
Expand Down
6 changes: 3 additions & 3 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Look like zsh's `alias` not fish's `abbr`
- [x] `-L` list in the form of commands
- [x] `-g` list/define global aliases
- [x] support `-L -g`
- [ ] maybe? `-r` list/define regular aliases
- [x] maybe? `-r` list/define regular aliases
- [ ] abbr on its own (or with -g or -r) lists like alias [-g|-r]
- [ ] don't do this one: `-m` list aliases that match a pattern
- [ ] don't do this one: `-s` list/define suffix aliases
- don't do this one: `-m` list aliases that match a pattern
- don't do this one: `-s` list/define suffix aliases


## 3.x
Expand Down
112 changes: 64 additions & 48 deletions zsh-abbr.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@ _zsh_abbr() {
{
local action_set number_opts opt opt_add opt_clear_session opt_dry_run \
opt_erase opt_expand opt_export_aliases opt_import_git_aliases \
opt_global opt_import_aliases opt_import_fish opt_session opt_list \
opt_rename opt_list_commands opt_user opt_print_version release_date \
scope_set should_exit text_bold text_reset util_usage version
opt_type_global opt_import_aliases opt_import_fish opt_session opt_list \
opt_type_regular opt_rename opt_list_commands opt_user \
opt_print_version type_set release_date scope_set should_exit \
text_bold text_reset util_usage version
action_set=false
number_opts=0
opt_add=false
opt_clear_session=false
opt_dry_run=false
opt_erase=false
opt_expand=false
opt_import_git_aliases=false
opt_global=false
opt_import_fish=false
opt_session=false
opt_list=false
opt_export_aliases=false
opt_type_global=false
opt_import_aliases=false
opt_rename=false
opt_import_fish=false
opt_import_git_aliases=false
opt_list=false
opt_list_commands=false
opt_type_regular=false
opt_rename=false
opt_session=false
opt_user=false
opt_print_version=false
type_set=false
release_date="March 7 2020"
scope_set=false
should_exit=false
Expand All @@ -61,7 +64,7 @@ _zsh_abbr() {
${text_bold}abbr${text_reset} --import-aliases [SCOPE]
${text_bold}abbr${text_reset} --list|-l
${text_bold}abbr${text_reset} --list-commands|-L|-s
${text_bold}abbr${text_reset} --rename|-r [SCOPE] OLD_ABBREVIATION NEW
${text_bold}abbr${text_reset} --rename|-R [SCOPE] OLD_ABBREVIATION NEW
${text_bold}abbr${text_reset} --help|-h
${text_bold}abbr${text_reset} --version|-v
Expand Down Expand Up @@ -254,7 +257,7 @@ _zsh_abbr() {
fi

if $opt_session; then
if $opt_global; then
if $opt_type_global; then
if (( ${+ZSH_ABBR_SESSION_GLOBALS[$1]} )); then
unset "ZSH_ABBR_SESSION_GLOBALS[${(b)1}]"
success=true
Expand All @@ -264,7 +267,7 @@ _zsh_abbr() {
success=true
fi
else
if $opt_global; then
if $opt_type_global; then
source "${TMPDIR:-/tmp}/zsh-user-global-abbreviations"

if (( ${+ZSH_ABBR_USER_GLOBALS[$1]} )); then
Expand Down Expand Up @@ -335,7 +338,7 @@ _zsh_abbr() {
add $_alias
done < <(alias -r)

opt_global=true
opt_type_global=true

while read -r _alias; do
add $_alias
Expand Down Expand Up @@ -416,6 +419,39 @@ _zsh_abbr() {
print -l ${(k)ZSH_ABBR_SESSION_COMMANDS}
}

function list_commands() {
if [ $# -gt 0 ]; then
util_error " list commands: Unexpected argument"
return
fi

if ! $opt_session; then
if ! $opt_type_regular; then
for abbreviation expansion in ${(kv)ZSH_ABBR_USER_GLOBALS}; do
printf "abbr -g %s=\"%s\"\\n" "$abbreviation" "$expansion"
done
fi

if ! $opt_type_global; then
for abbreviation expansion in ${(kv)ZSH_ABBR_USER_COMMANDS}; do
printf "abbr %s=\"%s\"\\n" "$abbreviation" "$expansion"
done
fi
fi

if ! $opt_type_regular; then
for abbreviation expansion in ${(kv)ZSH_ABBR_SESSION_GLOBALS}; do
printf "abbr -S -g %s=\"%s\"\\n" "$abbreviation" "$expansion"
done
fi

if ! $opt_type_global; then
for abbreviation expansion in ${(kv)ZSH_ABBR_SESSION_COMMANDS}; do
printf "abbr -S %s=\"%s\"\\n" "$abbreviation" "$expansion"
done
fi
}

function print_version() {
if [ $# -gt 0 ]; then
util_error " version: Unexpected argument"
Expand All @@ -435,13 +471,13 @@ _zsh_abbr() {
fi

if $opt_session; then
if $opt_global; then
if $opt_type_global; then
expansion=${ZSH_ABBR_SESSION_GLOBALS[$1]}
else
expansion=${ZSH_ABBR_SESSION_COMMANDS[$1]}
fi
else
if $opt_global; then
if $opt_type_global; then
expansion=${ZSH_ABBR_USER_GLOBALS[$1]}
else
expansion=${ZSH_ABBR_USER_COMMANDS[$1]}
Expand All @@ -461,35 +497,6 @@ _zsh_abbr() {
fi
}

function list_commands() {
if [ $# -gt 0 ]; then
util_error " list commands: Unexpected argument"
return
fi

if ! $opt_session; then
for abbreviation expansion in ${(kv)ZSH_ABBR_USER_GLOBALS}; do
printf "abbr -g %s=\"%s\"\\n" "$abbreviation" "$expansion"
done

if ! $opt_global; then
for abbreviation expansion in ${(kv)ZSH_ABBR_USER_COMMANDS}; do
printf "abbr %s=\"%s\"\\n" "$abbreviation" "$expansion"
done
fi
fi

for abbreviation expansion in ${(kv)ZSH_ABBR_SESSION_GLOBALS}; do
printf "abbr -S -g %s=\"%s\"\\n" "$abbreviation" "$expansion"
done

if ! $opt_global; then
for abbreviation expansion in ${(kv)ZSH_ABBR_SESSION_COMMANDS}; do
printf "abbr -S %s=\"%s\"\\n" "$abbreviation" "$expansion"
done
fi
}

function util_add() {
local abbreviation
local abbreviation_last_word
Expand All @@ -516,7 +523,7 @@ _zsh_abbr() {
fi

if $opt_session; then
if $opt_global; then
if $opt_type_global; then
if ! (( ${+ZSH_ABBR_SESSION_GLOBALS[$1]} )); then
if $opt_dry_run; then
echo "abbr -S -g $abbreviation=${quote}${expansion}${quote}"
Expand All @@ -534,7 +541,7 @@ _zsh_abbr() {
success=true
fi
else
if $opt_global; then
if $opt_type_global; then
source "${TMPDIR:-/tmp}/zsh-user-global-abbreviations"

if ! (( ${+ZSH_ABBR_USER_GLOBALS[$1]} )); then
Expand Down Expand Up @@ -569,7 +576,7 @@ _zsh_abbr() {
util_alias() {
for abbreviation expansion in ${(kv)${(P)1}}; do
alias_definition="alias "
if [[ $opt_global == true ]]; then
if [[ $opt_type_global == true ]]; then
alias_definition+="-g "
fi
alias_definition+="$abbreviation='$expansion'"
Expand Down Expand Up @@ -675,7 +682,9 @@ _zsh_abbr() {
;;
"--global"|\
"-g")
opt_global=true
[ "$type_set" = true ] && util_bad_options
type_set=true
opt_type_global=true
((number_opts++))
;;
"--import-fish")
Expand Down Expand Up @@ -723,6 +732,13 @@ _zsh_abbr() {
opt_list_commands=true
((number_opts++))
;;
"--regular"|\
"-r")
[ "$type_set" = true ] && util_bad_options
type_set=true
opt_type_regular=true
((number_opts++))
;;
"--rename"|\
"-R")
[ "$action_set" = true ] && util_bad_options
Expand Down

0 comments on commit a5cab16

Please sign in to comment.