PowershellFrameworkCollective / psframework

A module that provides tools for other modules and scripts
MIT License
433 stars 41 forks source link

Add ListItemText support to Register-PSFTeppScriptblock #563

Closed Alex-Larner closed 1 year ago

Alex-Larner commented 1 year ago

Could you please update Register-PSFTeppScriptblock to support the Completion Result's List Item Text property in addition to the current Text & ToolTip properties and add a parameter to specify what property to filter the wordToComplete on? i.e.

Register-PSFTeppScriptblock -Name Test.Human -ScriptBlock { 
    $Humans | Select-PSFObject 'ID as Text', 'Name as ListText', 'ToString() as ToolTip'
} -FilterOn ListText

Setup Code

class Human {
    [int]$ID
    [string]$Name

    Human (
        [int]$ID,
        [string]$Name
    ) {
        $this.ID = $ID
        $this.Name = $Name
    }

    [string] ToString () {
        return "$($this.Name) ($($this.ID))"
    }
}

$HumanData = @(
    @{
        ID = 1
        Name = 'John Doe'
    },
    @{
        ID = 2
        Name = 'Jane Doe'
    },
    @{
        ID = 3
        Name = 'Play Dough'
    },
    @{
        ID = 4
        Name = 'Pizza Dough'
    }
)

$Humans = $HumanData | ForEach-Object { [Human]::New($_.ID, $_.Name) }

function Invoke-Human {
    [CmdletBinding()]
    param (
        [Human]$Human
    )
    Write-PSFMessage -Level Host -Message "Using $Human"
}

Currently the list items are shown as the text and not the tooltip, so the list view relies on knowing the IDs instead of the names, or navigating through the options one at a time to see the name in the tooltip, which negates the user-friendliness:

Register-PSFTeppScriptblock -Name Test.Human -ScriptBlock { $Humans | Select-PSFObject 'ID as Text', 'Name as Tooltip' }
Register-PSFTeppArgumentCompleter -Name Test.Human -Parameter Human -Command Invoke-Human
Invoke-Human -Human <Ctrl+Space>

image

But if you specify the WordToComplete property and the ListItemText, you can autocomplete on the user-friendly names and show those names on the list view, while the selection renders down to the ID:

Register-PSFTeppScriptblock -Name Test.Human -ScriptBlock {
    param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $Humans |
        Where-Object Name -like "$wordToComplete*" |
            ForEach-Object {
                [System.Management.Automation.CompletionResult]::New($_.ID, $_.Name, 'ParameterValue', $_.ToString())
            }
}
Register-PSFTeppArgumentCompleter -Name Test.Human -Parameter Human -Command Invoke-Human
Invoke-Human -Human <Ctrl+Space>

image

Invoke-Human -Human J<Ctrl+Space>

image

FriedrichWeinmann commented 1 year ago

Hi Alex, implemented and released in 1.7.270