有没有一种方法可以在PowerShell中居中文本?

4
我们如何在PowerShell中居中文本?显然,WindowWidth不存在,那么有没有什么办法可以让文本居中呢?
我们想要的输出结果是:
    *
   ***
  *****
 *******

所以我写了下面的代码

for ($i=1; $i -le 7; $i+=2)
{
  write-host("*" * $i)
}

但是我们得到
*
***
*****
*******

1
你需要 _blanks_。 - B001ᛦ
我们不能将“blanks”合并到for循环中吗? - Darwin Ameli
看一下 $host 自动变量。 - Maximilian Burszley
3个回答

7
function Write-HostCenter { param($Message) Write-Host ("{0}{1}" -f (' ' * (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Floor($Message.Length / 2)))), $Message) }

Write-HostCenter '*'
Write-HostCenter '***'
Write-HostCenter '*****'

非常感谢您的帮助。 - Darwin Ameli
你能否简单地阐述一下它的含义,让我们可以明白它吗? - Darwin Ameli

4

我觉得很有趣,基于这个想法写了一些代码,使一个框被创建并使文本居中。

我相信有人可以编写更简洁的版本,但这个版本已经能够完成工作 :)

# ----------------------------------------------------------------------------------
#
# Script functions
#
# ----------------------------------------------------------------------------------
function MakeTopAndButtom
{
    $string = "# "
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + "-"
    }
    $string = $string + " #"

    return $string
}

function MakeSpaces
{
    $string = "# "
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + " "
    }
    $string = $string + " #"
    return $string
}

function CenterText
{
    param($Message)

    $string = "# "

    for($i = 0; $i -lt (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Max(0, $Message.Length / 2))) - 4; $i++)
    {
        $string = $string + " "
    }

    $string = $string + $Message

    for($i = 0; $i -lt ($Host.UI.RawUI.BufferSize.Width - ((([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Max(0, $Message.Length / 2))) - 2 + $Message.Length)) - 2; $i++)
    {
        $string = $string + " "
    }

    $string = $string + " #"
    return $string

}

function LinesOfCodeInCorrentFolder
{
    return (gci -include *.ps1 -recurse | select-string .).Count
}

$MakeTopAndButtom = MakeTopAndButtom
$MakeSpaces = MakeSpaces
$lines = LinesOfCodeInCorrentFolder



# ----------------------------------------------------------------------------------
#
# Run
#
# ----------------------------------------------------------------------------------
$MakeTopAndButtom
$MakeSpaces
$MakeSpaces
$MakeSpaces
$MakeSpaces

CenterText "Lines of .ps1 code in this folder: $($lines)"
CenterText "Press any key to exit"

$MakeSpaces
$MakeSpaces
$MakeSpaces
$MakeSpaces
$MakeTopAndButtom

Read-Host

这将产生如下输出结果:
# ---------------------------------------------------------------------------------------- #
#                                                                                          #
#                                                                                          #
#                                                                                          #
#                                                                                          #
#                       Lines of .ps1 code in this folder: 6524                            #
#                                 Press any key to exit                                    #
#                                                                                          #
#                                                                                          #
#                                                                                          #
#                                                                                          #
# ---------------------------------------------------------------------------------------- #

3
您可以计算需要添加的空格,然后按以下方式包含它们:
$Width = 3

for ($i=1; $i -le 7; $i+=2)
{   
    Write-Host (' ' * ($width - [math]::floor($i / 2))) ('*' * $i)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接