├── .github ├── ISSUE_TEMPLATE │ ├── ask_question.md │ ├── bug_report.md │ └── feature_request.md ├── pull_request_template.md └── workflows │ └── ci.yml ├── .pre-commit-config.yaml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── conf.d └── abbr_tips.fish ├── functions ├── __abbr_tips_bind_newline.fish ├── __abbr_tips_bind_space.fish ├── __abbr_tips_clean.fish └── __abbr_tips_init.fish └── tests ├── common.fish ├── test_abbr.fish ├── test_alias.fish ├── test_plugin_init.fish ├── test_plugin_update.fish └── test_tips.fish /.github/ISSUE_TEMPLATE/ask_question.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Question 3 | about: Ask a question. 4 | title: "" 5 | labels: question 6 | assignees: "" 7 | --- 8 | 9 | **Question** 10 | 11 | What is your question? 12 | 13 | **Considerations** 14 | 15 | Are there any other considerations? 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "" 5 | labels: bug 6 | assignees: Gazorby 7 | --- 8 | 9 | **Describe the bug** 10 | A clear and concise description of what the bug is. 11 | 12 | **To Reproduce** 13 | Steps to reproduce the behavior: 14 | 15 | **Expected behavior** 16 | A clear and concise description of what you expected to happen. 17 | 18 | **Screenshots** 19 | If applicable, add screenshots to help explain your problem. 20 | 21 | **System (please complete the following information):** 22 | 23 | - fish version 24 | - Distribution `cat /etc/os-release` 25 | 26 | **Additional context** 27 | Add any other context about the problem here. 28 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: "" 5 | labels: enhancement 6 | assignees: "" 7 | --- 8 | 9 | **Is your feature request related to a problem? Please describe.** 10 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 11 | 12 | **Describe the solution you'd like** 13 | A clear and concise description of what you want to happen. 14 | 15 | **Describe alternatives you've considered** 16 | A clear and concise description of any alternative solutions or features you've considered. 17 | 18 | **Additional context** 19 | Add any other context or screenshots about the feature request here. 20 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | 3 | ## Related issues 4 | 5 | ## Notes 6 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | 8 | # Allows you to run this workflow manually from the Actions tab 9 | workflow_dispatch: 10 | 11 | jobs: 12 | tests: 13 | runs-on: ${{ matrix.os }} 14 | strategy: 15 | matrix: 16 | os: [macos-latest, ubuntu-latest] 17 | 18 | steps: 19 | - uses: actions/checkout@v3 20 | - uses: fish-actions/install-fish@v1.1.0 21 | 22 | - name: Install abbreviation-tips with Fisher 23 | uses: fish-shop/install-plugin@v1 24 | with: 25 | plugin-manager: fisher 26 | plugins: gazorby/fish-abbreviation-tips 27 | 28 | - name: Run Fishtape tests 29 | uses: fish-shop/run-fishtape-tests@v1 30 | with: 31 | pattern: tests/**.fish 32 | 33 | syntax-check: 34 | runs-on: ubuntu-latest 35 | 36 | steps: 37 | - uses: actions/checkout@v3 38 | 39 | - uses: fish-actions/install-fish@v1 40 | 41 | - uses: fish-actions/syntax-check@v1 42 | 43 | # Check Fish format 44 | format-check: 45 | runs-on: ubuntu-latest 46 | 47 | steps: 48 | - uses: actions/checkout@v3 49 | 50 | - uses: fish-actions/install-fish@v1 51 | 52 | - uses: fish-actions/format-check@v1 53 | 54 | # Check Markdown and Yaml format 55 | prettier: 56 | runs-on: ubuntu-latest 57 | steps: 58 | - uses: actions/checkout@v3 59 | - uses: actionsx/prettier@v2 60 | with: 61 | args: --check . 62 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v4.3.0 4 | hooks: 5 | - id: check-toml 6 | - id: mixed-line-ending 7 | - id: check-case-conflict 8 | - id: check-merge-conflict 9 | - id: check-docstring-first 10 | - id: check-added-large-files 11 | - id: check-executables-have-shebangs 12 | - id: debug-statements 13 | - id: end-of-file-fixer 14 | - id: fix-byte-order-marker 15 | - id: trailing-whitespace 16 | 17 | - repo: https://github.com/hugoh/pre-commit-fish.git 18 | rev: v1.2 19 | hooks: 20 | - id: fish_syntax 21 | - id: fish_indent 22 | 23 | - repo: https://github.com/pre-commit/mirrors-prettier 24 | rev: "v2.7.1" 25 | hooks: 26 | - id: prettier 27 | types: [markdown] 28 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.5.1 (2020-05-12) 2 | 3 | - ♻️ ref: prefix with '\_\_' for private scope 4 | - 🐛 fix(regex): don't treat '-' as a word separator 5 | 6 | ## 0.5.0 (2020-05-12) 7 | 8 | - ✨ feat: customize regexes 9 | - ♻️ ref: use verbose conditional operators 10 | 11 | ## 0.4.0 (2020-04-20) 12 | 13 | - ⚡ perf: update abbreviations list incrementally 14 | 15 | # [0.3.1](https://github.com/Gazorby/abbr-tips/compare/v0.3.0...v0.3.1) (2020-04-17) 16 | 17 | ### 🐛 Bug Fixes 18 | 19 | - fix: test variable existence instead of value ([f33cf23](https://github.com/Gazorby/abbr-tips/commit/f33cf23)) 20 | - fix: update function to erase on uninstall ([ba239d1](https://github.com/Gazorby/abbr-tips/commit/ba239d1)) 21 | 22 | # [0.3.0](https://github.com/Gazorby/abbr-tips/compare/v0.2.0...v0.3.0) (2020-04-16) 23 | 24 | ### ♻️ Refactor 25 | 26 | - refactor: remove flock dependency ([376072d](https://github.com/Gazorby/abbr-tips/commit/376072d)) 27 | 28 | # [0.2.0](https://github.com/Gazorby/abbr-tips/compare/v0.1.0...v0.2.0) (2020-04-13) 29 | 30 | ### 🐛 Bug Fixes 31 | 32 | - fix: don't use universal scope by default ([832691a](https://github.com/Gazorby/abbr-tips/commit/832691a)) 33 | - fix: erase functions on uninstall ([cd1a75a](https://github.com/Gazorby/abbr-tips/commit/cd1a75a)) 34 | - fix: fix uninstall function ([b58971f](https://github.com/Gazorby/abbr-tips/commit/b58971f)) 35 | - fix: still need univresal scope for abbr lists ([6a700a0](https://github.com/Gazorby/abbr-tips/commit/6a700a0)) 36 | 37 | # [0.1.0](https://github.com/Gazorby/abbr-tips/compare/f5ab8ed...v0.1.0) (2020-04-13) 38 | 39 | ### ✨ Features 40 | 41 | - feat: customize tips prompt ([ae29fca](https://github.com/Gazorby/abbr-tips/commit/ae29fca)) 42 | - feat: customize tips update mode ([4681009](https://github.com/Gazorby/abbr-tips/commit/4681009)) 43 | - feat: don't show tips if an abbr was used ([5f2e6ac](https://github.com/Gazorby/abbr-tips/commit/5f2e6ac)) 44 | - feat: prettier tips ([f5ab8ed](https://github.com/Gazorby/abbr-tips/commit/f5ab8ed)) 45 | 46 | ### 🐛 Bug Fixes 47 | 48 | - fix: check for abbr status in on whitespace input ([421ede5](https://github.com/Gazorby/abbr-tips/commit/421ede5)) 49 | - fix: delete keybindings when uninstalling ([11ce0ae](https://github.com/Gazorby/abbr-tips/commit/11ce0ae)) 50 | - fix: don't show tips if abbr was used and options added ([a2b5214](https://github.com/Gazorby/abbr-tips/commit/a2b5214)) 51 | - fix: prompt variable not properly set ([1cc43e0](https://github.com/Gazorby/abbr-tips/commit/1cc43e0)) 52 | - fix: properly resetting abbr status ([59e90e2](https://github.com/Gazorby/abbr-tips/commit/59e90e2)) 53 | - fix: reset abbr status after displaying tip ([46c70c1](https://github.com/Gazorby/abbr-tips/commit/46c70c1)) 54 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to abbr-tips 2 | 3 | 👍🎉 First off, thanks for taking the time to contribute! 🎉👍 4 | 5 | Please open an issue and discuss the problem / feature before starting to code. 6 | 7 | ## Code style 8 | 9 | - Keep the style consistent with existent code. 10 | - Stay with pure fish (use builtins) 11 | - Avoid dependencies 12 | 13 | ## Git Commit Messages 14 | 15 | - Follow [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0-beta.2/#specification) 16 | - If you like emojis, follow [gitmoji guide](https://gitmoji.carloscuesta.me/) 17 | 18 | ## Test 19 | 20 | You can use (and enhance!) scripts in `test` folder to test your code. You need to run the tests with [fishtape](https://github.com/jorgebucaran/fishtape) 21 | 22 | ## IDE 23 | 24 | If you are using Visual Studio Code, i recommend these extensions : 25 | 26 | - [fish-vscode](https://marketplace.visualstudio.com/items?itemName=skyapps.fish-vscode) for syntax highlighting 27 | - [fish-ide](https://marketplace.visualstudio.com/items?itemName=lunaryorn.fish-ide) for syntax errors checking 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Matthieu MN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fish-abbreviation-tips [![Generic badge](https://img.shields.io/badge/Version-v0.5.1-.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/fish->=3.1.0-red.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://shields.io/) 2 | 3 | [![asciicast](https://asciinema.org/a/322043.svg)](https://asciinema.org/a/322043) 4 | 5 | It helps you remember abbreviations and aliases by displaying tips when you can use them. 6 | 7 | ## ✅ Requirements 8 | 9 | - [fish](https://github.com/fish-shell/fish-shell) 3.1.0+ 10 | 11 | ## 🚀 Install 12 | 13 | Install using [Fisher](https://github.com/jorgebucaran/fisher): 14 | 15 | ```fish 16 | fisher install gazorby/fish-abbreviation-tips 17 | ``` 18 | 19 | ## 🔧 Usage 20 | 21 | Just use your shell normally and enjoy tips! 22 | 23 | ### Adding / removing abbreviations or aliases 24 | 25 | The plugin automatically track changes when adding/removing abbreviations or aliases using `abbr` or `functions` commands (see [behind the scenes](#-behind-the-scenes)), so you won't see tips anymore for an abbreviation/alias that has been erased, and you will get new ones for newly added abbreviations/aliases 26 | 27 | ## 🛠 Configuration 28 | 29 | Configuration is done through environment variables. 30 | 31 | To avoid spamming your `config.fish`, you can set environment variables using `set -U` once to make them persistent across restarts and share them across fish's instances. 32 | 33 | ### Default configuration 34 | 35 | ```fish 36 | ABBR_TIPS_PROMPT "\n💡 \e[1m{{ .abbr }}\e[0m => {{ .cmd }}" 37 | ABBR_TIPS_ALIAS_WHITELIST # Not set 38 | 39 | ABBR_TIPS_REGEXES '(^(\w+\s+)+(-{1,2})\w+)(\s\S+)' '(^( ?\w+){3}).*' '(^( ?\w+){2}).*' '(^( ?\w+){1}).*' 40 | # 1 : Test command with arguments removed 41 | # 2 : Test the firsts three words 42 | # 3 : Test the firsts two words 43 | # 4 : Test the first word 44 | ``` 45 | 46 | ### Tips prompt 47 | 48 | `ABBR_TIPS_PROMPT` 49 | 50 | By default, tips will show up like this : 51 | 52 | ```console 53 | 💡 ga => git add 54 | ``` 55 | 56 | But you can customize it using the prompt environment variable. The plugin will replace `{{ .abbr }}` with the abbreviation/alias and `{{ .cmd }}` with the corresponding command. 57 | 58 | ⚠️ tips are displayed using `echo -e` (interpretation of backslash escapes) 59 | 60 | ### Alias whitelist 61 | 62 | `ABBR_TIPS_ALIAS_WHITELIST` 63 | 64 | By default, if the command is a user-defined function (alias), the plugin won't search for a matching abbreviation because your aliases names are likely to be uniques, so there wouldn't be abbreviations with the same names. 65 | 66 | But, in some cases, you may write aliases that wrap existing commands without altering their actual execution (e.g., add some hooks before/after the command execution). In this special case, you may also have abbreviations using these aliases, so you don't want to ignore them. 67 | 68 | To do that, add these aliases to the environment variable. 69 | 70 | ### Regexes 71 | 72 | `ABBR_TIPS_REGEXES` 73 | 74 | If the command doesn't match an abbreviation/alias exactly, it is tested against some regexes to extract a possible abbreviation/alias. 75 | 76 | For example, you could have an abbreviation/alias like this : 77 | 78 | ```console 79 | gcm => git commit -m 80 | ``` 81 | 82 | So you want a tip when typing `git commit -m "my commit"`, but the command doesn't match exactly `git commit -m`. 83 | To tackle this, we have a default regex that will match commands with arguments removed, so your `git commit -m "my commit"` will be tested as `git commit -m`. 84 | 85 | You can add such regexes to the `ABBR_TIPS_REGEXES` list, and they will be tested in the order in which they have been added (see [default configuration](#default-configuration)). Matching is lazy, so if the string extracted with the first regex matches an abbreviation/alias, it won't go further. Remember that only the _first matching group_ will be tested. (so you must have at least one per regex) 86 | 87 | ## 🎥 Behind the scenes 88 | 89 | In order to not slow down your prompt, the plugin store abbreviations/aliases and their corresponding commands in lists (actually simulating a dictionary, as [fish doesn't support dict yet](https://github.com/fish-shell/fish-shell/issues/390)) to avoid iterating over all abbreviations/aliases each time you type a command. So retrieving an abbreviation or an alias from a command is fast as it doesn't involve any loop. 90 | 91 | The plugin will create lists once during installation by calling `__abbr_tips_init` in the background (more precisely in spawned shell, because [fish doesn't put functions in background](https://github.com/fish-shell/fish-shell/issues/238)). Then, lists will get updated when you add or remove abbreviation/alias using `abbr` or `functions` builtin. 92 | 93 | ## 💭 Inspiration 94 | 95 | Inspired by [zsh-fast-alias-tips](https://github.com/sei40kr/zsh-fast-alias-tips) and [alias-tips](https://github.com/djui/alias-tips) zsh plugins 96 | 97 | ## 📝 License 98 | 99 | [MIT](https://github.com/Gazorby/fish-abbreviation-tips/blob/master/LICENSE) 100 | -------------------------------------------------------------------------------- /conf.d/abbr_tips.fish: -------------------------------------------------------------------------------- 1 | for mode in default insert 2 | bind --mode $mode " " __abbr_tips_bind_space 3 | bind --mode $mode \n __abbr_tips_bind_newline 4 | bind --mode $mode \r __abbr_tips_bind_newline 5 | end 6 | 7 | set -g __abbr_tips_used 0 8 | 9 | # Trim simple/double quotes from args 10 | function trim_value 11 | echo "$argv" | string trim --left --right --chars '"\'' | string join ' ' 12 | end 13 | 14 | function __abbr_tips_install --on-event abbr_tips_install 15 | # Regexes used to find abbreviation inside command 16 | # Only the first matching group will be tested as an abbr 17 | set -Ux ABBR_TIPS_REGEXES 18 | set -a ABBR_TIPS_REGEXES '(^(\w+\s+)+(-{1,2})\w+)(\s\S+)' 19 | set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){3}).*' 20 | set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){2}).*' 21 | set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){1}).*' 22 | 23 | set -Ux ABBR_TIPS_PROMPT "\n💡 \e[1m{{ .abbr }}\e[0m => {{ .cmd }}" 24 | set -gx ABBR_TIPS_AUTO_UPDATE background 25 | 26 | __abbr_tips_init 27 | end 28 | 29 | function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the current command" 30 | set -l command (string split ' ' -- "$argv") 31 | set -l cmd (string replace -r -a '\\s+' ' ' -- "$argv" ) 32 | 33 | # Update abbreviations lists when adding/removing abbreviations 34 | if test "$command[1]" = abbr 35 | # Parse args as abbr options 36 | argparse --name abbr --ignore-unknown a/add e/erase g/global U/universal -- $command 37 | 38 | if set -q _flag_a 39 | and not contains -- "$argv[2]" $__ABBR_TIPS_KEYS 40 | set -a __ABBR_TIPS_KEYS "$argv[2]" 41 | set -a __ABBR_TIPS_VALUES (trim_value "$argv[3..-1]") 42 | else if set -q _flag_e 43 | and set -l abb (contains -i -- "$argv[2]" $__ABBR_TIPS_KEYS) 44 | set -e __ABBR_TIPS_KEYS[$abb] 45 | set -e __ABBR_TIPS_VALUES[$abb] 46 | end 47 | else if test "$command[1]" = alias 48 | # Update abbreviations list when adding aliases 49 | set -l alias_key 50 | set -l alias_value 51 | 52 | # Parse args as `alias` options 53 | argparse --name alias --ignore-unknown s/save -- $command 54 | 55 | if string match -q '*=*' -- "$argv[2]" 56 | set command_split (string split '=' -- $argv[2]) 57 | set alias_key "a__$command_split[1]" 58 | set alias_value $command_split[2..-1] 59 | else 60 | set alias_key "a__$argv[2]" 61 | set alias_value $argv[3..-1] 62 | end 63 | 64 | set alias_value (trim_value "$alias_value") 65 | 66 | if set -l abb (contains -i -- "$argv[3..-1]" $__ABBR_TIPS_KEYS) 67 | set __ABBR_TIPS_KEYS[$abb] $alias_key 68 | set __ABBR_TIPS_VALUES[$abb] $alias_value 69 | else 70 | set -a __ABBR_TIPS_KEYS $alias_key 71 | set -a __ABBR_TIPS_VALUES $alias_value 72 | end 73 | else if test "$command[1]" = functions 74 | # Parse args as `functions` options 75 | argparse --name functions e/erase -- $command 76 | 77 | # Update abbreviations list when removing aliases 78 | if set -q _flag_e 79 | and set -l abb (contains -i -- a__{$argv[2]} $__ABBR_TIPS_KEYS) 80 | set -e __ABBR_TIPS_KEYS[$abb] 81 | set -e __ABBR_TIPS_VALUES[$abb] 82 | end 83 | end 84 | 85 | # Exit in the following cases : 86 | # - abbreviation has been used 87 | # - command is already an abbreviation 88 | # - command not found 89 | # - or it's a function (alias) 90 | if test $__abbr_tips_used = 1 91 | set -g __abbr_tips_used 0 92 | return 93 | else if abbr -q "$cmd" 94 | or not type -q "$command[1]" 95 | return 96 | else if string match -q -- "alias $cmd *" (alias) 97 | return 98 | else if test (type -t "$command[1]") = function 99 | and count $ABBR_TIPS_ALIAS_WHITELIST >/dev/null 100 | and not contains "$command[1]" $ABBR_TIPS_ALIAS_WHITELIST 101 | return 102 | end 103 | 104 | set -l abb 105 | if not set abb (contains -i -- "$cmd" $__ABBR_TIPS_VALUES) 106 | for r in $ABBR_TIPS_REGEXES 107 | if set abb (contains -i -- (string replace -r -a -- "$r" '$1' "$cmd") $__ABBR_TIPS_VALUES) 108 | break 109 | end 110 | end 111 | end 112 | 113 | if test -n "$abb" 114 | if string match -q "a__*" -- "$__ABBR_TIPS_KEYS[$abb]" 115 | set -l alias (string sub -s 4 -- "$__ABBR_TIPS_KEYS[$abb]") 116 | if functions -q "$alias" 117 | echo -e (string replace -a '{{ .cmd }}' -- "$__ABBR_TIPS_VALUES[$abb]" \ 118 | (string replace -a '{{ .abbr }}' -- "$alias" "$ABBR_TIPS_PROMPT")) 119 | else 120 | set -e __ABBR_TIPS_KEYS[$abb] 121 | set -e __ABBR_TIPS_VALUES[$abb] 122 | end 123 | else 124 | echo -e (string replace -a '{{ .cmd }}' -- "$__ABBR_TIPS_VALUES[$abb]" \ 125 | (string replace -a '{{ .abbr }}' -- "$__ABBR_TIPS_KEYS[$abb]" "$ABBR_TIPS_PROMPT")) 126 | end 127 | end 128 | 129 | return 130 | end 131 | 132 | function __abbr_tips_update --on-event abbr_tips_update 133 | __abbr_tips_clean 134 | __abbr_tips_install 135 | end 136 | 137 | function __abbr_tips_uninstall --on-event abbr_tips_uninstall 138 | __abbr_tips_clean 139 | functions --erase __abbr_tips_init 140 | end 141 | -------------------------------------------------------------------------------- /functions/__abbr_tips_bind_newline.fish: -------------------------------------------------------------------------------- 1 | function __abbr_tips_bind_newline 2 | if test $__abbr_tips_used != 1 3 | if abbr -q -- (string trim -- (commandline)) 4 | set -g __abbr_tips_used 1 5 | else 6 | set -g __abbr_tips_used 0 7 | end 8 | end 9 | commandline -f execute 10 | end 11 | -------------------------------------------------------------------------------- /functions/__abbr_tips_bind_space.fish: -------------------------------------------------------------------------------- 1 | function __abbr_tips_bind_space 2 | commandline -i " " 3 | if test $__abbr_tips_used != 1 4 | if abbr -q -- (string trim -- (commandline)) 5 | set -g __abbr_tips_used 1 6 | else 7 | set -g __abbr_tips_used 0 8 | end 9 | end 10 | commandline -f expand-abbr 11 | end 12 | -------------------------------------------------------------------------------- /functions/__abbr_tips_clean.fish: -------------------------------------------------------------------------------- 1 | function __abbr_tips_clean -d "Clean plugin variables and functions" 2 | bind --erase \n 3 | bind --erase \r 4 | bind --erase " " 5 | set --erase __abbr_tips_used 6 | set --erase __abbr_tips_run_once 7 | set --erase __ABBR_TIPS_VALUES 8 | set --erase __ABBR_TIPS_KEYS 9 | set --erase ABBR_TIPS_PROMPT 10 | set --erase ABBR_TIPS_AUTO_UPDATE 11 | set --erase ABBR_TIPS_ALIAS_WHITELIST 12 | set --erase ABBR_TIPS_REGEXES 13 | functions --erase __abbr_tips_bind_newline 14 | functions --erase __abbr_tips_bind_space 15 | functions --erase __abbr_tips 16 | end 17 | -------------------------------------------------------------------------------- /functions/__abbr_tips_init.fish: -------------------------------------------------------------------------------- 1 | function __abbr_tips_init -d "Initialize abbreviations variables for fish-abbr-tips" 2 | set -e __ABBR_TIPS_KEYS 3 | set -e __ABBR_TIPS_VALUES 4 | set -Ux __ABBR_TIPS_KEYS 5 | set -Ux __ABBR_TIPS_VALUES 6 | 7 | set -l i 1 8 | set -l abb (string replace -r '.*-- ' '' -- (abbr -s)) 9 | while test $i -le (count $abb) 10 | set -l current_abb (string split -m1 -- ' ' "$abb[$i]") 11 | set -a __ABBR_TIPS_KEYS "$current_abb[1]" 12 | set -a __ABBR_TIPS_VALUES (string trim -c '\'' -- "$current_abb[2]") 13 | set i (math $i + 1) 14 | end 15 | 16 | set -l i 1 17 | set -l abb (string replace -r '.*-- ' '' -- (alias -s)) 18 | while test $i -le (count $abb) 19 | set -l current_abb (string split -m2 -- ' ' "$abb[$i]") 20 | set -a __ABBR_TIPS_KEYS "a__$current_abb[2]" 21 | set -a __ABBR_TIPS_VALUES (string trim -c '\'' -- "$current_abb[3]") 22 | set i (math $i + 1) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /tests/common.fish: -------------------------------------------------------------------------------- 1 | # Use with jorgebucaran/fishtape to run tests 2 | # i.e. fishtape test/fish-abbreviation-tips.fish 3 | 4 | function setup 5 | # Source plugin 6 | source functions/__abbr_tips_init.fish 7 | source functions/__abbr_tips_clean.fish 8 | source conf.d/abbr_tips.fish 9 | source functions/__abbr_tips_bind_newline.fish 10 | source functions/__abbr_tips_bind_space.fish 11 | # Backup settings variables 12 | set -g tmp_keys $__ABBR_TIPS_KEYS 13 | set -g tmp_values $__ABBR_TIPS_VALUES 14 | set -g tmp_tips_prompt $ABBR_TIPS_PROMPT 15 | set -g ABBR_TIPS_PROMPT "{{ .abbr }} => {{ .cmd }}" 16 | end 17 | 18 | function teardown 19 | # Restore variables 20 | set __ABBR_TIPS_KEYS $tmp_keys 21 | set __ABBR_TIPS_VALUES $tmp_values 22 | set ABBR_TIPS_PROMPT "$tmp_tips_prompt" 23 | end 24 | 25 | function clear_test_var 26 | # Clear variables to prevent the results 27 | # of each unit test from affecting each other 28 | set -g __ABBR_TIPS_KEYS 29 | set -g __ABBR_TIPS_VALUES 30 | abbr -e __abbr_test 31 | abbr -e __abbr_test_one 32 | abbr -e __abbr_test_two 33 | end 34 | -------------------------------------------------------------------------------- /tests/test_abbr.fish: -------------------------------------------------------------------------------- 1 | source (string join "/" (dirname (status --current-filename)) "common.fish") 2 | 3 | setup 4 | 5 | # Add abbreviation 6 | @test "add abbreviation tip key" ( 7 | clear_test_var 8 | __abbr_tips 'abbr -a __abbr_test grep -q' 9 | contains "__abbr_test" $__ABBR_TIPS_KEYS 10 | ) "$status" = 0 11 | 12 | @test "add abbreviation tip value" ( 13 | clear_test_var 14 | __abbr_tips 'abbr -a __abbr_test grep -q' 15 | contains "grep -q" $__ABBR_TIPS_VALUES 16 | ) "$status" = 0 17 | 18 | @test "add abbreviation tip key with simple quotes" ( 19 | clear_test_var 20 | __abbr_tips 'abbr -a __abbr_test \'grep -q\'' 21 | contains "__abbr_test" $__ABBR_TIPS_KEYS 22 | ) "$status" = 0 23 | 24 | @test "add abbreviation tip value with simple quotes" ( 25 | clear_test_var 26 | __abbr_tips 'abbr -a __abbr_test \'grep -q\'' 27 | contains "grep -q" $__ABBR_TIPS_VALUES 28 | ) "$status" = 0 29 | 30 | @test "add abbreviation tip key with double quotes" ( 31 | clear_test_var 32 | __abbr_tips 'abbr -a __abbr_test "grep -q"' 33 | contains "__abbr_test" $__ABBR_TIPS_KEYS 34 | ) "$status" = 0 35 | 36 | @test "add abbreviation tip value with double quotes" ( 37 | clear_test_var 38 | __abbr_tips 'abbr -a __abbr_test "grep -q"' 39 | contains "grep -q" $__ABBR_TIPS_VALUES 40 | ) "$status" = 0 41 | 42 | 43 | # Remove abbreviation 44 | @test "remove abbreviation tip key" ( 45 | clear_test_var 46 | __abbr_tips 'abbr -a __abbr_test ps' 47 | __abbr_tips 'abbr -e __abbr_test' 48 | not contains "__abbr_test" $__ABBR_TIPS_KEYS 49 | ) "$status" = 0 50 | 51 | @test "remove abbreviation tip value" ( 52 | clear_test_var 53 | __abbr_tips 'abbr -a __abbr_test ps' 54 | __abbr_tips 'abbr -e __abbr_test' 55 | not contains "ps" $__ABBR_TIPS_VALUES 56 | ) "$status" = 0 57 | 58 | 59 | teardown 60 | -------------------------------------------------------------------------------- /tests/test_alias.fish: -------------------------------------------------------------------------------- 1 | source (string join "/" (dirname (status --current-filename)) "common.fish") 2 | 3 | setup 4 | 5 | # Add alias 6 | @test "add alias tip key simple quotes" ( 7 | clear_test_var 8 | __abbr_tips 'alias __abbr_test_alias \'grep -q\'' 9 | contains "a____abbr_test_alias" $__ABBR_TIPS_KEYS 10 | ) "$status" = 0 11 | 12 | @test "add alias tip value simple quotes" ( 13 | clear_test_var 14 | __abbr_tips 'alias __abbr_test_alias \'grep -q\'' 15 | contains "grep -q" $__ABBR_TIPS_VALUES 16 | ) "$status" = 0 17 | 18 | @test "add alias tip key double quotes" ( 19 | clear_test_var 20 | __abbr_tips 'alias __abbr_test_alias "grep -q"' 21 | contains "a____abbr_test_alias" $__ABBR_TIPS_KEYS 22 | ) "$status" = 0 23 | 24 | @test "add alias tip value double quotes" ( 25 | clear_test_var 26 | __abbr_tips 'alias __abbr_test_alias "grep -q"' 27 | contains "grep -q" $__ABBR_TIPS_VALUES 28 | ) "$status" = 0 29 | 30 | @test "add alias tip value with =" ( 31 | clear_test_var 32 | __abbr_tips 'alias __abbr_test_alias=grep' 33 | contains "grep" $__ABBR_TIPS_VALUES 34 | ) "$status" = 0 35 | 36 | @test "add alias tip key with =" ( 37 | clear_test_var 38 | __abbr_tips 'alias __abbr_test_alias=grep' 39 | contains "a____abbr_test_alias" $__ABBR_TIPS_KEYS 40 | ) "$status" = 0 41 | 42 | @test "add alias tip value with = and quotes" ( 43 | clear_test_var 44 | __abbr_tips 'alias __abbr_test_alias="grep "' 45 | contains "grep" $__ABBR_TIPS_VALUES 46 | ) "$status" = 0 47 | 48 | @test "add alias tip key with = and quotes" ( 49 | clear_test_var 50 | __abbr_tips 'alias __abbr_test_alias="grep "' 51 | contains "a____abbr_test_alias" $__ABBR_TIPS_KEYS 52 | ) "$status" = 0 53 | 54 | # Remove alias 55 | @test "remove alias tip key" ( 56 | clear_test_var 57 | __abbr_tips 'alias __abbr_test_alias "grep -q"' 58 | __abbr_tips 'functions --erase __abbr_test_alias' 59 | not contains "a____abbr_test_alias" $__ABBR_TIPS_KEYS 60 | ) "$status" = 0 61 | 62 | @test "remove alias tip value" ( 63 | clear_test_var 64 | __abbr_tips 'alias __abbr_test_alias "grep -q"' 65 | __abbr_tips 'functions --erase __abbr_test_alias' 66 | not contains "grep -q" $__ABBR_TIPS_VALUES 67 | ) "$status" = 0 68 | 69 | 70 | teardown 71 | -------------------------------------------------------------------------------- /tests/test_plugin_init.fish: -------------------------------------------------------------------------------- 1 | source (string join "/" (dirname (status --current-filename)) "common.fish") 2 | 3 | setup 4 | 5 | # Initialization 6 | @test "initial tip key" ( 7 | clear_test_var 8 | abbr -a __abbr_test ps 9 | set -e __ABBR_TIPS_KEYS 10 | __abbr_tips_init 11 | contains "__abbr_test" $__ABBR_TIPS_KEYS 12 | ) "$status" = 0 13 | 14 | @test "initial tip value" ( 15 | clear_test_var 16 | abbr -a __abbr_test ps 17 | set -e __ABBR_TIPS_VALUES 18 | __abbr_tips_init 19 | contains "ps" $__ABBR_TIPS_VALUES 20 | ) "$status" = 0 21 | 22 | teardown 23 | -------------------------------------------------------------------------------- /tests/test_plugin_update.fish: -------------------------------------------------------------------------------- 1 | source (string join "/" (dirname (status --current-filename)) "common.fish") 2 | 3 | # Test that plugin update reset tip keys 4 | 5 | setup 6 | 7 | @test "plugin update __ABBR_TIPS_KEYS length" ( 8 | set len_keys (count $__ABBR_TIPS_KEYS) 9 | set -a __ABBR_TIPS_KEYS __wrong_key 10 | __abbr_tips_update 11 | test (count $__ABBR_TIPS_KEYS) -eq $len_keys 12 | ) "$status" = 0 13 | 14 | setup 15 | 16 | @test "plugin update __ABBR_TIPS_KEYS value" ( 17 | set last_key "$__ABBR_TIPS_KEYS[-1]" 18 | set -a __ABBR_TIPS_KEYS __wrong_key 19 | __abbr_tips_update 20 | test "$__ABBR_TIPS_KEYS[-1]" = "$last_key" 21 | ) "$status" = 0 22 | 23 | setup 24 | 25 | # Test that plugin update reset tip values 26 | 27 | @test "plugin update __ABBR_TIPS_VALUES length" ( 28 | set len_keys (count $__ABBR_TIPS_VALUES) 29 | set -a __ABBR_TIPS_VALUES __wrong_key 30 | __abbr_tips_update 31 | test (count $__ABBR_TIPS_VALUES) -eq $len_keys 32 | ) "$status" = 0 33 | 34 | setup 35 | 36 | @test "plugin update __ABBR_TIPS_VALUES value" ( 37 | set last_key "$__ABBR_TIPS_VALUES[-1]" 38 | set -a __ABBR_TIPS_VALUES __wrong_key 39 | __abbr_tips_update 40 | test "$__ABBR_TIPS_VALUES[-1]" = "$last_key" 41 | ) "$status" = 0 42 | 43 | 44 | teardown 45 | -------------------------------------------------------------------------------- /tests/test_tips.fish: -------------------------------------------------------------------------------- 1 | source (string join "/" (dirname (status --current-filename)) "common.fish") 2 | 3 | setup 4 | 5 | # Test tip 6 | @test "abbreviation tip match" ( 7 | clear_test_var 8 | __abbr_tips 'abbr -a __abbr_test ps' 9 | echo (__abbr_tips 'ps') 10 | ) = "__abbr_test => ps" 11 | 12 | @test "alias tip match" ( 13 | clear_test_var 14 | alias __abbr_test_alias "grep -q" 15 | __abbr_tips 'alias __abbr_test_alias "grep -q"' 16 | echo (__abbr_tips 'grep -q') 17 | ) = "__abbr_test_alias => grep -q" 18 | 19 | @test "multiple abbreviation tip match" ( 20 | clear_test_var 21 | abbr -a __abbr_test_one ps 22 | abbr -a __abbr_test_two "grep -q" 23 | __abbr_tips_init 24 | echo (__abbr_tips 'grep -q') 25 | ) = "__abbr_test_two => grep -q" 26 | 27 | @test "multiple alias tip match" ( 28 | clear_test_var 29 | alias abbr_test_alias_one ps 30 | alias abbr_test_alias_two "grep -q" 31 | __abbr_tips_init 32 | echo (__abbr_tips 'grep -q') 33 | ) = "abbr_test_alias_two => grep -q" 34 | 35 | teardown 36 | --------------------------------------------------------------------------------