tabtab icon indicating copy to clipboard operation
tabtab copied to clipboard

windows - explore powershell completion

Open mklabs opened this issue 9 years ago • 5 comments
trafficstars

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 avatar May 30 '16 06:05 mklabs

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 avatar May 30 '16 07:05 mklabs

https://github.com/lzybkr/PSReadLine

mklabs avatar May 30 '16 07:05 mklabs

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])
    }
}

cspotcode avatar Feb 10 '19 04:02 cspotcode

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

ExE-Boss avatar Jan 28 '20 20:01 ExE-Boss

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

KiruyaMomochi avatar Jul 26 '22 07:07 KiruyaMomochi