-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.sh
executable file
·102 lines (89 loc) · 2.28 KB
/
matrix.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
ESCAPE="\033["
WIDTH=$(tput cols)
HEIGHT=$(tput lines)
clear_screen () {
echo -e "${ESCAPE}2J" # clear screen
echo -e "${ESCAPE}0M" # reset styles
echo -e "${ESCAPE}?25l" # hide cursor
}
ctrlc () {
[[ -z "$(jobs -p)" ]] || kill $(jobs -p) # kill all subprocesses
echo -e "${ESCAPE}2J" # clear screen
echo -e "${ESCAPE}0M" # reset styles
echo -e "${ESCAPE}H" # go to 0, 0
echo -e "${ESCAPE}?25h" # show cursor
}
write_at_color () {
# random chance of the letter being white if $4 == 255
# aka first letter
if (( $4 > 220 )) && (( RANDOM % 10 >= 9 ))
then
echo -e "${ESCAPE}$2;$1H${ESCAPE}97m$3"
else
echo -e "${ESCAPE}$2;$1H${ESCAPE}38;2;0;$4;0m$3"
fi
}
clear_at () {
echo -e "${ESCAPE}$2;$1H "
}
strings_fall() {
# string coordinates
X=$(($1))
Y=$((RANDOM % HEIGHT))
# string length
LEN=$((RANDOM % 20 + 10))
# utf-8 starting kanji
BASE_CHAR=0x30a0
# random delay at start
sleep $(echo "scale=4;${RANDOM}.0/32767.0/4" | bc )
while true
do
for (( i = 0; i < LEN; i++ ))
do
# new Y
NY=$((Y - i))
if (( NY < 0 ))
then
NY=$((NY + HEIGHT))
fi
# previous Y
PY=$((NY - 1))
if (( PY < 0 ))
then
PY=$((PY + HEIGHT))
fi
# seed to generate letter
SEED=$((RANDOM % 95))
# calculate the utf code for the letter
CHAR=$(printf '%x\n' $((BASE_CHAR + SEED)) )
# calculate green fading
FADE=$((255 - 255 * i / LEN))
# clear old char
clear_at $X $PY
# write new char
write_at_color $X $NY "\u${CHAR}" $FADE
done
# update Y coordinate
((Y = (Y + 1) % HEIGHT))
# small delay
sleep 0.01
done
}
# check if bc is installed
if ! [ -x "$(command -v bc)" ]; then
echo 'Error: bc is not installed. Install it with your package manager.' >&2
exit 1
fi
# catch SIGINT
trap ctrlc SIGINT
# clear the screen
clear_screen
# spawn string
for ((i = 0; i < WIDTH/3; i++))
do
# they run in parallel
strings_fall $((i * 3)) &
done
# loop forever
wait