代码之家  ›  专栏  ›  技术社区  ›  Nathan Shively-Sanders

PowerShell彩色目录列表格式不正确

  •  3
  • Nathan Shively-Sanders  · 技术社区  · 14 年前

    我是从 http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html :

    function ls {
      $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)
    
      $fore = $Host.UI.RawUI.ForegroundColor
      $compressed = New-Object System.Text.RegularExpressions.Regex('\.(zip|tar|gz|rar)$', $regex_opts)
      $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)$', $regex_opts)
      $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)$', $regex_opts)
      $source = New-Object System.Text.RegularExpressions.Regex('\.(py|pl|cs|rb|h|cpp)$', $regex_opts)
      $text = New-Object System.Text.RegularExpressions.Regex('\.(txt|cfg|conf|ini|csv|log|xml)$', $regex_opts)
    
      Invoke-Expression ("Get-ChildItem $args") |
        %{
          if ($_.GetType().Name -eq 'DirectoryInfo') {
            $Host.UI.RawUI.ForegroundColor = 'DarkCyan'
            $_
            $Host.UI.RawUI.ForegroundColor = $fore
          } elseif ($compressed.IsMatch($_.Name)) {
            $Host.UI.RawUI.ForegroundColor = 'Yellow'
            $_
            $Host.UI.RawUI.ForegroundColor = $fore
          } elseif ($executable.IsMatch($_.Name)) {
            $Host.UI.RawUI.ForegroundColor = 'Red'
            $_
            $Host.UI.RawUI.ForegroundColor = $fore
          } elseif ($text.IsMatch($_.Name)) {
            $Host.UI.RawUI.ForegroundColor = 'Green'
            $_
            $Host.UI.RawUI.ForegroundColor = $fore
          } elseif ($source.IsMatch($_.Name)) {
            $Host.UI.RawUI.ForegroundColor = 'Cyan'
            $_
            $Host.UI.RawUI.ForegroundColor = $fore
          } else {
            $_
          }
        }
    }
    

    它工作得很好,但大多数时候我只想要宽格式的文件名。所以在调用表达式调用之后,我添加了

      Invoke-Expression ("Get-ChildItem $args") |
        %{
          if ($_.GetType().Name -eq 'DirectoryInfo') {
      :
      :
      :
            $_
          }
        } | format-wide -property Name
    }
    

    现在我有一只虫子。只有第二列的颜色是正确的;每列的第一项采用第二列的颜色。例如,如果我有

    > ls
    
    Directory     Program.exe
    

    然后目录和program.exe都将是红色的,即使目录应该是深青色的。我该如何更正?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Nathan Shively-Sanders    14 年前

    与其在向屏幕显示文本之间旋转主机的前景色/背景色,不如使用“写入主机”,这样可以更好地控制显示的文本(可以控制何时输出换行),例如:

    $_ | Out-String -stream | Write-Host -Fore Red
    

    对于广泛的列表使用,您需要自己处理列格式,除非您想更新directoryInfo/fileinfo类型的格式数据XML。如果你不想这样做,那么你可以用你想要的颜色写下每个名字-适当地填充。在最后一列中,将-nonewline参数设置为$false:

    $width =  $host.UI.RawUI.WindowSize.Width
    $cols = 3   
    ls | % {$i=0; $pad = [int]($width/$cols) - 1} `
           {$nnl = ++$i % $cols -ne 0; `
            Write-Host ("{0,-$pad}" -f $_) -Fore Green -NoNewLine:$nnl}
    
        2
  •  2
  •   Community Tales Farias    7 年前

    我只是想告诉你我发布的这个问题,它正确地输出Linux风格的彩色输出和列格式。 How to write a list sorted lexicographically in a grid listed by column?