PowerShell/scripts/write-animated.ps1
2024-05-15 15:25:28 +02:00

41 lines
1.1 KiB
PowerShell
Executable File

<#
.SYNOPSIS
Writes animated text
.DESCRIPTION
This PowerShell script writes text centered and animated to the console.
.PARAMETER text
Specifies the text line to write ("Welcome to PowerShell" by default)
.PARAMETER speed
Specifies the animation speed per character (10ms by default)
.EXAMPLE
PS> ./write-animated.ps1
(watch and enjoy)
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$text = "Welcome to PowerShell", [int]$speed = 10) # 10ms
function WriteLine([string]$line) {
[int]$end = $line.Length
$startPos = $HOST.UI.RawUI.CursorPosition
$spaces = " "
[int]$termHalfWidth = 120 / 2
foreach($pos in 1 .. $end) {
$HOST.UI.RawUI.CursorPosition = $startPos
Write-Host "$($spaces.Substring(0, $termHalfWidth - $pos / 2) + $line.Substring(0, $pos))" -noNewline
Start-Sleep -milliseconds $speed
}
Write-Host ""
}
try {
WriteLine $text
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}