Skip to content

Commit

Permalink
feat(import fish): new option + documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed Mar 1, 2020
1 parent 055e378 commit 6d674bb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
38 changes: 34 additions & 4 deletions README.md
Expand Up @@ -137,16 +137,17 @@ Default is user.
### Options

```shell
[(--add [(--global | -g)] | -a [(--global | -g)] ) arg
[(--add | -a ) [(--global | -g)] arg
| (--clear-session | -c)
| (--erase [(--global | -g)] | -e [(--global | -g)]) arg
| (--erase | -e ) [(--global | -g)] arg
| (--expand | -x) arg
| (--git-populate | -i)
| --import-fish [(--global | -g)] arg
| (--help | -h)
| (--list | -l)
| (--output-aliases | -o)
| (--populate | -p)
| (--rename [(--global | -g)] | -r [(--global | -g)]) args
| (--rename | -r ) [(--global | -g)] args
| (--show | -s)
]
```
Expand Down Expand Up @@ -274,6 +275,35 @@ This command is useful for migrating from aliases to abbreviations.

Note for users migrating from Oh-My-Zsh: [OMZ's Git aliases are shell aliases](https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/git/git.plugin.zsh), not aliases in the Git config. To add abbreviations for them, use **Populate**.

### Import Fish

```shell
abbr --import-fish FILE
```

Import fish abbr-syntax abbreviations (`abbreviation expansion` as compared to zsh abbr's `abbreviation=expansion`).

To migrate from fish:

```shell
fish
abbr -s > file/to/save/fish/abbreviations/to
zsh
abbr [(--global|-g)] [SCOPE] --import-fish file/to/save/fish/abbreviations/to
# file is no longer needed, so feel free to
# rm file/to/save/fish/abbreviations/to
```

To migrate from zsh-abbr < 3:

```shell
zsh
abbr [(--global|-g)] [SCOPE] ${HOME}/.config/zsh/universal-abbreviations
# zsh-abbr > 2 no longer uses that file
# If not customizing `ZSH_USER_ABBREVIATIONS_PATH=${HOME}/.config/zsh/universal-abbreviations` feel free to
# rm ${HOME}/.config/zsh/universal-abbreviations
```

#### List

```shell
Expand Down Expand Up @@ -405,7 +435,7 @@ When zsh-abbr updates the user abbreviations storage file, global user abbreviat

_It is possible for direct edits to the storage file to be lost_ if you make a change to the user abbreviations in a session that opened before the manual change was made. Run `source ~/.zshrc` in all open sessions after directly editing the user abbreviations storage file.

The user abbreviations storage file's default location is `${HOME}/.config/zsh/user-abbreviations`. Customize this by setting the `ZSH_USER_ABBREVIATIONS_PATH` variable in your `.zshrc` before loading zsh-abbr.
The user abbreviations storage file's default location is `${HOME}/.config/zsh/abbreviations`. Customize this by setting the `ZSH_USER_ABBREVIATIONS_PATH` variable in your `.zshrc` before loading zsh-abbr.

```shell
% cat ~/.zshrc
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Expand Up @@ -38,7 +38,7 @@ Zsh `abbr`, not the zsh port of fish `abbr`
- [x] in user file, no `--`
- [x] `add` syntax is `abbreviation='word'`/`abbreviation="word"`
- [x] confirm that quotes can be included by escaping them (`abbreviation="the \"full\" \'word\'"`)
- [ ] support importing from fish / migrating from <3.x
- [x] support importing from fish / migrating from <3.x

### Other

Expand Down
44 changes: 40 additions & 4 deletions zsh-abbr.zsh
Expand Up @@ -21,10 +21,10 @@ ZSH_ABBR_USER_PATH="${ZSH_ABBR_USER_PATH="${HOME}/.config/zsh/abbreviations"}"
_zsh_abbr() {
{
local action_set number_opts opt opt_add opt_clear_session opt_erase \
opt_expand opt_git_populate opt_global opt_session opt_list \
opt_output_aliases opt_populate opt_rename opt_show opt_user \
opt_print_version release_date scope_set should_exit text_bold \
text_reset util_usage version
opt_expand opt_git_populate opt_global opt_import_fish \
opt_session opt_list opt_output_aliases opt_populate opt_rename \
opt_show opt_user opt_print_version release_date scope_set \
should_exit text_bold text_reset util_usage version
action_set=false
number_opts=0
opt_add=false
Expand All @@ -33,6 +33,7 @@ _zsh_abbr() {
opt_expand=false
opt_git_populate=false
opt_global=false
opt_import_fish=false
opt_session=false
opt_list=false
opt_output_aliases=false
Expand Down Expand Up @@ -94,6 +95,8 @@ _zsh_abbr() {
o --help or -h Show this documentation.
o --import-fish FILE Import from fish shell or zsh-abbr < 3.
o --list -l Lists all ABBREVIATIONs.
o --output-aliases [-g] [DESTINATION_FILE] or -o [-g] [DESTINATION_FILE]
Expand Down Expand Up @@ -320,6 +323,31 @@ _zsh_abbr() {
done
}

function import_fish() {
local abbreviation
local expansion
local input_file

if [ $# -ne 1 ]; then
printf "expand requires exactly one argument\\n"
return
fi

input_file=$1

while read -r line; do
def=${line#* -- }
abbreviation=${def%% *}
expansion=${def#* }

if [[ ${expansion:0:1} == ${expansion: -1} && ${expansion:0:1} == [\'\"] ]]; then
expansion=${expansion:1:-1}
fi

util_add $abbreviation $expansion
done < $input_file
}

function list() {
if [ $# -gt 0 ]; then
util_error " list: Unexpected argument"
Expand Down Expand Up @@ -606,6 +634,12 @@ _zsh_abbr() {
opt_global=true
((number_opts++))
;;
"--import-fish")
[ "$action_set" = true ] && util_bad_options
action_set=true
opt_import_fish=true
((number_opts++))
;;
"--help"|\
"-h")
util_usage
Expand Down Expand Up @@ -699,6 +733,8 @@ _zsh_abbr() {
erase "$@"
elif $opt_expand; then
expand "$@"
elif $opt_import_fish; then
import_fish "$@"
elif $opt_git_populate; then
git_populate "$@"
elif $opt_list; then
Expand Down

0 comments on commit 6d674bb

Please sign in to comment.