Windows Powershell单行和居中的多色文本

Windows Powershell单行和居中的多色文本,windows,powershell,Windows,Powershell,我有一个powershell函数,需要进行更改。该功能的中心文本在终端然而,我需要能够输出多种颜色的文本在一行。如果我这样做-NoNewLine和做更多的写主机改变颜色。。。然后,它仍然计算终端的宽度,并且仍然添加了与不添加-NoNewLine一样多的填充。基本上,我希望我的文本居中,我希望能够使用多种颜色。用我所拥有的,我只能做每行一种颜色 function WriteCentered([String] $text, $color = $null) { $width = [int](G

我有一个powershell函数,需要进行更改。该功能的中心文本在终端然而,我需要能够输出多种颜色的文本在一行。如果我这样做-NoNewLine和做更多的写主机改变颜色。。。然后,它仍然计算终端的宽度,并且仍然添加了与不添加-NoNewLine一样多的填充。基本上,我希望我的文本居中,我希望能够使用多种颜色。用我所拥有的,我只能做每行一种颜色

function WriteCentered([String] $text, $color = $null)
{
    $width = [int](Get-Host).UI.RawUI.BufferSize.Width
    $twidth = [int]$text.Length
    $offset = ($width / 2) - ($twidth / 2)
    $newText = $text.PadLeft($offset + $twidth)

    if($color)
    {
        Write-Host $newText -ForegroundColor $color
    }        
    else
    {
        Write-Host $newText 
    }


}   
我添加了更多的IF条件,我更改了填充计算,我很难把它做得恰到好处

PowerShell模块在一条线上输出多种颜色方面已经做得很好了。您可以直接从GitHub下载并使用
import Module\PSWriteColor.psd1导入它,或者直接使用
install Module-Name PSWriteColor
从PowerShell库安装它

简而言之,语法是
Write Color-Text“GreenText”、“RedText”、“BlueText”-Color Green、Red、Blue
。因此,我们需要在
[String[]]$Text
参数前面加上一个包含必要空格的字符串,以便在屏幕上居中显示消息,并相应地在
[ConsoleColor[]]$color
参数前面加上颜色

这里有一个用于居中的小辅助函数

#Requires -Modules @{ ModuleName="PSWriteColor"; ModuleVersion="0.8.5" }
function WriteColor-Centered {
param(
    [Parameter(Mandatory=$true)][string[]]$Text,
    [Parameter(Mandatory=$true)][ConsoleColor[]]$Color
)
    $messageLength = 0
    $Text | ForEach-Object { $messageLength += $_.Length }

    [String[]] $centeredText = "{0}" -f (' ' * (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Floor($messageLength / 2))))
    $centeredText += $Text

    [ConsoleColor[]]$OutColor = @([ConsoleColor]::White)
    $OutColor += $Color

    Write-Color -Text $centeredText -Color $OutColor
    # Alt.: use WriteColor-Core, see below
    # WriteColor-Core -Text $centeredText -Color $OutColor
}
我从中复制了空格计算

编辑:有人问我是否可以在不导入模块的情况下完成这项工作。老实说,我现在觉得有点脏,因为我进入了一个编写良好的模块的源代码,从中剥离了所有的功能和错误处理,并粘贴在这里

无论如何,如果您在上面的包装器函数中替换调用
Write Color
,并调用以下
WriteColor Core
,那么您可以不用加载PSWriteColor模块

function WriteColor-Core {
param(
    [Parameter(Mandatory=$true)][string[]]$Text,
    [Parameter(Mandatory=$true)][ConsoleColor[]]$Color
)
    # Fallback defaults if one of the values isn't set
    $LastForegroundColor = [console]::ForegroundColor
    # The real deal coloring
    for ($i = 0; $i -lt $Text.Count; $i++) {
        $CurrentFGColor = if ($Color[$i]) { $Color[$i] } else { $LastForegroundColor }
        $WriteParams = @{
            NoNewLine       = $true
            ForegroundColor = $CurrentFGColor
        }
        Write-Host $Text[$i] @WriteParams
        # Store last color set, in case next iteration doesn't have a set color
        $LastForegroundColor = $CurrentFGColor
    }

    Write-Host
}

如果你想让合成字符串居中,同时在一行上有多种颜色,那么你必须传递多个字符串和一个平行的颜色数组。这不是你想要的,但可以调整:使用嵌入在单个字符串中的着色指令。有没有办法在没有模块的情况下实现这一点?我希望这个脚本能够开箱即用。我从PSWriteColor复制粘贴了一些代码。现在,它应该可以在没有模块的情况下工作。