-
-
Save DailenG/4ca3b738d6832a21b9112156246c3d03 to your computer and use it in GitHub Desktop.
Cats on the terminal titles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Modules DynamicTitle | |
$promptJob = Start-DTJobPromptCallback { | |
if ($null -eq $script:terminalCatPromptFrame) { | |
$script:terminalCatPromptFrame = 0 | |
} | |
$script:terminalCatPromptFrame++ | |
$isInError = $false | |
if ($Error[0]) { | |
$isInError = -not ($Error[0].Equals($script:terminalCatLastError)) | |
$script:terminalCatLastError = $Error[0] | |
} | |
$isInError, $script:terminalCatPromptFrame | |
} | |
$initializationScript = { | |
$mainTitle = ' PowerShell ' | |
$characters = @( | |
'🐈' | |
'🐈⬛' | |
) | |
$caution = '❕' | |
$streetParts = @( | |
'_._._.' | |
'_.-._' | |
) | |
$streetLength = 2 | |
function GetCharacter { | |
$characters | Get-Random | |
} | |
function GetStreet { | |
$street = '' | |
foreach ($i in 1..$streetLength) { | |
$street += $streetParts | Get-Random | |
} | |
$street | |
} | |
function GetWaitFrame { | |
Get-Random -Minimum 0 -Maximum 100 | |
} | |
$character = GetCharacter | |
$streetL = GetStreet | |
$streetR = GetStreet | |
$waitFrame = GetWaitFrame | |
$characterPos = 1 | |
$lastPromptFrame = 0 | |
$isCaution = $false | |
} | |
$scriptBlock = { | |
param($promptJob) | |
$isInError, $promptFrame = Get-DTJobLatestOutput $promptJob | |
if ($isInError -and ($promptFrame -ne $script:lastPromptFrame)) { | |
$script:isCaution = $true | |
} | |
if (-not $isInError) { | |
$script:isCaution = $false | |
} | |
$script:lastPromptFrame = $promptFrame | |
$title = $streetL + $mainTitle + $streetR | |
if ($script:waitFrame -gt 0) { | |
$script:waitFrame-- | |
$script:isCaution = $false | |
$title | |
return | |
} | |
$stringInfo = [System.Globalization.StringInfo]::new($title) | |
$length = $stringInfo.LengthInTextElements | |
$characterIndex = $length - 1 - $script:characterPos | |
if ($script:isCaution) { | |
if ($characterIndex -ge 1) { | |
$characterIndex -= 1 | |
$character = $caution + $character | |
} else { | |
$character = $character + $caution | |
} | |
$title = $stringInfo.SubstringByTextElements(0, $characterIndex) + $character + $stringInfo.SubstringByTextElements($characterIndex + 2) | |
} else { | |
$title = $stringInfo.SubstringByTextElements(0, $characterIndex) + $character + $stringInfo.SubstringByTextElements($characterIndex + 1) | |
$script:characterPos += 1 | |
if ($script:characterPos -ge $length) { | |
$script:characterPos = 1 | |
$script:waitFrame = GetWaitFrame | |
$script:character = Getcharacter | |
} | |
} | |
$title | |
} | |
$params = @{ | |
InitializationScript = $initializationScript | |
ScriptBlock = $scriptBlock | |
ArgumentList = $promptJob | |
} | |
Start-DTTitle @params |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment