#!/usr/bin/env bash # ------------------------------------------------------------------------------ # Copyright (c) 2013-2022 Geoffray Levasseur # Protected by the BSD3 license. Please read bellow for details. # # * Redistribution and use in source and binary forms, # * with or without modification, are permitted provided # * that the following conditions are met: # * # * Redistributions of source code must retain the above # * copyright notice, this list of conditions and the # * following disclaimer. # * # * Redistributions in binary form must reproduce the above # * copyright notice, this list of conditions and the following # * disclaimer in the documentation and/or other materials # * provided with the distribution. # * # * Neither the name of the copyright holder nor the names # * of any other contributors may be used to endorse or # * promote products derived from this software without # * specific prior written permission. # * # * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND # * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, # * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # * OF SUCH DAMAGE. # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Let the rain fall # Usage: rain [OPTIONS] # Options: # -s, --speed NUM Set the drop delay in seconds (default: 0.050). # Lower values = faster rain. # -c, --color COLOR Set the color theme (default: white). # -h, --help Display this help message and exit. # Available Colors: # green : The classic Matrix digital rain # blue : Deep ocean blue gradients # red : Crimson/Blood rain # yellow : Amber and gold tones # cyan : Electric cyan/turquoise # white : Greyscale and white (original style) rain() { show_usage() { printf "Usage: rain [OPTIONS]\n" printf "Options:\n" printf "\t-s, --speed NUM Set the drop delay in seconds (default: 0.050).\n" printf "\t Lower values = faster rain.\n" printf "\t-c, --color COLOR Set the color theme (default: white).\n" printf "\t-h, --help Display this help message and exit.\n\n" printf "Available Colors:\n" printf "\t\e[32mgreen\e[0m\t: The classic Matrix digital rain\n" printf "\t\e[34mblue\e[0m\t: Deep ocean blue gradients\n" printf "\t\e[31mred\e[0m\t: Crimson/Blood rain\n" printf "\t\e[33myellow\e[0m\t: Amber and gold tones\n" printf "\t\e[36mcyan\e[0m\t: Electric cyan/turquoise\n" printf "\twhite\t: Greyscale and white (original style)\n\n" printf "Example: rain --color green --speed 0.03\n" } local step_duration=0.050 local base_color="white" # default color scheme, can be overridden by --color # Analyse arguments while [[ "$#" -gt 0 ]]; do case $1 in -s|--speed) if [[ -n "$2" && ! "$2" =~ ^- ]]; then step_duration="$2"; shift else disp E "--speed requires a numeric value." show_usage && return 1 fi ;; -c|--color) if [[ -n "$2" && ! "$2" =~ ^- ]]; then base_color="$2"; shift else disp E "--color requires a color name." show_usage && return 1 fi ;; -h|--help) show_usage && return 0 ;; *) disp E "Unknown option: $1" show_usage && return 1 ;; esac shift done # Define colors (256-colors gradients) local rain_colors=() case $base_color in green) # Matrix style green for i in {22..28} {34..40} {46..48}; do rain_colors+=("\e[38;5;${i}m"); done ;; blue) # Deep ocean blues for i in {17..21} {27..33} {39..45}; do rain_colors+=("\e[38;5;${i}m"); done ;; red) # Crimson / blood red for i in {52..52} {88..88} {124..124} {160..160} {196..201}; do rain_colors+=("\e[38;5;${i}m"); done ;; yellow) # Amber / gold for i in {58..58} {100..100} {142..142} {184..184} {226..229}; do rain_colors+=("\e[38;5;${i}m"); done ;; cyan) # Electric cyan / turquoise for i in {30..31} {37..38} {44..45} {50..51}; do rain_colors+=("\e[38;5;${i}m"); done ;; *) # Greyscale / white (original style) rain_colors=("\e[37m" "\e[37;1m") for i in {244..255}; do rain_colors+=("\e[38;5;${i}m"); done ;; esac local exit_st=0 local rain_cars=("|" "│" "┃" "┆" "┇" "┊" "┋" "╽" "╿") local rain_tab=${#rain_cars[@]} local rain_color_tab=${#rain_colors[@]} local num_rain_metadata=5 local term_height=$(tput lines) local term_width=$(tput cols) local X=0 Y=0 drop_length=0 rain_drop=0 local max_rain_width=0 new_rain_odd=0 falling_odd=0 sigwinch() { term_width=$(tput cols) term_height=$(tput lines) #step_duration=0.025 ((max_rain_width = term_width * term_height / 4)) ((max_rain_height = term_height < 10 ? 1 : term_height / 10)) # In percentage ((new_rain_odd = term_height > 50 ? 100 : term_height * 2)) ((new_rain_odd = new_rain_odd * 75 / 100)) ((falling_odd = term_height > 25 ? 100 : term_height * 4)) ((falling_odd = falling_odd * 90 / 100)) } do_exit() { exit_st=1 } do_render() { # Clean screen first local idx=0 for ((idx = 0; idx < num_rains * num_rain_metadata; idx += num_rain_metadata)); do X=${rains[idx]} Y=${rains[idx + 1]} drop_length=${rains[idx + 4]} for ((y = Y; y < Y + drop_length; y++)); do ((y < 1 || y > term_height)) && continue echo -ne "\e[${y};${X}H " done done for ((idx = 0; idx < num_rains * num_rain_metadata; idx += num_rain_metadata)); do if ((100 * RANDOM / 32768 < falling_odd)); then # Falling if ((++rains[idx + 1] > term_height)); then # Out of screen, bye sweet <3 rains=("${rains[@]:0:idx}" "${rains[@]:idx+num_rain_metadata:num_rains*num_rain_metadata}") ((num_rains--)) continue fi fi X=${rains[idx]} Y=${rains[idx + 1]} rain_drop=${rains[idx + 2]} drop_color=${rains[idx + 3]} drop_length=${rains[idx + 4]} for ((y = Y; y < Y + drop_length; y++)); do ((y < 1 || y > term_height)) && continue printf "\e[${y};${X}H${drop_color}${rain_drop}" done done } trap do_exit TERM INT trap sigwinch WINCH # No echo stdin and hide the cursor stty -echo printf "\e[?25l" printf "\e[2J" local rains=() local num_rains=0 sigwinch while ((exit_st <= 0)); do if (($exit_st <= 0)); then read -n 1 -t $step_duration ch case "$ch" in q | Q) do_exit ;; esac if ((num_rains < max_rain_width)) && ((100 * RANDOM / 32768 < new_rain_odd)); then # Need new |, 1-based rain_drop="${rain_cars[rain_tab * RANDOM / 32768]}" drop_color="${rain_colors[rain_color_tab * RANDOM / 32768]}" drop_length=$((max_rain_height * RANDOM / 32768 + 1)) X=$((term_width * RANDOM / 32768 + 1)) Y=$((1 - drop_length)) rains=("${rains[@]}" "$X" "$Y" "$rain_drop" "$drop_color" "$drop_length") ((num_rains++)) fi # Let rain fall! do_render fi done echo -ne "\e[${term_height};1H\e[0K" # Show cursor and echo stdin echo -ne "\e[?25h" stty echo unset exit_st trap - TERM INT trap - WINCH } export -f rain # ------------------------------------------------------------------------------ # EOF