Home > Net >  Automatic invocation a function in PowerShell
Automatic invocation a function in PowerShell

Time:05-22

Right so I have been investigating the Terminal-Icons git repo because I have been wanting to make my own shell formatter [using powershell and csharp]. While most of it is completed,I noticed an interesting phrase in one of the files [https://github.com/devblackops/Terminal-Icons/blob/main/Terminal-Icons/Public/Format-TerminalIcons.ps1] :

List a directory. Terminal-Icons will be invoked automatically for display

Now, I do not quite understand how or what they are doing here[I am new to writing powershell code]. The main thing that I want to understand is how they are formatting the output of the Get-ChildItem cmdlet and how I could implement something like that.

CodePudding user response:

The function you link to, Format-TerminalIcons, is being called from custom for-display formatting data that the module project at hand associates with the .NET types that cmdlets such a Get-ChildItem and Get-Item output, namely System.IO.FileInfo and System.IO.DirectoryInfo.

In other words: the project makes use of a standard PowerShell feature that allows associating .NET types with custom for-display formatting instructions that are automatically applied when instances of such types render to the display.

As the Terminal-Icon read-me states:

It [the module] uses a custom format.ps1xml file that inspects the items being displayed and looks up their appropriate icon based on name or extension.

Specifically, that *.format.ps1xml file is Terminal-Icons.format.ps1xml, and in it you'll find multiple calls to Format-TerminalIcons; e.g.:

                            <TableColumnItem>
                                <ScriptBlock>
                                    Terminal-Icons\Format-TerminalIcons $_
                                </ScriptBlock>
                            </TableColumnItem>

See also:

  • Related