Powershell彩色目录列表在格式范围内不正确 [英] Powershell colored directory listing is incorrect with format-wide

查看:151
本文介绍了Powershell彩色目录列表在格式范围内不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从获得了这个彩色dir脚本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调用之后,我添加了

It works great, but I most of the time I want only the file names, in wide format. So after the invoke-expression call, I added

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

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

Now I have a bug. Only the colour of the second column is correct; the first item in each column takes the colour of the item in the second column. For example, if I have

> ls

Directory     Program.exe

然后Directory和Program.exe都将是红色的,即使Directory应该是DarkCyan。

Then both Directory and Program.exe will be red, even though Directory is supposed to be DarkCyan. How can I correct this?


推荐答案

不是在显示文本到屏幕之间扭曲主机的前景/背景颜色,你为什么不使用Write-Host,它给你更多的控制显示的文本(你可以控制何时换行符输出)例如:

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

And for the wide listing use, you will need to handle the column formatting yourself unless you want to update the format data XML for the DirectoryInfo/FileInfo types.  If you don't want to do that, then you can write out each name - padded out appropriately - with the desired color.  On the last column, set the -NoNewLine param to $false:

对于宽列表使用,您需要处理列格式化自己,除非你想更新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}

这篇关于Powershell彩色目录列表在格式范围内不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆