├── LICENSE ├── README.md └── tipz.zsh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 James Dinsdale (molovo.co) 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tipz 2 | 3 | Tipz is a small ZSH plugin, which gives you helpful hints when you execute a command for which you have an alias defined. 4 | 5 | ## Example 6 | 7 | [![asciicast](https://asciinema.org/a/4xlg93jg6dw9z20wn4supy4ru.png)](https://asciinema.org/a/4xlg93jg6dw9z20wn4supy4ru) 8 | 9 | ## Installation 10 | 11 | ### [Zulu](https://github.com/zulu-zsh/zulu) 12 | 13 | ```sh 14 | zulu install tipz 15 | ``` 16 | 17 | ### Manual 18 | 19 | ```sh 20 | git clone https://github.com/molovo/tipz tipz && cd tipz 21 | echo "source $(pwd)/tipz.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc 22 | ``` 23 | 24 | ## Configuration 25 | 26 | You can customize the prefix text if you'd like, by setting `TIPZ_TEXT` 27 | 28 | ```sh 29 | TIPZ_TEXT='💡 ' 30 | ``` 31 | 32 | ## License 33 | 34 | Copyright (c) 2016,2017 James Dinsdale (molovo.co) 35 | 36 | Tipz is licensed under The MIT License (MIT) 37 | 38 | ## Team 39 | 40 | * [James Dinsdale](http://molovo.co) 41 | 42 | ## Related 43 | 44 | * **[alias-tips](https://github.com/djui/alias-tips)** - A Python implementation of the same concept 45 | -------------------------------------------------------------------------------- /tipz.zsh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env zsh 2 | 3 | ### 4 | # Search the defined aliases for a match 5 | ### 6 | function _tipz_find_match() { 7 | local bits alias command result="" 8 | local -a aliases args; args="$@" 9 | 10 | # Load the current aliases into an array 11 | local oldIFS=$IFS 12 | IFS=$'\n' aliases=($(alias)) 13 | IFS=$oldIFS 14 | 15 | # Loop through each of the aliases 16 | for line in "${aliases[@]}"; do 17 | # Split the line on '=' to separate the command 18 | # and its alias 19 | bits=("${(s/=/)line}") 20 | alias=$bits[1] 21 | command=$bits[2] 22 | 23 | # Create a regex that finds an exact match for 24 | # the current argument string 25 | args="${(@)args[@]}" 26 | local pattern=$'^[\'\"]?'${args//([\+\{\}\(\)\[\]\*\?\:\\\.\|])/\\\$1}$'[\'\"]?$' 27 | 28 | # Check if the command matches the regex 29 | if [[ "$command" =~ $pattern ]]; then 30 | # Ensure that the longest matching command is stored 31 | if [[ ${#command} > ${#result} ]]; then 32 | result=$alias 33 | fi 34 | fi 35 | done 36 | 37 | # If a result has been found, output it 38 | if [[ -n $result ]]; then 39 | echo $result 40 | return 0 41 | fi 42 | 43 | return 1 44 | } 45 | 46 | ### 47 | # Search for alias tips for the currently executing command 48 | ### 49 | function _tipz_process { 50 | local -a cmd; cmd=($@) 51 | integer i=$(( ${#cmd} + 1 )) 52 | 53 | # Loop for the length of the argument list, knocking 54 | # an argument from the end of the list each time, and 55 | # then using the remaining arguments to search for aliases 56 | while [[ $i > 0 ]]; do 57 | # Check the current string for a match 58 | result=$(_tipz_find_match "${(@)cmd:0:$i}") 59 | 60 | # If the search exited successfully, 61 | # output the tip to the user 62 | if [[ $? -eq 0 ]]; then 63 | echo "\033[1;34m${TIPZ_TEXT:-'Tipz:'}\033[0;m \033[0;34m$result ${(@)cmd:$i}\033[0;m" 64 | return 0 65 | fi 66 | 67 | # Decrement the counter 68 | i=$(( i - 1 )) 69 | done 70 | 71 | return 1 72 | } 73 | 74 | ### 75 | # A small function to filter out strange arguments 76 | # sent from the add-zsh-hook preexec hook 77 | ### 78 | function _tipz_prexec() { 79 | _tipz_process $(echo $1) 80 | } 81 | 82 | ### 83 | # Register the preexec hook 84 | ### 85 | autoload -Uz add-zsh-hook 86 | add-zsh-hook preexec _tipz_prexec 87 | --------------------------------------------------------------------------------