PowerShell/docs/write-animated.md
2024-05-19 10:25:56 +02:00

96 lines
2.5 KiB
Markdown

Script: *write-animated.ps1*
========================
This PowerShell script writes text centered and animated to the console.
Parameters
----------
```powershell
PS> ./write-animated.ps1 [[-text] <String>] [[-speed] <Int32>] [<CommonParameters>]
-text <String>
Specifies the text line to write ("Welcome to PowerShell" by default)
Required? false
Position? 1
Default value Welcome to PowerShell
Accept pipeline input? false
Accept wildcard characters? false
-speed <Int32>
Specifies the animation speed per character (10ms by default)
Required? false
Position? 2
Default value 10
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
PS> ./write-animated.ps1
(watch and enjoy)
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.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
}
```
*(generated by convert-ps2md.ps1 using the comment-based help of write-animated.ps1 as of 05/19/2024 10:25:27)*