sigoden / argc-completions

{bash,zsh,fish,powershell,nushell}-completions for 1000+ commands.
MIT License
238 stars 13 forks source link

Pwsh #15

Closed Konfekt closed 11 months ago

Konfekt commented 11 months ago

Hello, thank you for this splendid tool. I wanted to test it in Pwsh 7.2.6. After running

git clone https://github.com/sigoden/argc-completions.git
cd argc-completions
./scripts/download-tools.sh    # download argc/yq to ./argc-completions/bin/
./scripts/setup-shell.sh powershell  # bash/zsh/powershell/fish/nushell/elvish/xonsh

there is an entry

# argc-completions
# Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
$env:ARGC_COMPLETIONS_ROOT = '/home/konfekt/argc-completions'    
$env:ARGC_COMPLETIONS_PATH = ($env:ARGC_COMPLETIONS_ROOT + '/completions')    
$env:PATH = $env:ARGC_COMPLETIONS_ROOT + '/bin' + [IO.Path]::PathSeparator + $env:PATH
argc --argc-completions powershell (
    (Get-ChildItem -File ($env:ARGC_COMPLETIONS_ROOT + '/completions')) |
    ForEach-Object { $_.Name -replace '\.sh$' }) | Out-String | Invoke-Expression

in Microsoft.PowerShell_profile.ps1. However, only completion for built-ins like gci works. As I suposed this would add completion to commands such as grep as well, I might be missing something?

sigoden commented 11 months ago

Run following code in powerwshell to check versions and deps:

cd $env:ARGC_COMPLETIONS_ROOT
argc version

The output text should look like the following:

argc 1.12.1
yq (https://github.com/mikefarah/yq/) version v4.34.1
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.2.0-p9, GNU MP 6.2.1)
sed (GNU sed) 4.9

Please let me know the results after execution so that I can analyze the problem. @Konfekt

Konfekt commented 11 months ago

All fine, impressive, updating from powershell 7.2.6 to 7.3.2 solved the problem

Konfekt commented 11 months ago

For completeness, it's the following output:

argc 1.12.1
yq (https://github.com/mikefarah/yq/) version v4.40.2
GNU Awk 4.2.1, API: 2.0
sed (GNU sed) 4.4
Konfekt commented 11 months ago

How about a check such as

if [ -d "$ARGC_COMPLETIONS_ROOT" ]; then
    export PATH="$ARGC_COMPLETIONS_ROOT/bin:$PATH"
    source <(ls -p -1 "$ARGC_COMPLETIONS_ROOT/completions" | sed -n 's/\.sh$//p' | xargs argc --argc-completions bash)
else
    echo "Please install argc-completions following https://github.com/sigoden/argc-completions#getting-started"
fi
sigoden commented 11 months ago

ARGC_COMPLETIONS_PATH is like PATH, it may contains multiple path. see https://github.com/sigoden/argc/blob/main/docs/environment-variables.md

Konfekt commented 11 months ago

Okay, I adapted

sigoden commented 11 months ago

$ARGC_COMPLETIONS_ROOT is required. Some scripts need $ARGC_COMPLETIONS_ROOT to locate _argc_util* functions like https://github.com/sigoden/argc-completions/blob/3d51c72134ff3a75f1cf82e76b3db947d4f15b59/completions/git.sh#L2764

sigoden commented 11 months ago

Argc internally use Register-ArgumentCompleter for completing. is the version requirement for PowerShell that high?

argc-completions can work on PS 5.1, but considering PowerShell/PowerShell#2912, you will have a better experience in PS 7+

Konfekt commented 11 months ago

Not sure about the lowest version making it work over here. As I tried on Linux, I only can try Pwsh versions > 5

Konfekt commented 11 months ago

Maybe

ARGC_COMPLETIONS_ROOT=/home/konfekt/argc-completions
if [ -d "$ARGC_COMPLETIONS_ROOT" ]; then
    export ARGC_COMPLETIONS_ROOT
    export ARGC_COMPLETIONS_PATH="$ARGC_COMPLETIONS_ROOT/completions"
    export PATH="$ARGC_COMPLETIONS_ROOT/bin:$PATH"
    source <(ls -p -1 "$ARGC_COMPLETIONS_ROOT/completions" | sed -n 's/\.sh$//p' | xargs argc --argc-completions zsh)
else
    unset ARGC_COMPLETIONS_ROOT
    echo "Please install argc-completions following https://github.com/sigoden/argc-completions#getting-started!"
fi

is the safest bet with the idea of using the same .zshrc on different computers.

Powershell Version:

# Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
$ARGC_COMPLETIONS_ROOT = $env:HOME + '/argc-completions'
if (Test-Path -Path $ARGC_COMPLETIONS_ROOT) {
  $env:ARGC_COMPLETIONS_ROOT = $ARGC_COMPLETIONS_ROOT
  $env:ARGC_COMPLETIONS_PATH = ($env:ARGC_COMPLETIONS_ROOT + '/completions')
  $env:PATH = $env:ARGC_COMPLETIONS_ROOT + '/bin' + [IO.Path]::PathSeparator + $env:PATH
  argc --argc-completions powershell (
      (Get-ChildItem -File ($env:ARGC_COMPLETIONS_ROOT + '/completions')) |
      ForEach-Object { $_.Name -replace '\.sh$' }) | Out-String | Invoke-Expression
} else {
  Remove-Item ARGC_COMPLETIONS_ROOT
    Write-Host "Please install argc-completions!"
}
sigoden commented 11 months ago

$ARGC_COMPLETIONS_ROOT should be together with the argc-completions directory. If you delete the argc-completions directory , you should remove the setup script from your shell.

Konfekt commented 11 months ago

I understand. The idea is that on some computers the directory is available, on others not.

Konfekt commented 11 months ago

By the way, I think people would be more comfortable if the added lines such as in https://github.com/sigoden/argc-completions/issues/15#issuecomment-1826439125 would be shown right away instead of saying that somehow the config files will be suitably adapted for argc-completions. At least mention that it will be not more than five lines or so.