rsnip 0.6.1

A universal command-line snippet manager
Documentation
# rsnip.bash completion and aliases
{%- macro section(name) -%}
# =================================== {{ name }} ===================================
{%- endmacro -%}

# Unset any existing aliases and completions
complete -r rsnip > /dev/null 2>&1  # Remove any existing completion
{%- for type in snippet_types %}
# DEBUG: {{ type }}
{%- if type.alias %}
unalias {{ type.alias }} 2>/dev/null  # Remove existing alias
unalias e{{ type.alias }} 2>/dev/null  # Remove existing edit alias
complete -r "{{ type.alias }}" > /dev/null 2>&1  # Remove existing completion
complete -r "e{{ type.alias }}" > /dev/null 2>&1  # Remove existing edit completion
{%- endif %}
{%- endfor %}

# Set up aliases
{%- for type in snippet_types %}
{%- if type.alias %}
alias {{ type.alias }}="rsnip copy --ctype {{ type.name }} --input"
alias e{{ type.alias }}="rsnip edit --ctype {{ type.name }} --input"
{%- endif %}
{%- endfor %}

# Enhanced completion function
_rsnip_complete() {
    local cur prev cmd
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Detect if we're using an alias or full command
    local is_alias=false
    local alias_type=""
    local is_edit=false
    case "${COMP_WORDS[0]}" in
    {%- for type in snippet_types %}
    {%- if type.alias %}
        "{{ type.alias }}")
            is_alias=true
            alias_type="{{ type.name }}"
            ;;
        "e{{ type.alias }}")
            is_alias=true
            is_edit=true
            alias_type="{{ type.name }}"
            ;;
    {%- endif %}
    {%- endfor %}
        *)
            # List of available commands
            local commands="copy edit list types"

            # If completing a command
            if [[ ${COMP_CWORD} -eq 1 ]]; then
                COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
                return 0
            fi
            ;;
    esac

    # Get available snippet types
    local snippet_types=$(rsnip types --list)

    case "${prev}" in
        "--ctype"|"-c")
            COMPREPLY=( $(compgen -W "${snippet_types}" -- ${cur}) )
            return 0
            ;;
        "complete"|"copy"|"edit")
            COMPREPLY=( $(compgen -W "--ctype --input --interactive" -- ${cur}) )
            return 0
            ;;
    esac

    # Handle alias cases
    if [[ ${is_alias} == true ]]; then
        # Save current terminal state
        tput smcup

        local result
        result="$(rsnip complete --interactive --ctype "${alias_type}" --input "${cur}")"

        # Restore terminal state
        tput rmcup

        if [[ -n "$result" ]]; then
            COMPREPLY=("$result")
            # Redraw line after fzf closes
            printf '\e[5n'
        fi
        return 0
    fi

    # If we're completing an input and have a type specified
    local ctype=""
    for ((i=1; i<COMP_CWORD; i++)); do
        if [[ "${COMP_WORDS[i]}" == "--ctype" || "${COMP_WORDS[i]}" == "-c" ]]; then
            ctype="${COMP_WORDS[i+1]}"
            break
        fi
    done

    if [[ -n "${ctype}" && "${prev}" == "--input" ]]; then
        # Save cursor position and clear below
        printf '\e7\e[J'

        local result
        result="$(rsnip complete --interactive --ctype "${ctype}" --input "${cur}")"

        # Restore cursor position and clear any remnants
        printf '\e8\e[J'

        if [[ -n "$result" ]]; then
            COMPREPLY=("$result")
        else
            # On ESC/cancel preserve current input
            COMPREPLY=("$cur")
        fi
        # Make sure line is redrawn correctly
        bind 'redraw-current-line' >/dev/null 2>&1
    fi
}

# Ensure terminal properly handles escape sequences
if [[ $TERM != "dumb" ]]; then
    bind 'set enable-bracketed-paste off' 2>/dev/null
    bind 'set mark-symlinked-directories on' 2>/dev/null
fi

# Setup completion for command and all aliases
complete -F _rsnip_complete rsnip
{%- for type in snippet_types %}
{%- if type.alias %}
complete -F _rsnip_complete "{{ type.alias }}"
complete -F _rsnip_complete "e{{ type.alias }}"
{%- endif %}
{%- endfor %}

{{ section("Usage") }}
# To initialize rsnip, add this to your shell configuration file (usually ~/.bashrc):
#
# source <(rsnip --generate bash)
#
# Examples:
# , <tab>          # Fuzzy find and copy a snippet
# e, <tab>         # Fuzzy find and edit a snippet