-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmas
executable file
·205 lines (186 loc) · 4 KB
/
xmas
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
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
# shellcheck disable=SC2175
set -eo pipefail
version="0.0.5"
star='\033[33m★\033[m'
left='\033[32m/\033[m'
right='\033[32m\\033[m'
bottom='\033[32m^\033[m'
trunk='\033[31m| |\033[m'
_help() {
echo "
${0##*/} -- Plant X'mas tree(s)
Usage: ${0##*/} [options]
Options:
-s, --size <POSITIVE INT> game level (default=5)
-n, --number <POSITIVE INT> number of tree (default=1)
-l, --line <POSITIVE INT> number of line (default=1)
-a, --animation enable animation
-i, --interval <NATURAL INT> animation interval (default=1)
-m, --monochrome disable color
-h, --help display this help and exit
-v, --version output version infromation and exit
Version:
$version"
}
_orand() {
colors=(21 33 34 35 36 37)
ornaments=(
' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' '
'i' '⸛' '⁂' '⸮' '&' '@' '。'
)
echo -ne "\033[${colors[RANDOM % 6]}m${ornaments[RANDOM % 17]}\033[m"
}
_nchar() {
local i
if [ "$1" = 0 ]; then
echo -n ""
else
for i in $(eval echo {1.."$1"}); do
echo -ne "$2"
done
fi
}
_mktree() {
local n i _
n="$1"
# header
_nchar "$((n + 1))" " "
echo -ne "$star"
_nchar "$((n + 1))" " "
echo
# body
for i in $(eval echo {1.."$n"}); do
_nchar "$((n - i))" " "
echo -ne "$left"
e="$((i * 2 - 1))"
for _ in $(eval echo {1.."$e"}); do
_orand
done
echo -ne "$right"
_nchar "$((n + 1 - i))" " "
echo
done
# footer 1
_nchar "$n" "$bottom"
echo -ne "$trunk"
_nchar "$n" "$bottom"
echo
# footer 2
_nchar "$n" " "
echo -ne "$trunk"
echo
}
_print_tree() {
eval paste "$(
for i in $(eval echo {1.."$2"}); do
echo -n "<(_mktree \"$1\") "
done
)"
}
_mono() {
if [ "$1" = 1 ]; then
sed 's/\x1b\[[0-9;]*m//g'
else
cat
fi
}
main() {
local size number line animation interval monochrome
size="5"
number="1"
line="1"
animation="0"
interval="1"
monochrome="0"
s="0"
for arg in "${@}"; do
if [ $s -gt 0 ]; then
((s--))
continue
fi
case $arg in
-h | --help)
_help
return 0
;;
-v | --version)
echo "$version"
return 0
;;
-s | --size)
if [[ -z $2 ]] || ! [[ $2 =~ ^[0-9]+$ ]] || [ "$2" -lt 1 ]; then
echo "error: option '$1' requires positive int -- $2" >&2
return 1
fi
size="$2"
shift 2
s=1
;;
-n | --number)
if [[ -z $2 ]] || ! [[ $2 =~ ^[0-9]+$ ]] || [ "$2" -lt 1 ]; then
echo "error: option '$1' requires positive int -- $2" >&2
return 1
fi
number="$2"
shift 2
s=1
;;
-l | --line)
if [[ -z $2 ]] || ! [[ $2 =~ ^[0-9]+$ ]] || [ "$2" -lt 1 ]; then
echo "error: option '$1' requires positive int -- $2" >&2
return 1
fi
line="$2"
shift 2
s=1
;;
-i | --interval)
if [[ -z $2 ]] || ! [[ $2 =~ ^[0-9]+$ ]] || [ "$2" -lt 0 ]; then
echo "error: option '$1' requires natural int -- $2" >&2
return 1
fi
interval="$2"
shift 2
s=1
;;
-a | --animation)
animation="1"
shift
;;
-m | --monochrome)
monochrome="1"
shift
;;
-*)
echo "error: unknown option -- $1" >&2
return 1
;;
*)
echo "error: extra argument -- '$arg'" >&2
return 1
;;
esac
done
local tmp_f="$(mktemp)"
if [ "$animation" = "1" ]; then
while :; do
for i in $(eval echo {1.."$line"}); do
_print_tree "$size" "$number" | _mono "$monochrome"
done > "$tmp_f"
cat "$tmp_f"
sleep "$interval"s
for i in $(eval echo {1.."$line"}); do
echo -ne "\\033[$((size + 3))A"
done
done
else
for i in $(eval echo {1.."$line"}); do
_print_tree "$size" "$number" | _mono "$monochrome"
done > "$tmp_f"
cat "$tmp_f"
fi
}
main "${@}"
exit $?