kardolus / chatgpt-cli

ChatGPT CLI is an advanced command-line interface for ChatGPT models via OpenAI and Azure, offering streaming, query mode, and history tracking for seamless, context-aware conversations. Ideal for both users and developers, it provides advanced configuration and easy setup options to ensure a tailored conversational experience with the GPT model.
MIT License
404 stars 28 forks source link

feat: add new flag to generate completion script #28

Closed benbenbang closed 4 months ago

benbenbang commented 4 months ago

Add a simple logic to generate a completion script.

The following is the "proof"

resolve #26


ChatGpt prompt:
╰─➤ go run cmd/chatgpt/main.go hello world 👋                                                                                  
Hello! How can I assist you today?

Set completion flag without input:
╰─➤ go run cmd/chatgpt/main.go --set-completions                                                                               
flag needs an argument: --set-completions
exit status 1

Set completion flag with invalid arg
╰─➤ go run cmd/chatgpt/main.go --set-completions donno                                                                         

Usage:
  --set-completions [bash|zsh|fish|powershell]

Flags:
  -h, --help   help for --set-completions

Invalid Arg: donno

Set completion flag with -h or --help
╰─➤ go run cmd/chatgpt/main.go --set-completions -h                                                                            
To load completions:

Bash:

  $ source <(chatgpt completion bash)

  # To load completions for each session, execute once:
  # Linux:
  $ chatgpt completion bash > /etc/bash_completion.d/chatgpt
  # macOS:
  $ chatgpt completion bash > /usr/local/etc/bash_completion.d/chatgpt

Zsh:

  # If shell completion is not already enabled in your environment,
  # you will need to enable it.  You can execute the following once:

  $ echo "autoload -U compinit; compinit" >> ~/.zshrc

  # To load completions for each session, execute once:
  $ chatgpt completion zsh > "${fpath[1]}/_chatgpt"

  # You will need to start a new shell for this setup to take effect.

fish:

  $ chatgpt completion fish | source

  # To load completions for each session, execute once:
  $ chatgpt completion fish > ~/.config/fish/completions/chatgpt.fish

PowerShell:

  PS> chatgpt completion powershell | Out-String | Invoke-Expression

  # To load completions for every new session, run:
  PS> chatgpt completion powershell > chatgpt.ps1
  # and source this file from your PowerShell profile.

Usage:
  --set-completions [bash|zsh|fish|powershell]

Flags:
  -h, --help   help for --set-completions

Set completion flag with zsh, for example:
╰─➤ go run cmd/chatgpt/main.go --set-completions zsh   
#compdef --set-completions
compdef _--set-completions --set-completions

# zsh completion for --set-completions                    -*- shell-script -*-

__--set-completions_debug()
{
    local file="$BASH_COMP_DEBUG_FILE"
    if [[ -n ${file} ]]; then
        echo "$*" >> "${file}"
    fi
}

_--set-completions()
{
    ...
}

# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_--set-completions" ]; then
    _--set-completions
fi                                                                        
kardolus commented 4 months ago

Thank you! This is great :).

The script that is being generated doesn't work for me for some reason. I may as well be misunderstanding the usage though. Here's my output:

 2024-02-22 17:27:57 ⌚  Guillermos-MacBook-Pro in ~/workspace/tmp/chatgpt-cli
± |feat/implement_autocompletion ✓| → . <(./bin/chatgpt --set-completions bash)
-bash: complete: --: invalid option
complete: usage: complete [-abcdefgjksuv] [-pr] [-DEI] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
benbenbang commented 4 months ago

Thank you! This is great :).

The script that is being generated doesn't work for me for some reason. I may as well be misunderstanding the usage though. Here's my output:

 2024-02-22 17:27:57 ⌚  Guillermos-MacBook-Pro in ~/workspace/tmp/chatgpt-cli
± |feat/implement_autocompletion ✓| → . <(./bin/chatgpt --set-completions bash)
-bash: complete: --: invalid option
complete: usage: complete [-abcdefgjksuv] [-pr] [-DEI] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]

Ah thanks, I should have seen this from the output above 🤦🏻‍♂️ The reason is due to #compdef --set-completions instead of chatgpt

I tested the new version. Now, it should work perfectly

completions

benbenbang commented 4 months ago

Fixed output:

╰─➤ chatgpt --set-completions zsh                                                                                              
#compdef chatgpt
compdef _chatgpt chatgpt

# zsh completion for chatgpt                              -*- shell-script -*-

__chatgpt_debug()
{
    ...
}

_chatgpt()
{
    ...
}

# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_chatgpt" ]; then
    _chatgpt
fi

Also, make the Usage clearer

╰─➤ chatgpt --set-completions abc                                                                                              

Usage:
  chatgpt --set-completions [bash|zsh|fish|powershell]

Flags:
  -h, --help   help for --set-completions

Invalid Arg: abc
kardolus commented 4 months ago

Very cool! Thank you!!