Myzel394 / zsh-copilot

How we all expected GitHub Copilot in the CLI to be. No `suggest` bullshit
97 stars 11 forks source link

Run automatically #1

Open Myzel394 opened 7 months ago

Myzel394 commented 7 months ago

So I tried to let copilot run automatically, but wasn't successful to do so. I used the TRAPALRM function with TMOUT to run a function every few seconds to detect whether the user finished typing. While this worked, showing suggestions or changing the BUFFER does not seem to be allowed by zsh. I tried calling the widget to let it handle the situation, but was unable to find a solution.

If someone knows a way how to do this, I'd be happy about PRs.

Here's the code I used:

# Global variables
local last_input_hash=""
# This is the hash to which the last suggestion happened against. This is used to prevent multiple suggestions for the same input
local last_suggested_input_hash=""

TMOUT=2
TRAPALRM() {
    if [[ "$BUFFER" == "" ]]; then
        last_input_hash=""
        return
    fi

    # content plus length
    local hash=$(echo "$BUFFER-${#BUFFER}")

    if [[ "$hash" == "$last_suggested_input_hash" ]]; then
        return
    fi

    if [[ "$hash" != "$last_input_hash" ]]; then
        # User is still typing
        last_input_hash="$hash"
    else
        if [[ $is_suggesting == 'false' ]]; then
            _suggest_ai
            last_suggested_input_hash="$hash"
        fi
    fi
}