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;
  • about_[Tab] -> about_Alias
  • about_r[Tab] -> about_regular_expression
  • about_regular_[Tab] -> about_regular_expression
  • about_u[Tab] -> about_ubiquitous_parameters
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 :

7 comments:

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

    ReplyDelete
  2. Works great. I missed that, too :-)

    ReplyDelete
  3. Thanks for the feedback guys~

    Mow, I have editted the previous code as you have suggested ;) thanks~

    ReplyDelete
  4. Would you mind posting your COMPLETE TabExpansion somewhere again? The Pastebin.com stuff is gone.

    ReplyDelete
  5. 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...

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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.

    ReplyDelete