【问题标题】:Changing Verbose colors in PowerShell 7.2在 PowerShell 7.2 中更改详细颜色
【发布时间】:2021-12-28 19:03:53
【问题描述】:

我希望下面的代码使用我的默认前景色打印详细文本:

$Host.PrivateData.VerboseForegroundColor = [console]::ForegroundColor
Write-Verbose 'Test' -Verbose

但是,它照常打印黄色文本。更改错误前景色 确实 工作:

$Host.PrivateData.ErrorForegroundColor = [console]::ForegroundColor
Write-Error 'test'

我发现绕过这个的唯一方法是这样做:

Write-Verbose 'Test' -Verbose *>&1 | Write-Host

但这并没有真正改变详细的颜色,它只是强制它使用 Write-Host 作为默认文本直接打印到控制台主机。我知道 Write-Host 确实可以让您将消息颜色更改为您想要的任何颜色,但这并不是一个理想的解决方案。

【问题讨论】:

  • $Host.PrivateData.VerboseForegroundColor = [console]::ForegroundColor.ToString()?
  • (get-host).privatedata.verboseforegroundcolor = 'Gray' 在 ps 5 中有效,但在 ps 7 中无效。使用 $psstyle.Foreground.White 不起作用。我的测试是echo hi > there; rm there -v
  • @JosefZ 恐怕没什么区别。

标签: powershell colors powershell-core verbose


【解决方案1】:

Powershell 7.2+$Host.PrivateData 中设置样式的方式不正确。它是为了向后兼容。详情见全新about_*页面:about_ANSI_Terminals

你想要

$PSStyle.Formatting.Verbose = $PSStyle.Foreground.FromRgb(0x34f2aa)

查看文档,它添加了一堆选项:粗体、闪烁、隐藏、反向、斜体、下划线、文件信息......about_ANSI_Terminals

测试一下

function testVerbose { 
   [CmdletBinding()]
   param( [Parameter() ]$x )
   "$x"
   $X.GetType().FullName | write-verbose
   
}

testVerbose 34.5 -Verbose                                             ; 

$PSStyle.Formatting.Verbose = $PSStyle.Foreground.FromRgb(0x34f2aa)

testVerbose 34.5 -Verbose

查看 ansi 转义

查看ANSI 控制字符的一种方法是替换“``e”。

# I saved the value before changing it
$originalVerbose -replace "`e", ''

$PSStyle.Formatting.Verbose -replace "`e", ''

注意:切换到24bit-color ANSI escape sequence

打印所有控制字符

Format-ControlChar 将所有控制字符转换为其Symbol 版本,而不仅仅是e,使值可以安全地通过管道传输。

这很简单,只需将 0x2400 添加到代码点即可。 compart.com/block/U+2400

?> "$($PSStyle.Background.BrightCyan)Power$($PSStyle.Underline)$($PSStyle.Bold)Shell$($PSStyle.Reset)"
    | Format-ControlChar

␛[106mPower␛[4m␛[1mShell␛[0m

【讨论】:

    猜你喜欢
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多