Is there a way to center text in PowerShell

前端 未结 3 1391
长发绾君心
长发绾君心 2021-01-14 23:46

How can we center text in PowerShell? WindowWidth doesn\'t exist apparently, so is there a way somehow to keep the text centered?

We want this output :<

相关标签:
3条回答
  • 2021-01-14 23:57

    You can calculate the spaces you need to add and then include them as follows:

    $Width = 3
    
    for ($i=1; $i -le 7; $i+=2)
    {   
        Write-Host (' ' * ($width - [math]::floor($i / 2))) ('*' * $i)
    }
    
    0 讨论(0)
  • 2021-01-14 23:59
    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 '*****'
    
    0 讨论(0)
  • 2021-01-15 00:03

    I had a bit of fun and wrote some code based on this, that makes a box and center the text inside. Im sure someone can make a cleaner version, but this do the job just fine :)

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

    This gives an output like this:

    # ---------------------------------------------------------------------------------------- #
    #                                                                                          #
    #                                                                                          #
    #                                                                                          #
    #                                                                                          #
    #                       Lines of .ps1 code in this folder: 6524                            #
    #                                 Press any key to exit                                    #
    #                                                                                          #
    #                                                                                          #
    #                                                                                          #
    #                                                                                          #
    # ---------------------------------------------------------------------------------------- #
    
    0 讨论(0)
提交回复
热议问题