/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" The Microsoft Code "," Powershell links and free E-book "," PowerShell Tab Completion "," PowerShell out-DataGrid update and more Dataset ut... "," PowerShell : Getting Subdirectories using WMI part 3 "," PowerShell : Getting Subdirectories using WMI part 2 "," PowerShell : Getting Subdirectories using WMI "," PowerShell basic parameter checking of a function "," PowerShell Celsius Fahrenheit converting "," PowerShell : How Can I Rename Files Using File Nam... "," ")

Saturday, June 03, 2006

 


PowerShell Tab Completion Part 2



In a former post I did an extension on the Tab completion function of PowerShell
PowerShell Tab Completion to do also tab completion on Methods and properties of types.

in the comments , Jaques left an other addition and he did that also does add Tab competion and he did an Article in his Blog (in French) : PowerShell et la touche Tab (suite et fin)
(For a English version (Google translated) Go here )

As getting all the type info takes a while as it loops through all loaded assemblies , going on from his addition , I made one that does gets the TypeList only the first time and stores it in a Datatable also it handles also namespaces and levels.

also I add two other columns DC (dotCount) and NS (Namespace).
The dotCount is to filter only the items that are on the current level.
so that you can Tab level by level.

the second column NS I use to also list the Namespaces in the Tabcompletion,
they will be listed before the typenames and will not hace a closing bracked "]",
so that you can see that its and namespace and that you need another dot "." to get to a type.


[System.Dir [tab]
[System.DirectoryServices.[tab]
[System.DirectoryServices.DirectoryEntries][tab]

after that you can you the former addition the get a method o propery by typing :: and [Tab] again.(for more info see former post and janels article)

the first time you use tab on a type it will take a while and the completion is a bit buggy.
but after that the tabcompletion is very fast (note that you can use Shift-tab to scroll back also)
so this will make it very handy to browse the .NET namesspaces from PowerShell.

here the TabExpansion function, with only my additions, I wrapped them in a complete function so you can just past this code to the commandline to test it, (the other tab expansion willl not work then, but you can do this safely as it is only for the
session so if you start I new shell you have the normal tabhandling back.
but normaly you would add it to the default function (and load it in your profile) as explained in other posts.

# TabExpansion.ps1
# Additions to PowerShell TabHandling
# (for testing merge with default Profile for Normal use)
#
# /\/\o\/\/ 2006

# for testing to refill the DataTable
#$global:dtAssemblies = $null

function tabexpansion {

            # This is the default function that gets called for tab expansion. 
            # Edited by /\/\o\/\/ from the original to handle tab completion on types.
            
            param($line, $lastWord)

  
            switch -regex ($lastWord)
            {

               # Handle methods of Types..

               '(\[.*\])::(\w*)' {
                    invoke-expression "$($matches[1]) | gm -static" | where {$_.name -like "$($matches[2])*"} |% {
                      if ($_.MemberType -band $method) {
                        "$($matches[1])::$($_.name)" + '('
                      } Else {
                        "$($matches[1])::$($_.name)"
                      }
                    }
                   break;
               }

               # Cache and Handle namespace and TypeNames..

                 '\[(.*)' {

                   # only the first time Fill a DataTable with Typenames,namespaces and dotCount (level)

                   if (!($global:dtAssemblies)) {
                     $global:dtAssemblies = new data.datatable
                     [VOID]($global:dtAssemblies.Columns.add('name',[string]))
                     [VOID]($global:dtAssemblies.Columns.add('DC',[int]))
                     [VOID]($global:dtAssemblies.Columns.add('NS',[string]))
                     [void]([appdomain]::CurrentDomain.getassemblies() | foreach {
                       $_.GetTypes() |% {
                          $dc = $_.fullname.length - $_.fullname.replace('.','').length
                          $ns = $_.namespace
                          $global:dtAssemblies.rows.add("$_",$dc,$ns)
                       } 
                     })
                   }

                   # actual tab completion

                   $dots = $matches[1].length - $matches[1].replace('.','').length
                   switch ($dots) {
                     0 {"[system","[microsoft"}
                     Default { 
                         $res = $global:dtAssemblies.select("ns like '$($matches[1])%' and dc = $($dots + 1)") | select -uni ns |% {"[$($_.ns)"};
                         $res += $global:dtAssemblies.select("name like '$($matches[1])%' and dc = $dots") |% {"[$($_.name)]"}
                         @($res)
                       }
                   }
                   break;
                 }

               ###
               ##### Other Tab handling from default function here !!!!
               ###
              } 
        
}


I'm sure there is more to come on the tabcompletion front ;-)

Enjoy,

Greetings /\/\o\/\/

Tags :


Comments:
Anonymous Anonymous
This is brilliant! It works like a charm, once I have replaced "new data.datatable" with "new-objet data.datatable". I will have to add the "new" alias to my profile one day. :-)

BTW, the Google translation of my blog entry looks pretty good but opening it crashes all my Internet Explorer windows... :(

Thanks,
Jacques
 
Blogger /\/\o\/\/
hi Jacques,

Glad you like it.
took me some time and a lot of doing measure-command, to find a solution fast enough.

about the new, yep I keep on forgetting I have set-alias new new-object in my profile (and the loading of the formslibrary
I realy hope they add the alias in the RTW

next one on my wishlist for tab is multylevel variable expansion.

Greetings /\/\o\/\/
 
Blogger /\/\o\/\/
hotfix for only one namespace, and sorting :
(try [system.m [tab] versus
[system.ma [tab]
$res = @();$res += $global:dtAssemblies.select("ns like '$($matches[1])%' and dc = $($dots + 1)") | sort ns | select -uni ns |% {"[$($_.ns)"};
$res += $global:dtAssemblies.select("name like '$($matches[1])%' and dc = $dots") | sort name |% {"[$($_.name)]"}

gr /\/\o\/\/
 
Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?