From c496903afcb2f714f16dcc092c2fecbba1b3dda3 Mon Sep 17 00:00:00 2001 From: wolandark Date: Sat, 19 Oct 2024 18:11:26 +0330 Subject: [PATCH] Improved Version and Glyphs --- ANSI-Printing/Matrix-Rain/Echos.txt | 6 ++ ANSI-Printing/Matrix-Rain/How_It_Works.md | 66 ++++++++++++++++ .../Matrix-Rain/Matrix-Improved/terminfo.sh | 33 ++++++++ .../Matrix-Improved/woland-matrix.sh | 75 +++++++++++++++++++ ANSI-Printing/Matrix-Rain/woland-matrix.sh | 73 ++++++++++-------- 5 files changed, 221 insertions(+), 32 deletions(-) create mode 100644 ANSI-Printing/Matrix-Rain/Echos.txt create mode 100644 ANSI-Printing/Matrix-Rain/How_It_Works.md create mode 100755 ANSI-Printing/Matrix-Rain/Matrix-Improved/terminfo.sh create mode 100755 ANSI-Printing/Matrix-Rain/Matrix-Improved/woland-matrix.sh diff --git a/ANSI-Printing/Matrix-Rain/Echos.txt b/ANSI-Printing/Matrix-Rain/Echos.txt new file mode 100644 index 0000000..2c95fc9 --- /dev/null +++ b/ANSI-Printing/Matrix-Rain/Echos.txt @@ -0,0 +1,6 @@ +# Normal +echo -e "\033[$((i-1));${X}H\033[32m$Char\033[$i;${X}H\033[37m$Char" +# All Bold +echo -e "\033[$((i-1));${X}H\033[32m$Char\033[$i;${X}H\033[1;37m$Char" +# Just White Bold +echo -e "\033[$((i-1));${X}H\033[32m$Char\033[$i;${X}H\033[1;37m$Char\033[22m" diff --git a/ANSI-Printing/Matrix-Rain/How_It_Works.md b/ANSI-Printing/Matrix-Rain/How_It_Works.md new file mode 100644 index 0000000..e6bd390 --- /dev/null +++ b/ANSI-Printing/Matrix-Rain/How_It_Works.md @@ -0,0 +1,66 @@ +### `(` +- Starts a subshell to run the following commands in parallel (background). + +### `X=$((RANDOM%Columns))` +- Randomly selects a column position (`X`) within the width of the terminal (`Columns`), where `RANDOM` is a built-in Bash variable that generates a random number. + +### `Y=$((RANDOM%Rows))` +- Randomly selects a row position (`Y`) within the height of the terminal (`Rows`). + +### `for ((i = 1; i <= Rows; i++))` +- A loop that iterates over each row (`i`) from `1` to the number of terminal rows (`Rows`), simulating the animation down the terminal. + +### `CharIndex=$((RANDOM % ${#Alphabets[@]}))` +- Selects a random index from the `Alphabets` array, where `${#Alphabets[@]}` is the number of elements in the array. + +### `Char=${Alphabets[CharIndex]}` +- Retrieves a random character (`Char`) from the `Alphabets` array based on the selected `CharIndex`. + +### `echo -e "\033[$((i-1));${X}H\033[32m$Char\033[$i;${X}H\033[37m$Char"` +- Moves the cursor to position `(i-1, X)` (the previous row) and prints the character in green (`\033[32m`). +- Then, it moves the cursor to `(i, X)` (current row) and prints the same character in white (`\033[37m`). + +### `sleep 0.1` +- Pauses for 0.1 seconds before continuing, controlling the speed of the animation. + +### `if (( i >= Y )); then` +- Checks if the current row `i` is greater than or equal to the randomly selected row `Y`. If true, the following command erases the character that is `Y` rows above. + +### `echo -e "\033[$((i-Y));${X}H "` +- Moves the cursor to position `((i-Y), X)` and erases the character by printing a space. + +### `done` +- Ends the inner `for` loop. + +### `for ((i = i - Y; i <= Rows; i++))` +- Starts another loop to continue erasing characters as they "fall off" the screen. It starts from `i - Y` (where the last character was erased) to the end of the screen (`Rows`). + +### `echo -e "\033[$i;${X}f "` +- Moves the cursor to `(i, X)` and erases the character by printing a space. + +### `sleep 0.1` +- Pauses for 0.1 seconds before continuing to the next iteration. + +### `) &` +- Ends the subshell and runs the block of code in the background (`&`). + +### `read -rt 0.1 -n 1 key` +- Waits for 0.1 seconds for a key press from the user. If no key is pressed, it continues. If a key is pressed, it stores the key in the variable `key`. + +### `if [[ $key == "q" ]]; then` +- If the pressed key is "q", the following commands are executed to stop the script. + +### `pkill -P $$` +- Terminates all child processes (those running in the background, created by the `&`). + +### `echo -ne "\x1b[?1049l"` +- Restores the main screen (exits the alternate screen mode). + +### `echo -ne "\x1B[?25h"` +- Makes the cursor visible again. + +### `exit` +- Exits the script, stopping the infinite loop. + +### `sleep 0.05` +- Pauses for 0.05 seconds before restarting the `while` loop, slightly controlling the overall loop speed. diff --git a/ANSI-Printing/Matrix-Rain/Matrix-Improved/terminfo.sh b/ANSI-Printing/Matrix-Rain/Matrix-Improved/terminfo.sh new file mode 100755 index 0000000..f00d544 --- /dev/null +++ b/ANSI-Printing/Matrix-Rain/Matrix-Improved/terminfo.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# get_fg_color +# echo -e "Your colors are\n\t $FG_COLOR" +# source: https://gist.github.com/blueyed/c8470c2aad3381c33ea3 +# modified by Woland + +get_color() { + oldstty=$(stty -g) # Save current stty settings + Ps=${1:-11} # Default to 11 (background color) if no argument + + stty raw -echo min 0 time 0 # Set terminal to raw mode + printf "\033]$Ps;?\033\\" # Query terminal for background color + + # Add a minimal sleep to ensure response is captured + sleep 0.00000001 + + read -r answer # Read terminal response + result=${answer#*;} # Strip leading escape characters + stty $oldstty # Restore stty settings + + # Remove trailing unwanted characters and export result + # export _COLOR=$(echo "$result" | sed 's/[^rgb:0-9a-f/]\+$//') + eval "$2=\$(echo \"\$result\" | sed 's/[^rgb:0-9a-f/]\+\$//')" +} + +get_color 10 fg +get_color 11 bg +output_hex_fg="#$(echo "$fg" | sed -E 's/rgb:([0-9a-fA-F]{2})[0-9a-fA-F]{2}\/([0-9a-fA-F]{2})[0-9a-fA-F]{2}\/([0-9a-fA-F]{2})[0-9a-fA-F]{2}/\1\2\3/')" +output_hex_bg="#$(echo "$bg" | sed -E 's/rgb:([0-9a-fA-F]{2})[0-9a-fA-F]{2}\/([0-9a-fA-F]{2})[0-9a-fA-F]{2}\/([0-9a-fA-F]{2})[0-9a-fA-F]{2}/\1\2\3/')" + +# echo -e "Your terminal background color is:\n\t $bg" +# echo -e "Your terminal background color is:\n\t $fg" diff --git a/ANSI-Printing/Matrix-Rain/Matrix-Improved/woland-matrix.sh b/ANSI-Printing/Matrix-Rain/Matrix-Improved/woland-matrix.sh new file mode 100755 index 0000000..e08d043 --- /dev/null +++ b/ANSI-Printing/Matrix-Rain/Matrix-Improved/woland-matrix.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# +# Matrix Digital Rain +# Written by Woland https://github.com/wolandark +# https://github.com/wolandark/BASH_Scripts_For_Everyone +# +# Dependency: +# Use a font that supports katakana characters such as IBM Plex +# Preferably Use Xterm or Xterm Compatible Terminal +# + +# Include terminfo.sh To Store Initial BG and FG Colors +source terminfo.sh # Only works well in xterm, uxterm, rxvt, konsole, st + +# Init LINES COLUMNS With A No-Op +shopt -s checkwinsize || return 1; :; : + +# Setup Term, Hide Cursor, Switch To Alt Screen and Set Colors +echo -ne "\033[?25l" +echo -ne "\x1b[?1049h" +echo -ne "\033]11;#000000\007" +echo -ne "\033]10;#00FF00\007" + +Rows=$LINES +Columns=$COLUMNS +Rows=$((Rows -1)) + +Alphabets=("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "۱" "۲" "۳" "۴" "۵" "۶" "۷" "۸" "۹" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "ア" "イ" "ウ" "エ" "オ" "カ" "キ" "ク" "ケ" "コ" "サ" "シ" "ス" "セ" "ソ" "タ" "チ" "ツ" "テ" "ト" "ナ" "ニ" "ヌ" "ネ" "ノ" "ハ" "ヒ" "フ" "ヘ" "ホ" "マ" "ミ" "ム" "メ" "モ" "ヤ" "ユ" "ヨ" "ラ" "リ" "ル" "レ" "ロ" "ワ" "ヰ" "ヱ" "ヲ" "ン" " ") + +# Track Prev X Pos +declare -A previous_X + +while :; do + ( + X=$((RANDOM % (Columns / 2) * 2)) + if [[ -n ${previous_X[$X]} ]]; then + : + else + # Make Prev X Occupied + previous_X[$X]=1 + fi + + Y=$((RANDOM%Rows)) + + for ((i = 1; i <= Rows; i++)); do + CharIndex=$((RANDOM % ${#Alphabets[@]})) + Char=${Alphabets[CharIndex]} + + # White Bold Green Dim + echo -e "\033[$((i-1));${X}H\033[2;32m$Char\033[22m\033[$i;${X}H\033[1;37m$Char\033[22m" + sleep 0.1 + + if (( i >= Y )); then + echo -e "\033[$((i-Y));${X}H " + fi + done + + for ((i = i - Y; i <= Rows; i++)); do + echo -e "\033[$i;${X}f " + sleep 0.1 + done + ) & + + read -rt 0.1 -n 1 key + if [[ $key == "q" ]]; then + pkill -P $$ + # Restore Main Screen, Terminal Colors and Cursor + echo -ne "\x1b[?1049l" + echo -ne "\x1B[?25h" + echo -ne "\033]11;"$output_hex_bg"\007" + echo -ne "\033]10;"$output_hex_fg"\007" + exit + fi + sleep 0.05 +done diff --git a/ANSI-Printing/Matrix-Rain/woland-matrix.sh b/ANSI-Printing/Matrix-Rain/woland-matrix.sh index 02d2894..e8bda8b 100755 --- a/ANSI-Printing/Matrix-Rain/woland-matrix.sh +++ b/ANSI-Printing/Matrix-Rain/woland-matrix.sh @@ -1,54 +1,63 @@ -#!/bin/bash - +#!/usr/bin/env bash +# +# Matrix Digital Rain +# Written by Woland https://github.com/wolandark +# https://github.com/wolandark/BASH_Scripts_For_Everyone +# +# Dependency: +# Use a font that supports katakana characters such as IBM Plex +# Preferably Use Xterm or Xterm Compatible Terminal +# + +# Init LINES COLUMNS With A No-Op +shopt -s checkwinsize || return 1; (:;:) # Hide Cursor -echo -e "\033[?25l"; +echo -ne "\033[?25l"; # Use Alt Screen echo -ne "\x1b[?1049h" -Rows=$(tput lines) -Columns=$(tput cols) +Rows=$LINES +Columns=$COLUMNS Rows=$((Rows -1)) -Alphabets=("あ" "い" "う" "え" "お" "か" "き" "く" "け" "こ" "さ" "し" "す" "せ" "そ" "た" "ち" "つ" "て" "と" "な" "に" "ぬ" "ね" "の" "は" "ひ" "ふ" "へ" "ほ" "ま" "み" "む" "め" "も" "や" "ゆ" "よ" "ら" "り" "る" "れ" "ろ" "わ" "を" "ん" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "ア" "イ" "ウ" "エ" "オ" "カ" "キ" "ク" "ケ" "コ" "サ" "シ" "ス" "セ" "ソ" "タ" "チ" "ツ" "テ" "ト" "ナ" "ニ" "ヌ" "ネ" "ノ" "ハ" "ヒ" "フ" "ヘ" "ホ" "マ" "ミ" "ム" "メ" "モ" "ヤ" "ユ" "ヨ" "ラ" "リ" "ル" "レ" "ロ" "ワ" "ヲ" "ン" "З" "а" "р" "е" "г" "и" "с" "т" "р" "и" "р" "у" "й" "т" "е" "с" "ь" "с" "е" "й" "ч" "а" "с" "н" "а" "Д" "е" "с" "я" "т" "у" "ю" "М" "е" "ж" "д" "у" "н" "а" "р" "о" "д" "н" "у" "ю" "К" "о" "н" "ф" "е" "р" "е" "н" "ц" "и" "ю" "п" "о" "แ" "ผ่" "น" "ดิ" "น" "ฮั่" "น" "เ" "สื่" "อ" "ม" "โ" "ท" "ร" "ม" "แ" "ส" "น" "สั" "ง" "เ" "ว" "ช" "พ" "ร" "ะ" "ป" "ก" "เ" "ก" "ศ" "ก" "อ" "ง" "บู๊" "กู้" "ขึ้" "น" "ใ" "ห" "ม่" "ብ" "ላ" "ካ" "ለ" "ኝ" "እ" "ን" "ደ" "አ" "ባ" "ቴ" "በ" "ቆ" "መ" "ጠ" "ኝ" "ሰ" "ማ" "ይ" "አ" "ይ" "ታ" "ረ" "ስ" "ን" "ጉ" "ሥ" "አ" "ይ" "ከ" "ሰ" "ስ" "ᚻ" "ᛖ" "ᚳ" "ᚹ" "ᚫ" "ᚦ" "ᚦ" "ᚫ" "ᛏ" "ᚻ" "ᛖ" "ᛒ" "ᚢ" "ᛞ" "ᛖ" "ᚩ" "ᚾ" "ᚦ" "ᚫ" "ᛗ" "ᛚ" "ᚪ" "ᚾ" "ᛞ" "ᛖ" "ᚾ" "ᚩ" "ᚱ" "ᚦ" "ᚹ" "ᛖ" "ᚪ" "ᚱ" "ᛞ" "ᚢ" "ᛗ" "ᚹ" "ᛁ" "ᚦ" "ᚦ" "ᚪ" "ᚹ" "ᛖ" "ᛥ" "ᚫ" "გ" "თ" "ხ" "ო" "ვ" "თ" "ა" "ხ" "ლ" "ა" "ვ" "ე" "გ" "ა" "ი" "ა" "რ" "ო" "თ" "რ" "ე" "გ" "ი" "ს" "ტ" "რ" "ა" "ც" "ი" "ა" "τ" "ὴ" "ν" "ἀ" "ρ" "χ" "ὴ" "ν" "ὀ" "ρ" "θ" "ῶ" "ς" "ὑ" "π" "ο" "θ" "έ" "σ" "θ" "α" "ι" "," "μ" "ά" "τ" "α" "ι" "ο" "ν" "ἡ" "γ" "ο" "ῦ" "μ" "α" "ι" "π" "ε" "ρ" "ὶ" "τ" "ῆ" "ς" "∮" "E" "⋅" "d" "a" "=" "Q" "," "n" "→" "∞" "," "∑" "f" "(" "i" ")" "=" "∏" "g" "(" "i" ")" "," "∀" "x" "∈" "ℝ" ":" "⌈" "x" "⌉" "=" "−" "⌊" "−" "x" "⌋" "," "α" "∧" "¬" "β" "=" "¬" "(" "¬" "α" "∨" "β") +Alphabets=("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "۱" "۲" "۳" "۴" "۵" "۶" "۷" "۸" "۹" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "ア" "イ" "ウ" "エ" "オ" "カ" "キ" "ク" "ケ" "コ" "サ" "シ" "ス" "セ" "ソ" "タ" "チ" "ツ" "テ" "ト" "ナ" "ニ" "ヌ" "ネ" "ノ" "ハ" "ヒ" "フ" "ヘ" "ホ" "マ" "ミ" "ム" "メ" "モ" "ヤ" "ユ" "ヨ" "ラ" "リ" "ル" "レ" "ロ" "ワ" "ヰ" "ヱ" "ヲ" "ン" " ") while true do - ( - X=$((RANDOM%Columns)) - Y=$((RANDOM%Rows)) - - # for i in $(eval echo -e {1..$Rows}) - for ((i = 1; i <= Rows; i++)) - do - # Select a random alphabet from the array - CharIndex=$((RANDOM % ${#Alphabets[@]})) - Char=${Alphabets[CharIndex]} - - echo -e "\033[$((i-1));${X}H\033[32m$Char\033[$i;${X}H\033[37m$Char" + ( + X=$((RANDOM%Columns)) + Y=$((RANDOM%Rows)) + + for ((i = 1; i <= Rows; i++)) + do + # Select a random alphabet from the array + CharIndex=$((RANDOM % ${#Alphabets[@]})) + Char=${Alphabets[CharIndex]} + + # White Bold Green Dim + echo -e "\033[$((i-1));${X}H\033[2;32m$Char\033[22m\033[$i;${X}H\033[1;37m$Char\033[22m" sleep 0.1 - if (( i >= Y )); then - echo -e "\033[$((i-Y));${X}H " + if (( i >= Y )); then + echo -e "\033[$((i-Y));${X}H " fi - done + done - # for i in $(eval echo -e {$((i-d))..$Rows}); #[mat!rix] - for ((i = i - Y; i <= Rows; i++)) - do + for ((i = i - Y; i <= Rows; i++)) + do echo -e "\033[$i;${X}f " sleep 0.1 - done - ) & + done + ) & - read -rt 0.1 -n 1 key - if [[ $key == "q" ]]; then + read -rt 0.1 -n 1 key + if [[ $key == "q" ]]; then pkill -P $$ # Terminate Child Processes # Restore Main Screen echo -ne "\x1b[?1049l" # Restore Cursor echo -ne "\x1B[?25h" exit - fi - sleep 0.05 -done - + fi + sleep 0.05 + done