-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreensaver.sh
More file actions
executable file
·351 lines (310 loc) · 9.45 KB
/
screensaver.sh
File metadata and controls
executable file
·351 lines (310 loc) · 9.45 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env bash
#
# Bash Screensavers
#
# A collection of screensavers written in bash.
# Because who needs fancy graphics when you have ASCII?
#
BASH_SCREENSAVERS_NAME='Bash Screensavers'
BASH_SCREENSAVERS_VERSION='0.0.28'
BASH_SCREENSAVERS_CODENAME='Mystic Map'
BASH_SCREENSAVERS_URL='https://github.com/attogram/bash-screensavers'
BASH_SCREENSAVERS_DISCORD='https://discord.gg/BGQJCbYVBa'
BASH_SCREENSAVERS_LICENSE='MIT'
BASH_SCREENSAVERS_COPYRIGHT='Copyright (c) 2025 Attogram Project <https://github.com/attogram>'
BASH_SCREENSAVERS_GALLERY="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)/gallery"
chosen_screensaver='' # the chosen one
# Peak into the gallery
#
# Output: list of screensaver run scripts, 1 per line
peak_into_the_gallery() {
local screensaver name run
for screensaver in "$BASH_SCREENSAVERS_GALLERY"/*/; do
if [[ -d "${screensaver}" ]]; then
name="$(basename "${screensaver}")"
run="${screensaver}${name}.sh"
if [[ -f "${run}" ]]; then
printf '%s
' "$run"
fi
fi
done
}
# Enjoy a screensaver
#
# Input: 1 - full path to the screensaver run script
# Output: The visual goodness of the screensaver
enjoy_a_screensaver() {
local visual_goodness="$1"
if [[ ! -f "$visual_goodness" ]]; then
echo "404 Screensaver Not Found: $visual_goodness"
return 1
fi
if [[ ! -x "$visual_goodness" ]]; then
tput setaf 1 # red foreground
printf "
Woah there, partner! This screensaver ain't ready for the big show yet.
"
printf "Give it some execute permissions and we'll be in business:
"
printf "chmod +x %s
" "$visual_goodness"
printf "(Press ^C to exit and fix, or to ponder the meaning of file permissions)
"
tput setaf 2 # back to green
return 2
fi
( "$visual_goodness" ) # Execute the saver in a sub‑shell – isolates its `exit` from the menu script
return $?
}
intro() {
#TODO - smart emptiness, or even a nice box around the menu with pretty colors and stuff
local emptiness=' '
echo "$emptiness"
echo "$BASH_SCREENSAVERS_NAME v$BASH_SCREENSAVERS_VERSION ($BASH_SCREENSAVERS_CODENAME)" ' '
echo "$emptiness"
}
choose_screensaver() {
intro
local screensavers=()
while IFS= read -r line; do screensavers+=("$line"); done < <(peak_into_the_gallery)
if [[ ${#screensavers[@]} -eq 0 ]]; then
echo "Whoops! No screensavers found. Add some to the '$BASH_SCREENSAVERS_GALLERY' directory."
echo
exit 1
fi
# Create a list of names for display and for name matching
local names=()
local i=1
for saver in "${screensavers[@]}"; do
local name
name="$(basename "$saver" .sh)"
names+=("$name")
local tagline
# Source config in a subshell to prevent it from breaking the main script
tagline="$( (
local config_file
config_file="$(dirname "$saver")/config.sh"
if [[ -f "$config_file" ]]; then
# shellcheck source=/dev/null
source "$config_file"
echo "$tagline"
fi
) 2>/dev/null )"
if [[ -z "$tagline" ]]; then
printf ' %-2s. %s
' "$i" "$name"
else
printf ' %-2s. %-12s - %s
' "$i" "$name" "$tagline"
fi
i=$((i+1))
done
echo
echo '(Press ^C to exit)'
local choice
echo
echo -n 'Choose your screensaver: '
read -r -e choice
if [[ "$choice" == "r" || "$choice" == "random" ]]; then
return 2 # Special return code for random
fi
if [[ "$choice" =~ ^[0-9]+$ ]]; then # Check if choice is a number
if [ "$choice" -ge 1 ] && [ "$choice" -le "${#screensavers[@]}" ]; then
chosen_screensaver="${screensavers[$((choice-1))]}"
return 0
fi
else # Check if choice is a name
local index=0
for name in "${names[@]}"; do
if [[ "$name" == "$choice" ]]; then
chosen_screensaver="${screensavers[$index]}"
return 0
fi
index=$((index+1))
done
fi
#echo
echo 'Oops, invalid input! Please enter a number or name from the list. '
echo
return 1
}
create_new_screensaver() {
local name="$1"
local dir="$BASH_SCREENSAVERS_GALLERY/$name"
if [ -d "$dir" ]; then
echo "Error: screensaver '$name' already exists at $dir" >&2
exit 1
fi
mkdir -p "$dir"
if [ $? -ne 0 ]; then
echo "Error: failed to create directory $dir" >&2
exit 1
fi
local script_path="$dir/$name.sh"
cat > "$script_path" << EOL
#!/usr/bin/env bash
_cleanup_and_exit() {
tput cnorm # show the cursor again
tput sgr0 # reset all attributes
echo
exit 0
}
trap _cleanup_and_exit EXIT INT TERM QUIT # Catch all common exit signals
animate() {
tput civis # Hide cursor
clear
local width=\$(tput cols)
local height=\$(tput lines)
while true; do
local x=\$((RANDOM % width))
local y=\$((RANDOM % height))
tput cup \$y \$x
echo "*"
sleep 0.1
done
}
animate
EOL
local config_path="$dir/config.sh"
cat > "$config_path" << EOL
# Config for the '$name' screensaver
# Name of the screensaver (12 chars max)
name="$name"
# Tagline for the screensaver (40 chars max)
tagline=""
EOL
echo
echo "Successfully created new screensaver '$name' in $dir"
echo 'To make it runnable, execute:'
echo "chmod +x $script_path"
echo
}
_main_menu_cleanup() {
tput sgr0 # Reset terminal attributes
echo; echo; echo 'Enjoyed Bash Screensavers? Give the project a star on GitHub! ✨'
echo; echo "${BASH_SCREENSAVERS_URL}"; echo
}
main_menu() {
local run_random_first=$1
trap _main_menu_cleanup EXIT
if [[ "$run_random_first" == "random" ]]; then
run_random
fi
while true; do
tput setab 0 # black background
tput setaf 2 # green foreground
echo
choose_screensaver
local choice_return=$?
if [[ $choice_return -eq 1 ]]; then
continue # Invalid choice, re-show menu
fi
if [[ $choice_return -eq 2 ]]; then
run_random
continue # Run random, then re-show menu
fi
enjoy_a_screensaver "$chosen_screensaver" # run until user presses ^C
screensaver_return=$?
if (( screensaver_return )); then
tput setab 0 # black background
tput setaf 1 # red foreground
printf '
Oh no! Screensaver had trouble! Error code: %d
' "$screensaver_return"
tput sgr0 # reset
fi
done
}
BASH_SCREENSAVERS_DESCRIPTION="A collection of screensavers written in bash."
BASH_SCREENSAVERS_USAGE="Usage: $0 [-h|--help] [-v|--version] [-n <name>|--new <name>] [-r|--random] [name|number]"
run_direct() {
local choice="$1"
local screensavers=()
while IFS= read -r line; do screensavers+=("$line"); done < <(peak_into_the_gallery)
if [[ ${#screensavers[@]} -eq 0 ]]; then
echo "Whoops! No screensavers found." >&2
exit 1
fi
if [[ "$choice" =~ ^[0-9]+$ ]]; then # Check if choice is a number
if [ "$choice" -ge 1 ] && [ "$choice" -le "${#screensavers[@]}" ]; then
chosen_screensaver="${screensavers[$((choice-1))]}"
enjoy_a_screensaver "$chosen_screensaver"
return 0
fi
else # Check if choice is a name
local names=()
for saver in "${screensavers[@]}"; do
names+=("$(basename "$saver" .sh)")
done
local index=0
for name in "${names[@]}"; do
if [[ "$name" == "$choice" ]]; then
chosen_screensaver="${screensavers[$index]}"
enjoy_a_screensaver "$chosen_screensaver"
return 0
fi
index=$((index+1))
done
fi
echo "Error: invalid screensaver '$choice'" >&2
echo "$BASH_SCREENSAVERS_USAGE" >&2
exit 1
}
run_random() {
local screensavers=()
while IFS= read -r line; do screensavers+=("$line"); done < <(peak_into_the_gallery)
if [[ ${#screensavers[@]} -eq 0 ]]; then
echo "Whoops! No screensavers found." >&2
exit 1
fi
local random_index=$((RANDOM % ${#screensavers[@]}))
chosen_screensaver="${screensavers[$random_index]}"
enjoy_a_screensaver "$chosen_screensaver"
}
main() {
if [[ "$1" ]]; then
case "$1" in
-h|--help)
echo "$BASH_SCREENSAVERS_NAME v$BASH_SCREENSAVERS_VERSION"
echo "$BASH_SCREENSAVERS_DESCRIPTION"
echo
echo "$BASH_SCREENSAVERS_USAGE"
if [ $# -eq 1 ]; then
exit 0
fi
shift
main "$@"
exit $?
;;
-v|--version)
echo "$BASH_SCREENSAVERS_NAME v$BASH_SCREENSAVERS_VERSION"
if [ $# -eq 1 ]; then
exit 0
fi
shift
main "$@"
exit $?
;;
-n|--new)
if [ -z "$2" ]; then
echo "Error: missing screensaver name for $1 option." >&2
echo "$BASH_SCREENSAVERS_USAGE" >&2
exit 1
fi
create_new_screensaver "$2"
exit 0
;;
-r|--random)
main_menu "random"
exit 0
;;
*)
run_direct "$1"
exit 0
;;
esac
fi
main_menu
}
main "$@"