Added color options to The Matrix Screensaver.
This commit is contained in:
@@ -4,106 +4,180 @@
|
|||||||
# Author: Justin Oros
|
# Author: Justin Oros
|
||||||
# Source: https://github.com/JustinOros
|
# Source: https://github.com/JustinOros
|
||||||
|
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import curses
|
import curses
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
import argparse
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
# Main function that runs the Matrix screensaver
|
# Available colors (excluding black and white for regular text)
|
||||||
def matrix_rain(stdscr, color_pair):
|
COLOR_NAMES = ['red', 'green', 'yellow', 'blue', 'purple', 'cyan']
|
||||||
# Hide the cursor
|
COLOR_OPTIONS = COLOR_NAMES + ['rgb'] # 'rgb' cycles through colors every 5 seconds
|
||||||
curses.curs_set(0)
|
|
||||||
# Start color support and use default colors
|
|
||||||
curses.start_color()
|
|
||||||
curses.use_default_colors()
|
|
||||||
|
|
||||||
# Initialize color pair for the text (foreground) color
|
# Mapping from color names to curses color constants
|
||||||
curses.init_pair(1, color_pair, -1) # Text color, background color (default black)
|
COLOR_MAP = {
|
||||||
|
'black': curses.COLOR_BLACK,
|
||||||
|
'red': curses.COLOR_RED,
|
||||||
|
'green': curses.COLOR_GREEN,
|
||||||
|
'yellow': curses.COLOR_YELLOW,
|
||||||
|
'blue': curses.COLOR_BLUE,
|
||||||
|
'purple': curses.COLOR_MAGENTA,
|
||||||
|
'cyan': curses.COLOR_CYAN,
|
||||||
|
'white': curses.COLOR_WHITE,
|
||||||
|
}
|
||||||
|
|
||||||
# String of characters to simulate the "Matrix" rain effect
|
def matrix_rain(stdscr, color_mode, timeout=None):
|
||||||
|
# Initialize curses settings
|
||||||
|
curses.curs_set(0) # Hide the cursor
|
||||||
|
curses.start_color() # Enable color functionality
|
||||||
|
curses.use_default_colors() # Use default terminal background
|
||||||
|
|
||||||
|
# Initialize color pairs for each available color
|
||||||
|
for i, name in enumerate(COLOR_NAMES):
|
||||||
|
curses.init_pair(i + 1, COLOR_MAP[name], -1) # Foreground color, default background
|
||||||
|
|
||||||
|
# Initialize variables for color cycling (for 'rgb' mode)
|
||||||
|
color_index = 0
|
||||||
|
current_color = COLOR_NAMES[color_index]
|
||||||
|
next_color = current_color
|
||||||
|
last_color_change = time.time()
|
||||||
|
|
||||||
|
# Characters used in the Matrix rain effect (mix of ascii and Greek symbols)
|
||||||
chars = (
|
chars = (
|
||||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#$%^&*()[]{}|;:,.<>?/~!_`'\"\\±§€£¥∑πΩΔ∫ƒ"
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||||
"αβγδεζηθικλμνξοπρστυφχψω«»≠∞≡∧∨⊕∈∩∪≈αβγπ"
|
"@#$%^&*()[]{}|;:,.<>?/~!_`'\"\\"
|
||||||
"ϲπΔΘΩ≺⊂⊆≿⊤⊥⊢∝⇔⇒⇑⇓↔↕⊗⊙⊢⊣"
|
"αβγδεζηθικλμνξοπρστυφχψω∑πΩΔ∫ƒ∞≡∧∨⊕⊗⊙"
|
||||||
"αβγδεζηθικλμνξοπρστυφχψω«»≠∞≡∧∨⊕∈∩∪≈αβγπ"
|
|
||||||
"ϲπΔΘΩ≺⊂⊆≿⊤⊥⊢∝⇔⇒⇑⇓↔↕⊗⊙⊢⊣"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# List to store the current position of each falling character column
|
# Signature string that appears as an easter egg every 30 seconds
|
||||||
columns = []
|
signature = "Justin Oros"
|
||||||
|
signature_columns = {} # Tracks column and starting Y-position of signature
|
||||||
|
signature_visible = False
|
||||||
|
signature_visible_until = 0
|
||||||
|
last_signature_time = time.time()
|
||||||
|
|
||||||
# Infinite loop to keep the screensaver running
|
start_time = time.time()
|
||||||
while True:
|
columns = [] # Y positions of falling characters for each column
|
||||||
# Get the current screen size (height and width)
|
column_speeds = [] # Speed at which each column falls (some variation)
|
||||||
sh, sw = stdscr.getmaxyx()
|
column_colors = [] # Color of each column (for 'rgb' cycling)
|
||||||
|
|
||||||
# If the terminal size changes, update the columns list
|
try:
|
||||||
if len(columns) != sw:
|
while True:
|
||||||
columns = [random.randint(0, sh - 1) for _ in range(sw)]
|
now = time.time()
|
||||||
|
if timeout and (now - start_time) > timeout:
|
||||||
|
break # Exit if timeout reached
|
||||||
|
|
||||||
# Clear the screen before drawing the next frame
|
sh, sw = stdscr.getmaxyx() # Get screen height and width
|
||||||
stdscr.erase()
|
|
||||||
|
|
||||||
# Loop over each column in the terminal window
|
# Initialize columns if screen width changes
|
||||||
for i in range(sw):
|
if len(columns) != sw:
|
||||||
# Create a string of 30 random characters for the falling text
|
columns = [random.randint(0, sh - 1) for _ in range(sw)]
|
||||||
char_string = ''.join(random.choice(chars) for _ in range(30))
|
column_speeds = [random.choice([1, 1, 2]) for _ in range(sw)] # Mostly speed 1, sometimes 2
|
||||||
x = i # X position for the current column
|
column_colors = [current_color] * sw
|
||||||
y = columns[i] # Y position for the current column
|
|
||||||
|
|
||||||
# Loop through each character in the string and apply fade effect
|
# Handle color cycling for 'rgb' mode every 5 seconds
|
||||||
for j in range(0, 30): # Limit to 30 layers of fade
|
if color_mode == 'rgb' and (now - last_color_change >= 5):
|
||||||
fade_y = (y - j) % sh # Loop the y position within screen bounds
|
color_index = (color_index + 1) % len(COLOR_NAMES)
|
||||||
if fade_y < 0: continue # Skip if outside the screen
|
next_color = COLOR_NAMES[color_index]
|
||||||
|
last_color_change = now
|
||||||
|
|
||||||
# Brightness effect: top characters are bright, others fade out
|
# Gradually switch column colors in 'rgb' mode with some randomness
|
||||||
brightness = max(0, 30 - j)
|
if color_mode == 'rgb':
|
||||||
smooth_brightness = int(30 * (1 - (j / 30))) # Linear fade
|
for i in range(sw):
|
||||||
|
if column_colors[i] != next_color and random.random() < 0.03:
|
||||||
|
column_colors[i] = next_color
|
||||||
|
elif color_mode in COLOR_NAMES:
|
||||||
|
column_colors = [color_mode] * sw
|
||||||
|
|
||||||
# Only draw characters that are within the screen bounds
|
# Show signature every 30 seconds for 5 seconds
|
||||||
if 0 <= fade_y < sh and 0 <= x < sw:
|
if not signature_visible and (now - last_signature_time >= 30):
|
||||||
try:
|
signature_columns.clear()
|
||||||
# Add the character at position (fade_y, x) with a fade effect
|
col = random.randint(0, sw - len(signature)) # Random horizontal position
|
||||||
stdscr.addstr(fade_y, x, char_string[j], curses.color_pair(1) | curses.A_DIM * smooth_brightness)
|
y_start = random.randint(5, max(6, sh - len(signature))) # Random vertical position within bounds
|
||||||
except curses.error:
|
signature_columns[col] = y_start
|
||||||
pass # If the drawing position is out of bounds, just skip it
|
signature_visible = True
|
||||||
|
signature_visible_until = now + 5 # Display duration of 5 seconds
|
||||||
|
last_signature_time = now
|
||||||
|
|
||||||
# Update the position for the next frame (loop back to the top after reaching the bottom)
|
# Hide signature after display time ends
|
||||||
columns[i] = (columns[i] + 1) % sh
|
if signature_visible and now > signature_visible_until:
|
||||||
|
signature_visible = False
|
||||||
|
signature_columns.clear()
|
||||||
|
|
||||||
# Refresh the screen to show the changes
|
stdscr.erase() # Clear screen before drawing new frame
|
||||||
stdscr.refresh()
|
|
||||||
# Control the frame rate (how fast the text falls)
|
|
||||||
time.sleep(0.05)
|
|
||||||
|
|
||||||
# Map color name to curses color code
|
for i in range(sw):
|
||||||
def get_color_pair(color_name):
|
x = i
|
||||||
color_map = {
|
y = columns[i]
|
||||||
'black': curses.COLOR_BLACK,
|
speed = column_speeds[i]
|
||||||
'red': curses.COLOR_RED,
|
color_name = column_colors[i]
|
||||||
'green': curses.COLOR_GREEN,
|
color_id = COLOR_NAMES.index(color_name) + 1
|
||||||
'yellow': curses.COLOR_YELLOW,
|
|
||||||
'blue': curses.COLOR_BLUE,
|
|
||||||
'purple': curses.COLOR_MAGENTA, # Map "purple" to magenta
|
|
||||||
'cyan': curses.COLOR_CYAN,
|
|
||||||
'white': curses.COLOR_WHITE,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Return the mapped color, default to green if invalid
|
# Draw signature if visible in this column
|
||||||
return color_map.get(color_name.lower(), curses.COLOR_GREEN)
|
if signature_visible and i in signature_columns:
|
||||||
|
y_start = signature_columns[i]
|
||||||
|
for j, ch in enumerate(signature):
|
||||||
|
pos_y = (y_start + j) % sh
|
||||||
|
try:
|
||||||
|
attr = curses.color_pair(color_id) | curses.A_BOLD
|
||||||
|
stdscr.addstr(pos_y, i, ch, attr)
|
||||||
|
except curses.error:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# Draw normal falling characters with fade effect
|
||||||
|
char_string = ''.join(random.choice(chars) for _ in range(30))
|
||||||
|
for j in range(30):
|
||||||
|
fade_y = (y - j) % sh
|
||||||
|
if 0 <= fade_y < sh:
|
||||||
|
try:
|
||||||
|
if j == 0:
|
||||||
|
attr = curses.color_pair(color_id) | curses.A_BOLD
|
||||||
|
elif j < 10:
|
||||||
|
attr = curses.color_pair(color_id)
|
||||||
|
else:
|
||||||
|
attr = curses.color_pair(color_id) | curses.A_DIM
|
||||||
|
stdscr.addstr(fade_y, x, char_string[j], attr)
|
||||||
|
except curses.error:
|
||||||
|
pass
|
||||||
|
|
||||||
|
columns[i] = (columns[i] + speed) % sh # Move column down for next frame
|
||||||
|
|
||||||
|
stdscr.refresh() # Update screen with changes
|
||||||
|
time.sleep(0.05) # Frame rate control (~20 FPS)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass # Graceful exit on Ctrl+C
|
||||||
|
|
||||||
# Command-line argument parsing and running the screensaver
|
|
||||||
def main():
|
def main():
|
||||||
# Create a parser for command-line arguments
|
parser = argparse.ArgumentParser(
|
||||||
parser = argparse.ArgumentParser(description="Matrix Screensaver")
|
description="Matrix Screensaver - Simulate falling code in the terminal",
|
||||||
# Add an argument for choosing the color of the falling text
|
formatter_class=argparse.RawTextHelpFormatter
|
||||||
parser.add_argument('-color', type=str, default='green', help="Choose the color of the falling text")
|
)
|
||||||
|
|
||||||
|
parser.add_argument('-c', '--color', type=str, default='green',
|
||||||
|
help="Set the falling text color (use -l to list options)")
|
||||||
|
parser.add_argument('-t', '--timeout', type=int, default=None,
|
||||||
|
help="Exit after N seconds (optional)")
|
||||||
|
parser.add_argument('-l', '--list-colors', action='store_true',
|
||||||
|
help="List available color options and exit")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Initialize curses and pass the chosen color to the screensaver function
|
if args.list_colors:
|
||||||
curses.wrapper(lambda stdscr: matrix_rain(stdscr, get_color_pair(args.color)))
|
print("Available colors:")
|
||||||
|
for color in COLOR_OPTIONS:
|
||||||
|
print(f" - {color}")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
chosen_color = args.color.lower()
|
||||||
|
if chosen_color not in COLOR_OPTIONS:
|
||||||
|
print(f"Error: Invalid color '{chosen_color}'. Use -l to list available options.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
curses.wrapper(lambda stdscr: matrix_rain(stdscr, chosen_color, args.timeout))
|
||||||
|
|
||||||
# Run the script if executed directly
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user