Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Version: 
Details    
spicedb / usr / share / fish / vendor_completions.d / spicedb.fish
Size: Mime:
# fish completion for spicedb                              -*- shell-script -*-

function __spicedb_debug
    set -l file "$BASH_COMP_DEBUG_FILE"
    if test -n "$file"
        echo "$argv" >> $file
    end
end

function __spicedb_perform_completion
    __spicedb_debug "Starting __spicedb_perform_completion"

    # Extract all args except the last one
    set -l args (commandline -opc)
    # Extract the last arg and escape it in case it is a space
    set -l lastArg (string escape -- (commandline -ct))

    __spicedb_debug "args: $args"
    __spicedb_debug "last arg: $lastArg"

    # Disable ActiveHelp which is not supported for fish shell
    set -l requestComp "SPICEDB_ACTIVE_HELP=0 $args[1] __complete $args[2..-1] $lastArg"

    __spicedb_debug "Calling $requestComp"
    set -l results (eval $requestComp 2> /dev/null)

    # Some programs may output extra empty lines after the directive.
    # Let's ignore them or else it will break completion.
    # Ref: https://github.com/spf13/cobra/issues/1279
    for line in $results[-1..1]
        if test (string trim -- $line) = ""
            # Found an empty line, remove it
            set results $results[1..-2]
        else
            # Found non-empty line, we have our proper output
            break
        end
    end

    set -l comps $results[1..-2]
    set -l directiveLine $results[-1]

    # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
    # completions must be prefixed with the flag
    set -l flagPrefix (string match -r -- '-.*=' "$lastArg")

    __spicedb_debug "Comps: $comps"
    __spicedb_debug "DirectiveLine: $directiveLine"
    __spicedb_debug "flagPrefix: $flagPrefix"

    for comp in $comps
        printf "%s%s\n" "$flagPrefix" "$comp"
    end

    printf "%s\n" "$directiveLine"
end

# this function limits calls to __spicedb_perform_completion, by caching the result behind $__spicedb_perform_completion_once_result
function __spicedb_perform_completion_once
    __spicedb_debug "Starting __spicedb_perform_completion_once"

    if test -n "$__spicedb_perform_completion_once_result"
        __spicedb_debug "Seems like a valid result already exists, skipping __spicedb_perform_completion"
        return 0
    end

    set --global __spicedb_perform_completion_once_result (__spicedb_perform_completion)
    if test -z "$__spicedb_perform_completion_once_result"
        __spicedb_debug "No completions, probably due to a failure"
        return 1
    end

    __spicedb_debug "Performed completions and set __spicedb_perform_completion_once_result"
    return 0
end

# this function is used to clear the $__spicedb_perform_completion_once_result variable after completions are run
function __spicedb_clear_perform_completion_once_result
    __spicedb_debug ""
    __spicedb_debug "========= clearing previously set __spicedb_perform_completion_once_result variable =========="
    set --erase __spicedb_perform_completion_once_result
    __spicedb_debug "Successfully erased the variable __spicedb_perform_completion_once_result"
end

function __spicedb_requires_order_preservation
    __spicedb_debug ""
    __spicedb_debug "========= checking if order preservation is required =========="

    __spicedb_perform_completion_once
    if test -z "$__spicedb_perform_completion_once_result"
        __spicedb_debug "Error determining if order preservation is required"
        return 1
    end

    set -l directive (string sub --start 2 $__spicedb_perform_completion_once_result[-1])
    __spicedb_debug "Directive is: $directive"

    set -l shellCompDirectiveKeepOrder 32
    set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2)
    __spicedb_debug "Keeporder is: $keeporder"

    if test $keeporder -ne 0
        __spicedb_debug "This does require order preservation"
        return 0
    end

    __spicedb_debug "This doesn't require order preservation"
    return 1
end


# This function does two things:
# - Obtain the completions and store them in the global __spicedb_comp_results
# - Return false if file completion should be performed
function __spicedb_prepare_completions
    __spicedb_debug ""
    __spicedb_debug "========= starting completion logic =========="

    # Start fresh
    set --erase __spicedb_comp_results

    __spicedb_perform_completion_once
    __spicedb_debug "Completion results: $__spicedb_perform_completion_once_result"