Skip to content

Instantly share code, notes, and snippets.

@mdgrs-mei
Last active April 22, 2024 11:12
Show Gist options
  • Save mdgrs-mei/11121aceb7e147dbe6ab4ab8d2abcd35 to your computer and use it in GitHub Desktop.
Save mdgrs-mei/11121aceb7e147dbe6ab4ab8d2abcd35 to your computer and use it in GitHub Desktop.
Cats on the terminal titles
#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
@joshua-russell
Copy link

joshua-russell commented Jul 2, 2023

I see, the black cat was introduced in Win11. In Win10 it is rendered as a cat with a square next to it haha!
Win10 users might change the caution cat to something like 😿or🙀. I think this would work:

IF ([System.Environment]::OSVersion.Version.Major -eq 10) {$catB = '🙀'}

I wanted to experiment with adding some terrain along the road. Something like this:

    $roadLength = 6

    function makeRoad {
        $list = '.','🌵','🌳'
        1..10 | % {$list += '_'}
        $segment = $null
        1..3 | % {$segment += $list[$(Get-Random -Minimum 0 -Maximum $list.count)]}
        $segment
    }
    1..$($roadlength*.5) | % {$road1+=makeroad}
    1..$($roadlength*.5) | % {$road2+=makeroad}
# Then set the title up like this: 
    $title = $road1 + $mainTitle + $road2

However, when the cat is next to one of the emojis, it becomes the � symbol. I'm unsure why that happens, or how to fix it.

@mdgrs-mei
Copy link
Author

mdgrs-mei commented Jul 2, 2023

@joshua-russell
Oh, I was not sure the black cat was not available on Win10. Thank you!

Currently, the road and the main title are assumed to be 2 byte characters, but those emojis are 4 bytes. That is the reason for the � symbol you saw. I think I can add emoji support for the road.

Adding terrain is a great idea by the way!

@HotCakeX
Copy link

HotCakeX commented Jul 2, 2023

This is cute, thanks

@mdgrs-mei
Copy link
Author

@HotCakeX Glad you liked it!

@mdgrs-mei
Copy link
Author

@joshua-russell I've updated the code to support emojis for the street. You can add emojis to $streetParts, but I recommend that we only use ascii characters because the animation is more stable.

@mdgrs-mei
Copy link
Author

Added street parts.

TerminalCats2

@mdgrs-mei
Copy link
Author

Cycling version.

TerminalCycle

@ronascentes
Copy link

Amazing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment