#!/usr/bin/env bash # ------------------------------------------------------------------------------ # Copyright (c) 2013-2026 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. # ------------------------------------------------------------------------------ load_conf "rain" # Common helpers, then the ones private to this module (helpers/rain.sh) # shellcheck source=../helpers/common.sh declare -F _profile_load_helpers >/dev/null || . "$(dirname -- "${BASH_SOURCE[0]}")/../helpers/common.sh" _profile_load_helpers "${BASH_SOURCE[0]}" # ------------------------------------------------------------------------------ # Let the rain fall (current style) # Usage: rain [OPTIONS] rain() { local _rain_show_usage _rain_show_usage() { printf "Usage: rain [OPTIONS]\n" printf "Options:\n" printf "\t-s, --speed NUM\tSet speed value (default: 5 => 0.050s).\n" printf "\t\t\t\tValues >=1 use a /100 scale (5 => 0.05s).\n" printf "\t\t\t\tValues <1 are interpreted as raw seconds.\n" printf "\t-d, --density NUM\tMaximum number of simultaneous falling elements.\n" printf "\t-c, --color COLOR\tSet the color theme (default: white).\n" printf "\t-h, --help\t\tDisplay this help message and exit.\n\n" printf "Available Colors (case-insensitive):\n" printf "\t\e[32mgreen\e[0m\t: Matrix-like green shades\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 "Press q (or Ctrl-C) to quit.\n" printf "Example: rain --color green --speed 3\n" } local _raw_speed="${RAIN_DEFAULT_SPEED:-5}" local step_duration step_duration=$(_rain_normalize_speed "$_raw_speed") || step_duration=0.050 local base_color="${RAIN_DEFAULT_COLOR:-white}" local density_override="${RAIN_DEFAULT_DENSITY:-}" if [[ -n "$density_override" ]]; then density_override=$(_rain_normalize_density "$density_override") || density_override="" fi while [[ "$#" -gt 0 ]]; do case $1 in -s|--speed) if [[ -n "$2" && ! "$2" =~ ^- ]]; then step_duration=$(_rain_normalize_speed "$2") || { disp E "--speed requires a numeric value greater than 0." _rain_show_usage return 1 } shift else disp E "--speed requires a numeric value greater than 0." _rain_show_usage return 1 fi ;; -c|--color) if [[ -n "$2" && ! "$2" =~ ^- ]]; then base_color="$2" shift else disp E "--color requires a color name." _rain_show_usage return 1 fi ;; -d|--density) if [[ -n "$2" && ! "$2" =~ ^- ]]; then density_override=$(_rain_normalize_density "$2") || { disp E "--density requires a positive integer value." _rain_show_usage return 1 } shift else disp E "--density requires a positive integer value." _rain_show_usage return 1 fi ;; -h|--help) _rain_show_usage return 0 ;; --) shift break ;; *) disp E "Unknown option: $1" _rain_show_usage return 1 ;; esac shift done _rain_engine "$step_duration" "$base_color" "rain" "" "$density_override" } export -f rain # ------------------------------------------------------------------------------ # Matrix style digital rain # Usage: matrix [OPTIONS] matrix() { local _matrix_show_usage _matrix_show_usage() { printf "Usage: matrix [OPTIONS]\n" printf "Options:\n" printf "\t-s, --speed NUM\tSet speed value (default: 3.5 => 0.035s).\n" printf "\t\t\t\tValues >=1 use a /100 scale (3.5 => 0.035s).\n" printf "\t\t\t\tValues <1 are interpreted as raw seconds.\n" printf "\t-d, --density NUM\tMaximum number of simultaneous falling elements.\n" printf "\t-c, --color COLOR\tSet color theme (default: green).\n" printf "\t-C, --charset SET\tCharacter set: binary, kana, ascii (default: binary).\n" printf "\t-h, --help\t\tDisplay this help message and exit.\n\n" printf "Press q (or Ctrl-C) to quit.\n" printf "Example: matrix -C kana -c green --speed 2\n" } local _raw_speed="${MATRIX_DEFAULT_SPEED:-3.5}" local step_duration step_duration=$(_rain_normalize_speed "$_raw_speed") || step_duration=0.035 local base_color="${MATRIX_DEFAULT_COLOR:-green}" # Lower-cased, like the value of -C: the configuration may say "Kana" local charset="${MATRIX_DEFAULT_CHARSET:-binary}" charset="${charset,,}" local density_override="${MATRIX_DEFAULT_DENSITY:-}" if [[ -n "$density_override" ]]; then density_override=$(_rain_normalize_density "$density_override") || density_override="" fi while [[ "$#" -gt 0 ]]; do case $1 in -s|--speed) if [[ -n "$2" && ! "$2" =~ ^- ]]; then step_duration=$(_rain_normalize_speed "$2") || { disp E "--speed requires a numeric value greater than 0." _matrix_show_usage return 1 } shift else disp E "--speed requires a numeric value greater than 0." _matrix_show_usage return 1 fi ;; -c|--color) if [[ -n "$2" && ! "$2" =~ ^- ]]; then case "${2,,}" in binary|kana|kanji|ascii) disp W "'${2}' looks like a charset value. Use -C/--charset for clarity." charset="${2,,}" ;; *) base_color="$2" ;; esac shift else disp E "--color requires a color name." _matrix_show_usage return 1 fi ;; -C|--charset) if [[ -n "$2" && ! "$2" =~ ^- ]]; then charset="${2,,}" shift else disp E "--charset requires a value." _matrix_show_usage return 1 fi ;; -d|--density) if [[ -n "$2" && ! "$2" =~ ^- ]]; then density_override=$(_rain_normalize_density "$2") || { disp E "--density requires a positive integer value." _matrix_show_usage return 1 } shift else disp E "--density requires a positive integer value." _matrix_show_usage return 1 fi ;; -h|--help) _matrix_show_usage return 0 ;; --) shift break ;; *) disp E "Unknown option: $1" _matrix_show_usage return 1 ;; esac shift done _rain_engine "$step_duration" "$base_color" "matrix" "$charset" "$density_override" } export -f matrix # ------------------------------------------------------------------------------ # Full-screen rainbow screensaver # Usage: rainbow [OPTIONS] rainbow() { local PARSED PARSED=$(getopt -o hs: --long help,speed: -n 'rainbow' -- "$@") # shellcheck disable=SC2181 # getopt return code is checked immediately after if [[ $? -ne 0 ]]; then disp E "Invalid options, use \"rainbow --help\" to display usage." return 1 fi local _raw_speed="${RAINBOW_DEFAULT_SPEED:-4}" local frame_sleep frame_sleep=$(_rain_normalize_speed "$_raw_speed") || frame_sleep=0.040 # Restore the getopt-normalised (shell-quoted) argument list local -a _pargs=() _getopt_split _pargs "$PARSED" set -- "${_pargs[@]}" while true; do case "$1" in -h|--help) printf "Usage: rainbow [OPTIONS]\n" printf "Options:\n" printf "\t-s, --speed NUM\tSet horizontal color shift speed (default: 4 => 0.040s).\n" printf "\t\t\t\tValues >=1 use a /100 scale (4 => 0.04s).\n" printf "\t\t\t\tValues <1 are interpreted as raw seconds.\n" printf "\t-h, --help\t\tDisplay this help message and exit.\n\n" printf "The rainbow fills the whole terminal with background colors only.\n" printf "It uses truecolor when supported, otherwise ANSI bright backgrounds.\n" printf "Press q (or Ctrl-C) to quit.\n" return 0 ;; -s|--speed) frame_sleep=$(_rain_normalize_speed "$2") || { disp E "--speed requires a numeric value greater than 0." return 1 } shift 2 ;; --) shift break ;; *) disp E "Invalid options, use \"rainbow --help\" to display usage." return 1 ;; esac done _rain_need_tty rainbow || return 1 local exit_st=0 local term_height=0 term_width=0 local use_truecolor=0 local offset=0 row=0 col=0 palette_width=0 idx=0 local row_line="" screen="" ch="" # shellcheck disable=SC2034 # used by _rain_enter/_rain_leave (dynamic scoping) local saved_stty="" saved_traps="" # WINCH handler (and initial sizing): rebuild the palette for the new width _rainbow_sigwinch() { term_width=$(tput cols) term_height=$(tput lines) ((term_width < 1)) && term_width=1 ((term_height < 1)) && term_height=1 if [[ "$TERM" == *direct* || "${COLORTERM:-}" == "truecolor" || "${COLORTERM:-}" == "24bit" ]]; then use_truecolor=1 else use_truecolor=0 fi palette_width=$term_width offset=0 _rainbow_build_palette "$palette_width" "$use_truecolor" } _rain_enter _rainbow_sigwinch _rainbow_sigwinch while ((exit_st <= 0)); do ch="" # A timeout makes read fail (status > 128): expected, not an error read -r -n 1 -t "$frame_sleep" ch || true case "$ch" in q|Q) _rain_stop ;; esac # One row of colours, shifted by offset (col - offset is at least # 1 - palette_width, so adding palette_width makes it positive) row_line="" for ((col = 0; col < term_width; col++)); do idx=$(( (col - offset + palette_width) % palette_width )) row_line+="${RAINBOW_BG_PALETTE[idx]} " done row_line+=$'\e[0m' # Every row is the same: the whole screen goes out in one write screen="" for ((row = 1; row <= term_height; row++)); do screen+="\e[${row};1H${row_line}" done printf '%b' "$screen" offset=$(( (offset + 1) % palette_width )) done _rain_leave unset -f _rainbow_sigwinch unset RAINBOW_BG_PALETTE return 0 } export -f rainbow # ------------------------------------------------------------------------------ # EOF