PowerShell/scripts/write-animated.ps1

41 lines
1.1 KiB
PowerShell
Raw Normal View History

2023-10-31 13:03:45 +01:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-09-25 19:43:22 +02:00
Writes animated text
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2024-05-15 15:25:28 +02:00
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)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2024-05-15 15:25:28 +02:00
PS> ./write-animated.ps1
(watch and enjoy)
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-02-06 14:38:58 +01:00
#>
2024-05-15 15:25:28 +02:00
param([string]$text = "Welcome to PowerShell", [int]$speed = 10) # 10ms
2021-02-06 14:38:58 +01:00
2024-05-15 15:25:28 +02:00
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
2021-02-06 14:38:58 +01:00
}
2023-06-15 17:20:52 +02:00
Write-Host ""
2021-02-06 14:38:58 +01:00
}
2023-07-03 21:21:10 +02:00
try {
2024-05-15 15:25:28 +02:00
WriteLine $text
2023-07-03 21:21:10 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2021-02-06 14:38:58 +01:00
}