Search code examples
powershelltextcenter

I need to show a disclaimer file (.TXT) center vertically and horizontally of the screen in red


I need to show a disclaimer file (.TXT) center vertically and horizontally of the screen in red.

$ConfigPath = '.\config.json'
$DisclamerPath = '.\DISCLAIMER.TXT' # *** Disclaimer path ***
IF (Test-Path -Path $ConfigPath -PathType Leaf)
{
    $Content = Get-Content '.\config.json' | ConvertFrom-Json
    IF ($Content.Config.First)
    {
        IF (Test-Path -Path $DisclamerPath -PathType Leaf)
        {
            $DISCL = Get-Content -Path $DisclamerPath
            FOREACH ($LINE IN $DISCL)
            {
                #I try some function like : WriteColor-Core -Text $LINE -Color red 
                #I try directly Write-Host $LINE -BackgroundColor DarkYellow -ForegroundColor Black
                                #I try a function like : centred_text
                $LINE # Error section
            }
            $null = Read-Host ' '
        }
        ELSE
        {
            Write-Host 'The disclaimer file not exist.🛑' -BackgroundColor Red -ForegroundColor Black
        }   

        $Content.Config.First = $false # no quotes otherwise it is interpreted as string
        $Content | ConvertTo-Json | Set-Content '.\config.json' -Encoding utf-8
        pause
    }
    ELSE
    {
        Write-Host 'This file has already been opened for the first time !'
        pause
    }
}
ELSE
{
    Write-Host 'The config file not exist.🛑' -BackgroundColor Red -ForegroundColor Black
    Start-Sleep -Seconds 5
    RETURN
}

Here a disclaimer example :

LEGAL DISCLAIMER

This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified

I try some function like :

WriteColor-Core -Text $LINE -Color red

I try directly:

Write-Host $LINE -BackgroundColor Red -ForegroundColor Black

I try a function like:

centred_text


Solution

  • You have two basic options:

    • Use a GUI library available for your platform, such as WinForms of WPF on Windows.

      • If you go this route, I suggest attempting an implementation yourself (WinForms will be easier), and then ask a focused question about your attempt if you get stuck.
    • Use a terminal-based approach, which, however, hinges on the ability to create a full-screen terminal window on demand.

      • For sophisticated terminal-based formatting, you'll still need a library, such as Terminal.Gui, which isn't the easiest to use from PowerShell, however - see the bottom section of this answer for a basic example of its use.

      • A simplistic approach with limited formatting is shown below, and it assumes that you have Windows Terminal (wt.exe) installed.

    # Windows-only, requires Windows Terminal.
    # Displays a full-screen terminal window with vertically centered
    # text printed in red, read from file .\disclaimer.txt
    # Closes the window when the user presses Enter.
    wt.exe --fullscreen -w $PID (Get-Process -Id $PID).Path -NoProfile {
      $lines = Get-Content .\disclaimer.txt
      $paddingLineCount = ([Console]::WindowHeight - $lines.Count) / 2
      if ($paddingLineCount -gt 0) {
        Write-Host -NoNewline ("`n" * $paddingLineCount)
      }
      $lines | Write-Host -ForegroundColor Red
      Write-Host 'Press Enter to exit.'
      Read-Host
    }