::::::::: PowerShell :::::::::
Tuesday, June 20, 2006
  Extending TabExpansion function Part 3
EDIT*: According to /\/\o\/\/'s advice(refer to comment's section), i have modified the previous source to use Get-Help instead of Get-ChildItem to retrieve available about_ help file names. (BTW, i will be modifying this script yet, again, so that "about_" would expand only when "Get-Help" or "Help" is already typed...(not to mess up File name expansion in $PsHome directory)


  # Handle "about_*" help manuals
'\s*(about_[\w|\d]*)' {
$matched = $matches[1]
$helpFileNames = Get-Help $matched | Sort-Object Name | Select-Object Name

if ($helpFileNames.Length -ne $null) {
$helpFileNames | foreach {
# extract manual name(NOTE:same RegEx pattern)
$m = [RegEx]::Matches($_.Name, '\s*(about_[\w|\d]*)')
$m[0].Value
}
}
}


In this version, i have added functionality to expand "about_[tab]" manual files.

Now you can do;
I was a bit annoyed to type long manual names like about_ubiquitous_parameters and about_regular_expression (which is a manual i read often) all the time so decided to become *lazy*...

When you do get-help about_alias, See Also section mentions about_variable manual.
If you try "about_v[Tab]" it would not work because there is no manual file named about_variable.help.txt in $pshome.

Now it should be apparent now how I have implemented my functionality.
It basically checks for manual file name that matches "about_blah*" against manual file names on $PSHOME directory.

switch -regex ($lastWord)
{
#
# Other functionalities...
#

# Handle "about_*" help manuals
'\s*(about_[\w\d]*)' {
$matched = $matches[1]
# Search for manual file name in $PSHOME
$helpFileNames = Get-ChildItem "$pshome/$($matched)*.help.txt" | Sort-Object Name | Select-Object Name

if ($helpFileNames -ne $null) {
$helpFileNames foreach {
# extract manual name(NOTE:same RegEx pattern)
$m = [RegEx]::Matches($_.Name, '\s*(about_[\w\d]*)')
$m[0].Value
}
}
}

# ...

Add above piece of code to "TabExpansion.ps1", go to wherever you have saved the file and ". .\TabExpansion.ps1" and you are good to go.
For previous blog posts(mine or /\/\o\/\/'s)on "TabExpansion", go check out "http://del.icio.us/powershell/TabExpansion"


Tags :
 
Comments:
Using het_help may speed it up :

PoSH>(measure-command {Get-ChildItem "$pshome/about_*.help.txt"}).TotalMilliseconds
46.8331

PoSH>(measure-command {get-help about_*}).TotalMilliseconds
16.745

Greetings /\/\o\/\/
 
Works great. I missed that, too :-)
 
Thanks for the feedback guys~

Mow, I have editted the previous code as you have suggested ;) thanks~
 
Would you mind posting your COMPLETE TabExpansion somewhere again? The Pastebin.com stuff is gone.
 
I have reposted here.
http://pastehere.com/?armhfr

Uhm, I am still trying to add more stuff on the function, so i have broken one of mow's tab functionality($host.ui.rawUi.[tab] wouldn't work)

I will try to update the working version up on the blog page...

But this one has only one more functionality which is to expand an alias into its original cmdlet/function name.

So "ls[tab]" would result in "Get-ChildItem" and "sal[tab]" to "Set-Alias" etc...
 
Could you start reving the version number when you make changes. So far I've ended up looking through the source on my system to figure out if I have the latest version.
 
Hello there, Steve?
Ah, i haven't actually added the source for TabExpansion function to a source control at the time working on this function.

But i have added tab function into a source control, so when I update the source next time, it will start from version 1.
 
Post a Comment



<< Home
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger