From d9f5847aedd468eae44636185285fc6f37c5e43e Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Sun, 21 Apr 2024 17:23:21 +0200 Subject: [PATCH] Added write-centered.ps1 --- scripts/write-centered.ps1 | 33 +++++++++++++++++++++++++++++++++ scripts/write-headline.ps1 | 7 ++++--- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 scripts/write-centered.ps1 diff --git a/scripts/write-centered.ps1 b/scripts/write-centered.ps1 new file mode 100644 index 00000000..75ff499f --- /dev/null +++ b/scripts/write-centered.ps1 @@ -0,0 +1,33 @@ +<# +.SYNOPSIS + Writes text centered +.DESCRIPTION + This PowerShell script writes the given text centered to the console. +.PARAMETER text + Specifies the text to write +.EXAMPLE + PS> ./write-headline.ps1 "Hello World" + Hello World +.LINK + https://github.com/fleschutz/PowerShell +.NOTES + Author: Markus Fleschutz | License: CC0 +#> + +param([string]$text = "") + +try { + if ($text -eq "") { $text = Read-Host "Enter the text to write" } + + $ui = (Get-Host).ui + $rui = $ui.rawui + [int]$numSpaces = ($rui.MaxWindowSize.Width - $text.Length) / 2 + + [string]$spaces = "" + for ([int]$i = 0; $i -lt $numSpaces; $i++) { $spaces += " " } + Write-Host "$spaces$text" + exit 0 # success +} catch { + "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} \ No newline at end of file diff --git a/scripts/write-headline.ps1 b/scripts/write-headline.ps1 index d0d3015c..5d660374 100644 --- a/scripts/write-headline.ps1 +++ b/scripts/write-headline.ps1 @@ -1,10 +1,10 @@ <# .SYNOPSIS - Writes text as headline + Writes a headline .DESCRIPTION This PowerShell script writes the given text as a headline. .PARAMETER text - Specifies the headline text + Specifies the text to write .EXAMPLE PS> ./write-headline.ps1 "Hello World" @@ -17,8 +17,9 @@ #> param([string]$text = "") + try { - if ($text -eq "") { $text = Read-Host "Enter the headline text" } + if ($text -eq "") { $text = Read-Host "Enter the text to write" } Write-Host "`n* $text *" -foregroundColor green [int]$len = $text.Length