Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache the api response into a file, to accelerate the completion. #586

Open
fakeyanss opened this issue Aug 14, 2022 · 0 comments
Open

Cache the api response into a file, to accelerate the completion. #586

fakeyanss opened this issue Aug 14, 2022 · 0 comments
Labels

Comments

@fakeyanss
Copy link

Sorry I have not found the doc | Advanced Command Line source. So I can not submit a PR of it.

If we send a request to the server https://www.toptal.com/developers/gitignore/api/$argv each time, the network latency of command completion will be obvious.

We can save the api response into a file to cache it.

# generate .gitignore template
function gi() {
    curl -sL https://www.toptal.com/developers/gitignore/api/$@ ;
}

# Provides gi completion for zsh
_gitignoreio_get_command_list() {
    # gi cmd cache file
    cache=~/.config/.gi_cmd_list
    ls cache >/dev/null 2>&1
    if [[ $? == 0 ]]; then
        modify=$(date -j -f %c $(stat -x $cache|grep 'Modify: '|awk -F 'Modify: ' '{print $2}') +%s)
        expire=$(($(date +%s)-$modify))
        # check update once half a month
        if [[ expire > 1296000 ]]; then
            cat $cache
            exit 0
        fi
    fi
    curl -sL https://www.toptal.com/developers/gitignore/api/list | tr "," "\n" > $cache
    cat $cache
}

_gitignoreio () {
    compset -P '*,'
    compadd -S '' $(_gitignoreio_get_command_list)
}

compdef _gitignoreio gi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants