自动调用 PowerShell 中的函数

Automatic invocation a function in PowerShell

是的,所以我一直在研究 Terminal-Icons git 存储库,因为我一直想制作自己的 shell 格式化程序 [使用 powershell 和 csharp]。虽然大部分都已完成,但我注意到其中一个文件中有一个有趣的短语 [https://github.com/devblackops/Terminal-Icons/blob/main/Terminal-Icons/Public/Format-TerminalIcons.ps1] :

列出目录。终端图标将被自动调用以显示

现在,我不太明白如何什么他们在这里做什么[我是新手写作力量shell 代码]。我想了解的主要事情是他们如何格式化 Get-ChildItem cmdlet 的输出以及我如何实现类似的东西。

您 link 的功能,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

换句话说:该项目使用标准的 PowerShell 功能,允许将 .NET 类型与自定义 for-display 格式化指令相关联,这些指令在此类类型的实例呈现到显示器时自动应用。

Terminal-Icon read-me 所述:

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.

具体来说,*.format.ps1xml 文件是 Terminal-Icons.format.ps1xml,您会在其中找到对 Format-TerminalIcons 的多次调用;例如:

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

另请参阅: