PowerShell/docs/write-animated.md
2024-11-20 11:52:20 +01:00

2.4 KiB

The write-animated.ps1 Script

This PowerShell script writes text centered and animated to the console.

Parameters

/home/markus/Repos/PowerShell/scripts/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

PS> ./write-animated.ps1
(watch and enjoy)

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.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 as of 11/20/2024 11:52:01)