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

windows - explore powershell completion #26

Open
mklabs opened this issue May 30, 2016 · 5 comments
Open

windows - explore powershell completion #26

mklabs opened this issue May 30, 2016 · 5 comments

Comments

@mklabs
Copy link
Owner

mklabs commented May 30, 2016

http://www.leeholmes.com/blog/2012/09/13/bash-like-tab-completion-in-powershell/

powershell became a very capable terminal emulator, see how we could support windows completion.

@mklabs
Copy link
Owner Author

mklabs commented May 30, 2016

http://powertab.codeplex.com/
http://powertab.codeplex.com/wikipage?title=Tab%20Expansion%20Handlers&referringTitle=Home


Register-TabExpansion "Get-Service" -Type Command {
    param($Context, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces)  # 1:

    $Argument = $Context.Argument
    switch -exact ($Context.Parameter) {
        'DisplayName' {
            $TabExpansionHasOutput.Value = $true  # 2:
            Get-Service -DisplayName "*$Argument*" | Select-Object -ExpandProperty DisplayName  # 3:
        }
        'Name' {
            $TabExpansionHasOutput.Value = $true  # 2:
            Get-Service -Name "$Argument*" | Select-Object -ExpandProperty Name  # 3:
        }
    }
}

param handler

Register-TabExpansion "PSDrive" -Type Parameter {
    param($Argument, [ref]$TabExpansionHasOutput, [ref]$QuoteSpaces)  # 1:

    if ($Argument -notlike '^\$') {
        $TabExpansionHasOutput.Value = $true  # 2:
        Get-PSDrive "$Argument*" | Select-Object -ExpandProperty Name  # 3:
    }
}

register

Register-TabExpansion [-Name] <String> [-Handler] <ScriptBlock> [-Type <String>] [-Force]

@mklabs
Copy link
Owner Author

mklabs commented May 30, 2016

@cspotcode
Copy link

PowerShell Core natively includes Register-ArgumentCompleter to register completions for external binaries.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-6

It can return an array of CompletionResult instances, which describe the completion text, label text, description, etc.

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.completionresult?view=powershellsdk-1.1.0

PowerShell can natively emit and parse JSON, so it should be easy to pass all the necessary data to and from tabtab without the need to write much PowerShell syntax. Tabtab can do all the work in JS and write JSON to stdout.

Register-ArgumentCompleter -Native -CommandName tabtab-test -ScriptBlock {
    $argsEncoded = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(($args | convertto-json)))
    $results = tabtab-test completion -- $argsEncoded | convertfrom-json
    foreach($result in $results) {
        [Management.Automation.CompletionResult]::new($result[0], $result[1], $result[2], $result[3])
    }
}

@ExE-Boss
Copy link

Do note that PowerShell is now also supported on Unix‑based operating systems, so this is no longer just for Windows.

@KiruyaMomochi
Copy link

Here is how Cobra, a CLI framework for Go, supports it:
https://github.com/spf13/cobra/blob/main/powershell_completions.go

Base on that, user @jcwillox has written a script to support PowerShell completion for pnpm:
https://gist.github.com/jcwillox/027b6c105d190abfa0333c13836f5bec

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

No branches or pull requests

4 participants