181 lines · 5.9 KB
function __z -d "Jump to a recent directory."
    function __print_help -d "Print z help."
        printf "Usage: $Z_CMD  [-celrth] regex1 regex2...\n\n"
        printf "         -c --clean    Removes directories that no longer exist from $Z_DATA\n"
        printf "         -d --dir      Opens matching directory using system file manager.\n"
        printf "         -e --echo     Prints best match, no cd\n"
        printf "         -l --list     List matches and scores, no cd\n"
        printf "         -p --purge    Delete all entries from $Z_DATA\n"
        printf "         -r --rank     Search by rank\n"
        printf "         -t --recent   Search by recency\n"
        printf "         -x --delete   Removes the current directory from $Z_DATA\n"
        printf "         -h --help     Print this help\n\n"

        if type -q fisher
            printf "Run `fisher help z` for more information.\n"
        end
    end
    function __z_legacy_escape_regex
        # taken from escape_string_pcre2 in fish
        # used to provide compatibility with fish 2
        for c in (string split '' $argv)
            if contains $c (string split '' '.^$*+()?[{}\\|-]')
                printf \\
            end
            printf '%s' $c
        end
    end

    set -l options "h/help" "c/clean" "e/echo" "l/list" "p/purge" "r/rank" "t/recent" "d/directory" "x/delete"

    argparse $options -- $argv

    if set -q _flag_help
        __print_help
        return 0
    else if set -q _flag_clean
        __z_clean
        printf "%s cleaned!\n" $Z_DATA
        return 0
    else if set -q _flag_purge