-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflakes
executable file
·194 lines (168 loc) · 3.96 KB
/
snowflakes
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# Snowflake - Draw falling snowflakes on the screen
# Copyright (c) 2008 Lara Stoltenow <[email protected]>
# Licensed under WTFPL
nomodechg=0
function usage()
{
# Show the help text.
cat << EOT
Usage: $0 [ options ] <freq1> <char1> [ <freq2> <char2> [ ... ] ]
freq1, freq2, ... freqn are the frequencies of which the given char1, char2,
... charn fall down the screen.
--help or -h Show this help message.
--nomodechg or -n Don't change the screen mode.
EOT
}
while [ "$(cut -b 1 <<<$1)" = "-" ]
do
case "$1" in
--help | -h ) usage; exit;;
--nomodechg | -n ) nomodechg=1;;
*) echo "$0: unknown option '$1'."; echo "Try '$0 --help' for help.";
exit 1;;
esac
shift
done
# The main program, depending on the fact that all additional parameters are
# already passed. Make sure there are enough arguments...
if [ $# -eq 0 ]
then
echo "$0: missing operands"
echo "'$0 --help' gives more options."
exit 1
fi
f=()
c=()
s=0
t=
while [ "$1" != "" ]
do
f[${#f[*]}]="$1"
c[${#c[*]}]="$2"
shift 2
done
while [ $s -lt ${#f[*]} ]
do
t="$t ${f[$s]}"
s=$[$s+1]
done
function low_begin()
{
# Initialize our screen, and such stuff.
if [ $nomodechg -ne 1 ]
then
echo -ne '\e[?1049h\e[?25l'
fi
}
function low_end()
{
# Reset our screen and do some really needed cleanup.
if [ $nomodechg -ne 1 ]
then
echo -ne '\e[?1049l\e[?25h'
fi
exit
}
function update_termsize()
{
export w=$(stty size | cut -d' ' -f 2)
export h=$(stty size | cut -d' ' -f 1)
}
function throw_flakes()
{
# Throw one line of snowflakes. The frequency can be given as arguments.
# The numbers are in x of 100 flakes.
local ctr r s t _f
_f=()
while [ "$1" != "" ]
do
_f[${#_f[*]}]="$1"
shift
done
echo -ne '\e[H\e[L'
ctr=1
while [ $ctr -lt $w ]
do
r=$RANDOM
s=0
t=0
while [ $s -lt ${#_f[*]} ]
do
t=$[$t+${_f[$s]}]
if [ $r -le $[(32768/100)*$t] ]
then
echo -n "${c[$s]}"
t=0
break
fi
s=$[$s+1]
done
if [ $t -ne 0 ]
then
echo -n " "
fi
ctr=$[$ctr+1]
done
sleep 0.01
}
function begin()
{
# Called to slightly fade the snowflakes in.
local ctr s t
ctr=0
while [ $ctr -lt $h ]
do
s=0
t=
while [ $s -lt ${#f[*]} ]
do
t="$t $[($ctr*${f[$s]})/$h]"
s=$[$s+1]
done
throw_flakes $t
ctr=$[$ctr+1]
done
}
function end()
{
# Called to slightly fade the snowflakes out.
local ctr s t
echo -ne '\e[M'
ctr=$h
while [ $ctr -gt 0 ]
do
s=0
t=
while [ $s -lt ${#f[*]} ]
do
t="$t $[($ctr*${f[$s]})/$h]"
s=$[$s+1]
done
throw_flakes $t
ctr=$[$ctr-1]
done
ctr=$h
while [ $ctr -gt 0 ]
do
echo -ne '\e[L'
sleep 0.1
ctr=$[$ctr-1]
done
low_end
exit
}
trap end 2
trap low_end 9
trap low_end 15
update_termsize
low_begin
begin
while true
do
throw_flakes $t
if [ $RANDOM -lt $[(32768/100)*10] ]
then
update_termsize
fi
done