当我显示目录条目时会发出什么标题?

时间:2011-07-30 13:51:22

标签: powershell powershell-v2.0

令人尴尬承认,但在Monad处于测试阶段之后,我从某种程度上忽略了PowerShell。所以我现在正在蘸着脚趾。

我想要一个功能来为目录列表着色,我在网上找到了一个:

function LL {
    param ($dir = ".", $all = $false)

    $origFg = $host.ui.rawui.foregroundColor
    if ( $all ) { $toList = ls -force $dir }
    else { $toList = ls $dir }

    foreach ($Item in $toList)  {
        Switch ($Item.Extension)  {
            ".Exe" {$host.ui.rawui.foregroundColor = "Yellow"}
            ".cmd" {$host.ui.rawui.foregroundColor = "Red"}
            ".msh" {$host.ui.rawui.foregroundColor = "Red"}
            ".vbs" {$host.ui.rawui.foregroundColor = "Red"}
            Default {$host.ui.rawui.foregroundColor = $origFg}
        }
##        if ($item.Mode.StartsWith("d")) {$host.ui.rawui.foregroundColor = "White"}
        if ($item.PSIsContainer) {$host.ui.rawui.foregroundColor = "White"}
        $item
    }
    $host.ui.rawui.foregroundColor = $origFg
}

唯一的问题是结果显示中的标题内容始终显示为分配给目录中第一个条目的颜色。

所以我决定查看分配给$ tolist变量的数组:

09:47:10|# $tolist = ls
09:47:26|# $tolist[0]


    Directory: D:\Documents and Settings\200018252


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/15/2011   8:15 AM            .ssh


09:47:37|# $tolist[9]


    Directory: D:\Documents and Settings\200018252


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         3/10/2011   1:14 PM            vimperator


09:48:19|#

因此,当显示$ tolist的每个元素前面都有一个空白行,然后显示一个目录,另一个空白行,然后是列标题。

我需要控制此信息的前景色。发生了什么?

3 个答案:

答案 0 :(得分:0)

发出的内容将是format- *命令之一,格式表或格式列表,具体取决于对象类型以及默认成员集中的属性数。

它使用格式表达到一定数量的成员,如果格式表太多(我不记得设置了该阈值的位置),则恢复为格式列表。默认成员集中包含的属性在types.ps1xml文件中定义:

http://technet.microsoft.com/en-us/library/dd347581.aspx

您可以修改此文件,但似乎没有任何指定显示颜色的规定。

答案 1 :(得分:0)

非常讨厌获取标题颜色的方式(我等着看是否有人发布了不像这样的黑客,但是因为没有人发布,这是我的解决方案):

$dummy = "justadummynametoaddtothebeginningofthelistofitems"
$headerColor = "green"

function LL {
    param ($dir = ".", $all = $false)

    new-object System.IO.FileInfo $dummy

    $origFg = "white"
    if ( $all ) { $toList = ls -force $dir }
    else { $toList = ls $dir }

    foreach ($Item in $toList)  {
        Switch ($Item.Extension)  {
            ".Exe" {$host.ui.rawui.foregroundColor = "Yellow"}
            ".cmd" {$host.ui.rawui.foregroundColor = "Red"}
            ".msh" {$host.ui.rawui.foregroundColor = "Red"}
            ".vbs" {$host.ui.rawui.foregroundColor = "Red"}
            Default {$host.ui.rawui.foregroundColor = $origFg}
        }
##        if ($item.Mode.StartsWith("d")) {$host.ui.rawui.foregroundColor = "White"}
        if ($item.PSIsContainer) {$host.ui.rawui.foregroundColor = "White"}
        $item
    }
    $host.ui.rawui.foregroundColor = $origFg
}

function CheckDummy{
param($name,$otherProp) 
    $origFg = $host.ui.rawui.foregroundColor
    if($name -ne $dummy){
        $host.ui.rawui.foregroundColor = $origFg
        $otherProp
    }
    else{
    $host.ui.rawui.foregroundColor = $headerColor
    $null
    }

}

function LLNew{

$a = @{Name="Mode";Expression = {CheckDummy $_.Name $_.Mode; }},
     @{Name="LastWriteTime";Expression = {CheckDummy $_.Name $_.LastWriteTime}},
     @{Name="Length";Expression = {CheckDummy $_.Name$_.Length}},
     @{Name="Name";Expression = {CheckDummy $_.Name $_.Name}}

LL | ft $a
}

可能不值得获得标题颜色,但必须尝试:)

顺便说一句,更容易获得你想要的着色方法如下:

ls | %{

 if($_.extension -match ".exe"){
        [console]::ForegroundColor="green"; $_;
 } else {
        [console]::ForegroundColor="white"; $_;
 }

}

答案 2 :(得分:0)

正在进行中......

这由PowerShell的类型格式化系统处理。它们的定义存储在XML文件中。您可以使用以下步骤更改这些格式规则。

首先,获取当前规则的副本。您可以使用两种方法。

## 1, export the rules (but the result will not be nicely formatted XML, so needs some cleanup)
Get-FormatData -TypeName FileSystemTypes | Export-FormatData test.format.ps1xml

## 2, grab the source XML directly (be sure to remove the signature from the bottom of the file)
Copy-Item C:\Windows\System32\WindowsPowerShell\v1.0\FileSystem.format.ps1xml .

接下来,我们需要修改规则来进行着色。 (一旦我弄清楚所需的编辑,我需要回到这里。)

## TODO: modify the formatting from controls to direct host output

最后,我们的配置文件会在加载PowerShell时更新格式数据。

Update-FormatData -PrependPath test.format.ps1xml