当使用get-content回显到屏幕时,如何启用powershell解释ansi颜色代码? [英] How do you enable powershell to interpret ansi color codes when using get-content to echo to the screen?

查看:215
本文介绍了当使用get-content回显到屏幕时,如何启用powershell解释ansi颜色代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志文件,其中包含围绕各种文本的ansi颜色代码。我使用powershell语言命令将其回显到控制台:

I have a log file that contains ansi color codes around various text. I'm echoing it to the console using the powershell language command:

get-content logfile.log -wait

这样我就可以看到最新的日志更改。但是,所有的ansi颜色代码都显示为文本字符,例如:

so I can see the latest log changes. However, all the ansi color codes show up as text characters like:

 Esc[90mEsc[39m

如何在Powershell窗口中将它们解释为颜色代码?

How do you get them intepreted as color codes in the powershell window?

对powershell语言还不是很熟悉,是否有powershell命令或编码选项可以处理?我已经阅读了各种powershell文档,但在这些ansi代码中却找不到任何内容。

Not being very familiar with the powershell language yet, is there a powershell command or encoding option to handle this? I've read various powershell docs but haven't found anything in them re these ansi codes.

推荐答案

您可以翻译ANSI通过在ESC处分割文本并将颜色转换为 Write-Host .... -Forground< color> 指令来对颜色进行转义代码。

You can translate the ANSI escape codes for colors by splitting the text at ESC and translating the colors into Write-Host .... -Forground <color> instructions.

function Open-Colored([String] $Filename)
  { Write-Colored(cat -Raw $Filename) }

function Write-Colored([String] $text)
  { # split text at ESC-char
    $split = $text.Split([char] 27)
    foreach ($line in $split)
      { if ($line[0] -ne '[')
          { Write-Host $line -NoNewline }
        else
          { if     (($line[1] -eq '0') -and ($line[2] -eq 'm')) { Write-Host $line.Substring(3) -NoNewline }
            elseif (($line[1] -eq '3') -and ($line[3] -eq 'm'))
              { # normal color codes
                if     ($line[2] -eq '0') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Black       }
                elseif ($line[2] -eq '1') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkRed     }
                elseif ($line[2] -eq '2') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkGreen   }
                elseif ($line[2] -eq '3') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkYellow  }
                elseif ($line[2] -eq '4') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkBlue    }
                elseif ($line[2] -eq '5') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkMagenta }
                elseif ($line[2] -eq '6') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkCyan    }
                elseif ($line[2] -eq '7') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Gray        }
              }
            elseif (($line[1] -eq '3') -and ($line[3] -eq ';') -and ($line[5] -eq 'm'))
              { # bright color codes
                if     ($line[2] -eq '0') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor DarkGray    }
                elseif ($line[2] -eq '1') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Red         }
                elseif ($line[2] -eq '2') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Gree        }
                elseif ($line[2] -eq '3') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Yellow      }
                elseif ($line[2] -eq '4') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Blue        }
                elseif ($line[2] -eq '5') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Magenta     }
                elseif ($line[2] -eq '6') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Cyan        }
                elseif ($line[2] -eq '7') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor White       }
              }
          }
      }
  }

用法:

Open-Colored .\myColoredLogfile.log

这篇关于当使用get-content回显到屏幕时,如何启用powershell解释ansi颜色代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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