diff --git a/scripts/cheat-autocompletion.bash b/scripts/cheat-autocompletion.bash index 95f6288..2a847ae 100755 --- a/scripts/cheat-autocompletion.bash +++ b/scripts/cheat-autocompletion.bash @@ -1,9 +1,64 @@ -function _cheat_autocomplete { - sheets=$(cheat -l | sed -n '2,$p'|cut -d' ' -f1) - COMPREPLY=() - if [ $COMP_CWORD = 1 ]; then - COMPREPLY=(`compgen -W "$sheets" -- $2`) - fi +_cheat_cheatpaths() +{ + if [[ -z "${cur// }" ]]; then + compgen -W "$(cheat -d | cut -d':' -f1)" + else + compgen -W "$(cheat -d | cut -d':' -f1 | grep $cur)" + fi } -complete -F _cheat_autocomplete cheat +_cheat_cheatsheets() +{ + if [[ "${CHEAT_USE_FZF:-0}" != 0 ]]; then + FZF_COMPLETION_TRIGGER='' _fzf_complete "--no-multi" "$@" < <(cheat -l | tail -n +2 | cut -d' ' -f1) + else + COMPREPLY=( $(compgen -W "$(cheat -l $cur | tail -n +2 | cut -d' ' -f1)") ) + fi +} + +_cheat_tags() +{ + if [[ -z "${cur// }" ]]; then + compgen -W "$(cheat -T)" + else + compgen -W "$(cheat -T | grep $cur)" + fi +} + +_cheat() +{ + local cur prev words cword split + _init_completion -s || return + + case $prev in + --edit|-e) + _cheat_cheatsheets + ;; + --path|-p) + COMPREPLY=( $(_cheat_cheatpaths) ) + ;; + --rm) + _cheat_cheatsheets + ;; + --tags|-t) + COMPREPLY=( $(_cheat_tags) ) + ;; + esac + + $split && return + + if [[ ! "$cur" =~ ^-.* ]]; then + _cheat_cheatsheets + return + fi + + if [[ $cur == -* ]]; then + COMPREPLY=( $(compgen -W '$(_parse_help "$1")' -- "$cur") ) + [[ $COMPREPLY == *= ]] && compopt -o nospace + return + fi + +} && +complete -F _cheat cheat + +# ex: filetype=sh diff --git a/scripts/fzf.bash b/scripts/fzf.bash deleted file mode 100755 index 6cabadc..0000000 --- a/scripts/fzf.bash +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# This function enables you to choose a cheatsheet to view by selecting output -# from `cheat -l`. `source` it in your shell to enable it. (Consider renaming -# or aliasing it to something convenient.) - -# Arguments passed to this function (like --color) will be passed to the second -# invokation of `cheat`. -function cheat-fzf { - eval `cheat -l | tail -n +2 | fzf | awk -v vars="$*" '{ print "cheat " $1 " -t " $3, vars }'` -}