Jump to content
Software FX Community

Tab Completion in PowerGadgets


Recommended Posts

Tab Completion and how it can be modified is an interesting feature in PowerShell, for our particular cmdlets we actually improved on it in several ways.


Enumeration values are listed when appropriate, e.g. out-chart -gallery <TAB> will list Lines, Area, Bar, Pie and all the gallery types supported by the -gallery parameter.

Formatted values

Certain parameters require specially formatted values, examples of this are Font, Size, Color and TimeSpan. By typing out-chart -font <TAB> you will get a preview of the format required to specify a font.

Runtime values

In certain properties we can actually use runtime information for tab completion:

  • out-chart -sidebar <TAB> will list all the PowerGadgets gadgets currently running in the Vista sidebar. Works for all 3 cmdlets. 
  • out-gauge -template <TAB> will list all the PGT files in the documents folder. Works for all 3 cmdlets.
  • out-map -mapsource <TAB> will list all the map files (SVG) in the map library folder. Note that we will also list subdirectories, to get the files in one of the listed folders append a backslash and type <TAB> again.


In order to implement all the functionality in our controls we obviously grouped the exposed API in different classes, you access these classes with the underscore (-) character after the object name, e.g. in out-chart we expose an AxisX and AxisY objects, both implement similar functionality as both are an Axis. You get to any axis property by typing out-chart -axisy_<TAB> you will get a list of the properties supported by an Axis object. Note that sometimes objects will in turn contain other objects, for example typing out-chart -axisy_labelsformat_<TAB> will list the properties that allow you to control the formatting of the labels in an axis (Decimals, Format, Culture, etc.)

You may be asking yourself at this point why your PowerShell session does not expose any of this tab-completion features, the reason is that tab completion is controlled by a function in your profile, you can read more about profiles here, but most of the time you will create a file named profile.ps1 and place it in a subdirectory of your Documents folder called WindowsPowershell. Because trying to mix your current profile was not a practical approach, we decided to just ship a file named profile.ps1, it contains a modified version of the built-in tabexpansion function. If you do not have a profile all you have to do is copy this file to the appropriate folder. If you already have one you will have to merge the contents of both files, possibly merging the contents of the tabcompletion functions, if you already had one. The modifications we did to this function are highlighted in this picture (some details of the function were replaced with ...)

Posted Image

If anybody from the PowerShell team is reading this, I think this would be a nice improvement for future versions. Some way to allow cmdlets to enhance tab completion for their use without actually having to modify the profile function (e.g. a static function with a predefined name in the cmdlet class). Obviously being able to hook into the tab completion with a function in your profile allows you to customize this process in very interesting ways but if multiple cmdlets want to enhance tab completion you can see how the model starts to complicate.


Link to comment
Share on other sites

  • 5 months later...
  • Create New...