Skip to content

Instantly share code, notes, and snippets.

@Daethyra
Created October 24, 2024 18:17
Show Gist options
  • Save Daethyra/32ad76daefb415667cb187724bf62a18 to your computer and use it in GitHub Desktop.
Save Daethyra/32ad76daefb415667cb187724bf62a18 to your computer and use it in GitHub Desktop.
# Set up the console colors - black background with dark green text for that Matrix feel
$host.UI.RawUI.BackgroundColor = 'Black' # Sets terminal background to black
$host.UI.RawUI.ForegroundColor = 'DarkGreen' # Sets text color to dark green
Clear-Host # Clears the terminal screen (like cls in CMD or clear in bash)
# Define the characters that can appear in our digital rain
# Includes numbers, letters, and some Japanese-looking characters for style
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZヲアウエオカキケコサシスセソタツテナニヌネハヒホマミムメモヤユラリワ'
# Get the width of the terminal window - we'll need this to know how many "rain streams" to create
$width = $host.UI.RawUI.WindowSize.Width
# Create two arrays to track our rain streams:
$streamLengths = @() # How long each vertical stream of characters will be
$positions = @() # Current position of each stream (negative means it hasn't started falling yet)
# Initialize our rain streams
# We create one stream for each column in the terminal width
for ($i = 0; $i -lt $width; $i++) {
# Each stream gets a random length between 5 and 15 characters
$streamLengths += Get-Random -Minimum 5 -Maximum 15
# Each stream starts at a random negative position
# This creates a staggered falling effect at the start
# -1 * (random number between 1 and 20) means streams start above the screen
$positions += -1 * (Get-Random -Minimum 1 -Maximum 20)
}
# Main animation loop - runs forever until user hits Ctrl+C
while ($true) {
# Get current cursor position in the terminal
$curPos = [System.Console]::CursorTop
# If we've reached the bottom of the terminal, move back to top
# The -2 buffer prevents overflow issues
if ($curPos -gt ($host.UI.RawUI.WindowSize.Height - 2)) {
[System.Console]::SetCursorPosition(0, 0)
}
# Start with a blank line full of spaces
$line = " " * $width
# Process each column in our terminal width
for ($i = 0; $i -lt $width; $i++) {
# Check if this stream is currently visible on screen
# position >= 0 means the stream has entered the screen from above
# position < streamLength means we haven't finished drawing this stream
if ($positions[$i] -ge 0 -and $positions[$i] -lt $streamLengths[$i]) {
# Pick a random character from our character set
$charIndex = Get-Random -Minimum 0 -Maximum $chars.Length
# Replace the space at position i with our random character
# Remove removes the character at position i
# Insert puts our new character at position i
$line = $line.Remove($i, 1).Insert($i, $chars[$charIndex])
}
# Move this stream down by one position
$positions[$i]++
# If this stream has fallen past its length plus some extra space
# Reset it to start falling from above again
if ($positions[$i] -gt $streamLengths[$i] + 10) {
# Give it a new random length
$streamLengths[$i] = Get-Random -Minimum 5 -Maximum 15
# Move it back above the screen
$positions[$i] = -1 * (Get-Random -Minimum 1 -Maximum 20)
}
}
# Display the current line we've built
Write-Host $line
# Wait 100 milliseconds before next update
# This controls the speed of the animation and reduces CPU usage
Start-Sleep -Milliseconds 100
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment