PowerShell TabExtension - Part 6 Customizing PowerTab 0.9
I mentioned in last post about the new version of PowerTab PowerShell TabExtension - Part 5 PowerTab 0.9 ,That the TabSelection handler used for DoubleTab is extendable :
Aaron has also updated his Invoke-Intellisense, and changed it into a Cmdlet See : PowerTab 0.9 for PowerShell and Invoke-Intellisens... ,
As you could see in his post you just had to change the variable $alternateHandler in the Tabextension.ps1 file to change the DoubleTab to another handler, in this case Aaron's new Invoke-Intellisense Cmdlet, but as you can see later you can add more handlers, and that was enough to change all doubleclick functionality to his commandlet and enable it to handle the selection :
There is even a better way, if you change lhe line to this :
if (-not $AlternateHandler){$AlternateHandler = 'ConsoleList'}
you can change it interactively in the PowerShell console by changing this variable to switch between the modes, and do not need to change the function anymore to switch handlers :
Let's see how this extendable system works, same as out-DataGridView (Other GUI handler see : PowerTab 0.72 PowerShell TabExtension - Part 2 The basics) the Console Based Intellisync done by calling a function that is, I also did put it in a different scriptfile.
As an example I made a helper script to load Snapins, that uses the out-ConsoleList function to show all snapins that are registered but not loaded (Add-PSSnapin ) and select the snapin to load, to show how you can also use this for your own uses outside of the Tabcompletion :
PoSH> Function add-RegisteredSnapin {
>> Write-Host -NoNewline 'SnapIn to Add:'
>>
>> Get-PSSnapin -Registered |% {$_.name} |? {
>> (get-psSnapin |% {$_.name}) -NotContains $_
>> } | Out-ConsoleList |% {Add-PSSnapin $_} ;''
>> }
>>
PoSH> add-RegisteredSnapin
SnapIn to Add:PoSH>
PoSH>
PoSH> InvokeApartmentSample
PoSH> LerchSnapIn
PoSH> PowerGadgets
PoSH> Pscx
PoSH> [ 2 of 4 ]
PoSH>
and after loading the snapin you can do the same trick with invoke-intellisense as you can see here (note that as LerchSnapIn is allready loaded it's not shown anymore :
In the TabExpansionLib.ps1 the helperfunction Invoke-TabItemSelector is defined where you can add other handlers :
Function Invoke-TabItemSelector ($LastWord,$SelectionHandler = 'Default') {
Switch ($SelectionHandler) {
'Default' {$Input}
'intellisense'{$Input | Invoke-Intellisense $LastWord}
'ConsoleList'{$Input | Out-ConsoleList $LastWord}
}
}
Just create another name and add the script you want invoked in the Switch statement here, and your able use it from PowerTab
In the TabExpansion function the handler is chosen depending on the DoubleTab state
If ($DoubleTab) {$SelectionHandler = $AlternateHandler} Else {$SelectionHandler = 'Default'}
As the Invoke-TabItemSelector is invoked by every Tabcompletion function using those two variables you can tune the Tab behavour on every individual case :
See the example for Alias (Only DoubleClick and the Function and CmdLet completion :
# Cmdlets, functions, aliases and Custom additions from DataBase
'(.*)' {
If ($DoubleTab) {
&{Get-Command -commandType Alias -Name "$($matches[1])" |% {$_.Definition}
$global:dsTabExpansion.Tables['Custom'].select("filter like '$($matches[1])' AND type = 'Custom'") |% {$_.text} }
}
}
'(.*)%' {
Get-Command -commandType Function,Filter, Cmdlet -Name "$($matches[1])*" |% {$_.Name} | Invoke-TabItemSelector $lastWord -SelectionHandler $SelectionHandler
}
In this case I wanted different behavour for aliases so could change that here, or listen to the default ($SelectionHandler) .
do you want another shortcut to complete partial functions e.g. :
PoSH> inv%
PoSH>
PoSH> Invoke-Expression
PoSH> Invoke-History
PoSH> Invoke-Intellisense
PoSH> Invoke-Item
PoSH> Invoke-TabItemSelector
PoSH> [ 1 of 5 ]
PoSH>
or for Native commands (!), add another pattern to PowerTab you can all do that here, so I recommend you to next to addin custom commands to the datadase and use the PowerTab utilities :
PowerTab 0.72 PowerShell TabExtension - Part 3 The DataBase and utilities
PowerTab 0.8 PowerShell TabExtension - Part 4 Updates from 0.72 (New Functions and Utilities )
also look at the rest of the completions in Tabexpansion especaily the ones at the and of the script they help to discover get to know more of the functionality and to configure PowerTab just as you like to to react .
Enjoy,
Greetings /\/\o\/\/