center text

Tagged: 

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Tony Antony Tony Antony 2 months ago.

  • Author
    Posts
  • #62064
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Is there a way to center text horizontally?

    Also, let's say I have

    $String = "Some String"

    Instead of

    Write-Host $String -foregroundcolor "Green"

    to display the text in Green,

    How can I display

    $String

    in Green?

    Thank you,

    Tony

  • #62067
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Write-Host is the only way to use colors, because it draws pixels directly on the screen rather than passing objects through a pipeline. And, because PowerShell's main use cases weren't presenting formatted text on screen, there's no built-in way of telling Write-Host to center that text. You'd have to dig around in the $host object, I expect, figure out the current window width in character, and calculate your own padding to display something centered.

    Creating user interfaces has always been time-consuming and not very much fun :).

  • #62071
    Profile photo of James Bernie
    James Bernie
    Participant

    The center option is interesting & where you might need to get creative (I can enjoy these challenges). I have a script which displays a number that will be 1-3 characters in length at the beginning of a line, as there is additional text after these numbers I have $pad1, $pad2 & $pad3 variables that will add 1, 2 or 3 spaces after the number so the total character count is always 4 and the extra text is aligned properly.

    If you know how the text is going to appear on screen every time you could create a variable with enough spaces to center the text. Mathematically, the screen is x characters across, if you count the amount of characters (y) in the text you wish to center you could then take (x – y) / 2 = # of characters you need to move the text in to center it.

    80 characters wide, 20 characters of text, (80 – 20) / 2 = 30 spaces required to indent text

    As you can't have 1/2 a character, you could possibly assume that you will round the number up to the nearest whole number, so you could use something like this: "{0:N0}" -f ((80-19)/2) to get 31.

    Finally and I only found this out by doing web searches just now you can use padleft to add the spaces for you.

    $a = "This is text"
    $a.PadLeft( "{0:N0}" -f ((80 – ($a | measure).count)/2)," ")

    I have a few things going on but basically we are using normal .padleft format which is:
    $a.padleft(# of characters, character to insert) i.e. $a.padleft(40," ")

    Within the () you have: "{0:N0}" -f – a digit with no decimal places, then 80 – the character count of $a and dividing in by 2 on the fly and PowerShell rounds it up for us.

    As to the color question, the following works in PSv5 on Windows 10:

    $test = "Write-host 'this is a test in green' -fore green"
    Write-host "this is a test in white and " -nonewline ; invoke-expression $test

  • #62079
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Thank you all

You must be logged in to reply to this topic.