Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

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\/\/

Published Wednesday, April 18, 2007 4:50 PM by admin

Comments

# re: PowerShell TabExtension - Part 6 Customizing PowerTab 0.9

I just wanted to say that I think what you've done with the PowerShell Tab Extension is fantastic, love it! Very nice work!

If you don't mind a curious question about it, though: How do you hook up your tab extension to be called when the tab key is pressed in the powershell prompt? I know in general terms how the default powershell host does tab completion (more or less), but I thought it was pretty impressive and very clever how you were able to complement that in your extension!

Wednesday, April 18, 2007 7:12 PM by Tomas Restrepo

# re: PowerShell TabExtension - Part 6 Customizing PowerTab 0.9

Hi Tomas,

The default handling of Tab is calling the TabExpansion function.

this is also the case in the default installation, a default TabExpansion is provided with powerShell, so changing this function is all what is needed.

Greetings /\/\o\/\/

Thursday, April 19, 2007 7:25 AM by MoW

# re: PowerShell TabExtension - Part 6 Customizing PowerTab 0.9

as usual mow, your output is top-tier.

Thursday, April 19, 2007 1:47 PM by Oisin Grehan

# PowerShell TabExtension - Part 7 PowerTab 0.91 Released

And another version of the PowerShell tabExpansion script library PowerTab is released, with some really

Sunday, April 22, 2007 5:35 PM by The PowerShell Guy

# The PowerShell Guy : PowerTab

Sunday, April 22, 2007 5:57 PM by The PowerShell Guy : PowerTab

# re: PowerShell TabExtension - Part 6 Customizing PowerTab 0.9

@ Oisin :

You will love the jsut released 0.91 version ;-)

Greetings /\/\o\/\/

Sunday, April 22, 2007 6:03 PM by MoW
Anonymous comments are disabled