【问题标题】:Is there a way to center text in PowerShell有没有办法在 PowerShell 中使文本居中
【发布时间】:2019-11-15 17:43:47
【问题描述】:

我们如何在 PowerShell 中使文本居中? WindowWidth 显然不存在,所以有没有办法让文本居中?

我们想要这个输出:

    *
   ***
  *****
 *******

所以我写了

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

但是我们得到了

*
***
*****
*******

【问题讨论】:

  • 你需要空白
  • 我们不能将“blanks”合并到for循环中吗?
  • 查看$host自动变量。

标签: powershell


【解决方案1】:
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 '*****'

【讨论】:

  • 非常感谢您的帮助
  • 你能解开它并解释一下吗?
【解决方案2】:

您可以计算出需要添加的空格,然后按如下方式包含:

$Width = 3

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

【讨论】:

    【解决方案3】:

    我玩得很开心,并在此基础上编写了一些代码,它制作了一个框并将其中的文本居中。 我相信有人可以制作一个更干净的版本,但这做得很好:)

    # ----------------------------------------------------------------------------------
    #
    # 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                                    #
    #                                                                                          #
    #                                                                                          #
    #                                                                                          #
    #                                                                                          #
    # ---------------------------------------------------------------------------------------- #
    

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2020-07-27
      • 2021-09-29
      • 2012-10-28
      相关资源
      最近更新 更多