Powershell 彩色目录列表在格式范围内不正确

发布于 2024-09-12 23:28:05 字数 2326 浏览 1 评论 0原文

我从 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

那么 Directory 和 Program.exe 都将是红色的,即使 Directory 应该是 DarkCyan。我该如何纠正这个问题?

I got this colored dir script from 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)

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

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

, $regex_opts) $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)

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


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


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

, $regex_opts) $executable = New-Object System.Text.RegularExpressions.Regex('\.(exe|bat|cmd|ps1|psm1|vbs|rb|reg|dll|o|lib)

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


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


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

, $regex_opts) $source = New-Object System.Text.RegularExpressions.Regex('\.(py|pl|cs|rb|h|cpp)

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


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


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

, $regex_opts) $text = New-Object System.Text.RegularExpressions.Regex('\.(txt|cfg|conf|ini|csv|log|xml)

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


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


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

, $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 { $_ } } }

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-09-19 23:28:05

与其在向屏幕显示文本之间调整主机的前景色/背景色,不如使用 Write-Host ,它可以让您对显示的文本有更多的控制(您可以控制何时输出换行符),例如:

$_ | 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}

Rather than twiddling the foreground/background colors of the host in between displaying text to the screen, why don't you use Write-Host which gives you a bit more control over the displayed text (you can control when newlines are output) e.g.:

$_ | 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:

$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}
不气馁 2024-09-19 23:28:05

只是想我会向您指出我发布的这个问题,该问题可以正确输出 Linux 风格的彩色输出和列中的格式。 如何编写排序列表按字典顺序排列在按列列出的网格中?

Just thought I would point you to this question I posted which outputs linux style colored output and format in columns correctly. How to write a list sorted lexicographically in a grid listed by column?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文