From 9a272689eb82977a5a352815cb7ab689adfd53aa Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Wed, 20 May 2026 15:22:17 +0200 Subject: [PATCH] manage element density --- README.md | 2 ++ doc/profile.conf.example | 8 +++++ doc/profile.conf.fatalerrors | 8 +++++ profile.d/rain.sh | 66 ++++++++++++++++++++++++++++++++++-- 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ff12d5d..f4eb167 100644 --- a/README.md +++ b/README.md @@ -188,8 +188,10 @@ change the default without having to pass flags every time. |---|---|---| | `RAIN_DEFAULT_SPEED` | `0.1` | Falling speed for `rain` | | `RAIN_DEFAULT_COLOR` | `Green` | Colour for `rain` | +| `RAIN_DEFAULT_DENSITY` | dynamic | Maximum number of simultaneous falling elements for `rain` | | `MATRIX_DEFAULT_SPEED` | `0.05` | Falling speed for `matrix` | | `MATRIX_DEFAULT_COLOR` | `Green` | Colour for `matrix` | +| `MATRIX_DEFAULT_DENSITY` | dynamic | Maximum number of simultaneous falling elements for `matrix` | | `MATRIX_DEFAULT_CHARSET` | `binary` | Character set for `matrix` (`binary`, `kana`, `ascii`) | **`[ssh]`** diff --git a/doc/profile.conf.example b/doc/profile.conf.example index f813ac7..61d6606 100755 --- a/doc/profile.conf.example +++ b/doc/profile.conf.example @@ -225,12 +225,20 @@ TERM=xterm-256color # rain: Colour theme. Supported: white (default), green, blue, red, yellow, cyan #RAIN_DEFAULT_COLOR=white +# rain: Maximum number of simultaneous falling elements. +# Leave unset to keep the terminal-size-based dynamic default. +#RAIN_DEFAULT_DENSITY=80 + # matrix: Falling speed. #MATRIX_DEFAULT_SPEED=3.5 # matrix: Colour theme. Supported: green (default), blue, red, yellow, cyan, white #MATRIX_DEFAULT_COLOR=green +# matrix: Maximum number of simultaneous falling elements. +# Leave unset to keep the terminal-size-based dynamic default. +#MATRIX_DEFAULT_DENSITY=120 + # matrix: Character set. Supported: binary (default), kana, ascii #MATRIX_DEFAULT_CHARSET=binary diff --git a/doc/profile.conf.fatalerrors b/doc/profile.conf.fatalerrors index ca4b1d2..d1c9e99 100755 --- a/doc/profile.conf.fatalerrors +++ b/doc/profile.conf.fatalerrors @@ -191,6 +191,10 @@ SET_LOCALE="fr:fr_FR.UTF-8,us:en_US.UTF-8" # Supported values: white (default), green, blue, red, yellow, cyan #RAIN_DEFAULT_COLOR=white +# rain: Maximum number of simultaneous falling elements. +# Leave unset to keep the terminal-size-based dynamic default. +#RAIN_DEFAULT_DENSITY=80 + # matrix: Default speed value, using the /100 scale (3.5 => 0.035s). #MATRIX_DEFAULT_SPEED=3.5 @@ -198,6 +202,10 @@ SET_LOCALE="fr:fr_FR.UTF-8,us:en_US.UTF-8" # Supported values: green (default), blue, red, yellow, cyan, white #MATRIX_DEFAULT_COLOR=green +# matrix: Maximum number of simultaneous falling elements. +# Leave unset to keep the terminal-size-based dynamic default. +#MATRIX_DEFAULT_DENSITY=120 + # matrix: Default character set. # Supported values: binary (default), kana, ascii MATRIX_DEFAULT_CHARSET=kana diff --git a/profile.d/rain.sh b/profile.d/rain.sh index 706f39f..d29d7d9 100644 --- a/profile.d/rain.sh +++ b/profile.d/rain.sh @@ -110,12 +110,24 @@ _rain_normalize_speed() fi } +_rain_normalize_density() +{ + local raw_density="$1" + + if [[ ! "$raw_density" =~ ^[0-9]+$ || "$raw_density" -lt 1 ]]; then + return 1 + fi + + printf "%s" "$raw_density" +} + _rain_engine() { local step_duration="$1" local base_color="$2" local mode="$3" local charset="$4" + local density_override="$5" command -v tput >/dev/null 2>&1 || { disp E "The program 'tput' is required but not installed." @@ -175,6 +187,10 @@ _rain_engine() frame_sleep="$step_duration" ;; esac + + if [[ -n "$density_override" ]]; then + max_rain_width="$density_override" + fi } do_exit() @@ -251,7 +267,11 @@ _rain_engine() if ((num_rains < max_rain_width)) && ((100 * RANDOM / 32768 < new_rain_odd)); then rain_drop="${rain_chars[rain_tab * RANDOM / 32768]}" drop_color="${rain_colors[rain_color_tab * RANDOM / 32768]}" - drop_length=$((max_rain_height * RANDOM / 32768 + 1)) + if [[ "$mode" == "matrix" ]]; then + drop_length=$((max_rain_height * RANDOM / 32768 + 2)) + else + drop_length=$((max_rain_height * RANDOM / 32768 + 1)) + fi X=$((term_width * RANDOM / 32768 + 1)) Y=$((1 - drop_length)) rains=("${rains[@]}" "$X" "$Y" "$rain_drop" "$drop_color" "$drop_length") @@ -280,6 +300,7 @@ rain() printf "\t-s, --speed NUM Set speed value (default: 5 => 0.050s).\n" printf "\t Values >=1 use a /100 scale (5 => 0.05s).\n" printf "\t Values <1 are interpreted as raw seconds.\n" + printf "\t-d, --density NUM Maximum number of simultaneous falling elements.\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" @@ -296,6 +317,11 @@ rain() 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 @@ -323,6 +349,20 @@ rain() 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 @@ -340,7 +380,7 @@ rain() shift done - _rain_engine "$step_duration" "$base_color" "rain" "" + _rain_engine "$step_duration" "$base_color" "rain" "" "$density_override" } export -f rain @@ -356,6 +396,7 @@ matrix() printf "\t-s, --speed NUM Set speed value (default: 3.5 => 0.035s).\n" printf "\t Values >=1 use a /100 scale (3.5 => 0.035s).\n" printf "\t Values <1 are interpreted as raw seconds.\n" + printf "\t-d, --density NUM Maximum number of simultaneous falling elements.\n" printf "\t-c, --color COLOR Set color theme (default: green).\n" printf "\t-C, --charset SET Character set: binary, kana, ascii (default: binary).\n" printf "\t-h, --help Display this help message and exit.\n\n" @@ -367,6 +408,11 @@ matrix() step_duration=$(_rain_normalize_speed "$_raw_speed") || step_duration=0.035 local base_color="${MATRIX_DEFAULT_COLOR:-green}" local charset="${MATRIX_DEFAULT_CHARSET:-binary}" + 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 @@ -412,6 +458,20 @@ matrix() 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 @@ -429,7 +489,7 @@ matrix() shift done - _rain_engine "$step_duration" "$base_color" "matrix" "$charset" + _rain_engine "$step_duration" "$base_color" "matrix" "$charset" "$density_override" } export -f matrix