This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
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 : Monad msh PowerShell