Skip to content

Instantly share code, notes, and snippets.

@royrscb
Created April 16, 2019 23:43
Show Gist options
  • Save royrscb/c90bac300135185ee7f456b44e8d2bbe to your computer and use it in GitHub Desktop.
Save royrscb/c90bac300135185ee7f456b44e8d2bbe to your computer and use it in GitHub Desktop.
Matrix effect for linux terminal, working not in parallel
#!/bin/bash
##########################
# Author: Roy Ros Cobo ###
##########################
CHARSET=(ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ワ ン 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)
MAX_LINES=60
ROWS=$(tput lines)
COLS=$[$(tput cols) +1]
GREEN="\033[32m"; WHITE="\033[37m";
declare -a lines_row
declare -a lines_col
declare -a lines_len
declare -a lines_white
declare -a lines_prevChar
function rnd {
if [ $# -eq 1 ]
then echo $[$RANDOM%$1]
else echo $[$RANDOM%$1+$2]
fi
}
function print {
#ROW, COL, CHAR, COLOR
local CURSOR="\033[$1;$2H";
echo -ne "$CURSOR$4$3";
}
function erase_char {
#ROW, COL
print $1 $2 "\u0020"
}
function new_line {
lines_row[$1]=0
lines_col[$1]=$(rnd $COLS)
lines_len[$1]=$(rnd $[ROWS-(ROWS/4)+1] 3)
lines_white[$1]=$(rnd 3)
}
function actualize_line {
((lines_row[$1]++))
local row=${lines_row[$1]}
local col=${lines_col[$1]}
local len=${lines_len[$1]}
local new_char=${CHARSET[$(rnd ${#CHARSET[@]})]}
local prev_char=${lines_prevChar[$1]}
if [ ${lines_white[$1]} -eq 0 ]
then
if [ $row -le $[ROWS +1] ]; then print $[row -1] $col $prev_char $GREEN; fi
if [ $row -le $ROWS ]; then print $row $col $new_char $WHITE; fi
else
if [ $row -le $ROWS ]; then print $row $col $new_char $GREEN; fi
fi
if [ $row -gt $len ]; then erase_char $[row-len] $col; fi
lines_prevChar[$1]=$new_char
}
function is_done_line {
if [ $[${lines_row[$1]} - ${lines_len[$1]}] -eq $ROWS ]
then echo 1
else echo 0
fi
}
function matrix {
clear; setterm --cursor off; unclutter -idle 1 &
for i in $(seq 0 $[MAX_LINES -1]); do new_line $i; done
local n_line=0
while true
do
actualize_line $n_line
if [ $(is_done_line $n_line) -eq 1 ]; then new_line $n_line; fi
((n_line++))
if [ $n_line -eq $MAX_LINES ]; then n_line=0; fi
done
}
matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment