-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorscript
More file actions
140 lines (124 loc) · 3.81 KB
/
colorscript
File metadata and controls
140 lines (124 loc) · 3.81 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
#!/usr/bin/env bash
# define directories
DIR_COLORSCRIPTS="/usr/share/colorscripts/colorscripts"
DIR_BLACKLIST="${DIR_COLORSCRIPTS}/blacklisted"
# ensure the blacklist directory exists
mkdir -p "${DIR_BLACKLIST}"
# print help message
function _help() {
cat <<EOF
$(tput bold)colorscripts$(tput sgr0) - run various shell color scripts
$(tput bold)Usage:$(tput sgr0)
colorscript [OPTION] [SCRIPT NAME/INDEX]
$(tput bold)Options:$(tput sgr0)
$(tput setaf 3)-h, --help, help$(tput sgr0) Print this help.
$(tput setaf 3)-l, --list, list$(tput sgr0) List all installed color scripts.
$(tput setaf 3)-r, --random, random$(tput sgr0) Run a random color script.
$(tput setaf 3)-e, --exec, exec$(tput sgr0) Run specified color script by SCRIPT NAME or INDEX.
$(tput setaf 3)-a, --all, all$(tput sgr0) Run all color scripts and list their SCRIPT NAMEs.
$(tput setaf 3)-b, --blacklist, blacklist$(tput sgr0) Blacklist a color script by SCRIPT NAME or INDEX.
$(tput setaf 3)-u, --unblacklist, unblacklist$(tput sgr0) Unblacklist a color script by SCRIPT NAME or INDEX.
$(tput setaf 3)-v, --version, version$(tput sgr0) Print version information.
EOF
}
# list installed scripts
function _list() {
echo "Installed scripts:"
local index=1
for script in "${DIR_COLORSCRIPTS}"/*; do
[[ -f "$script" && ! "$script" =~ blacklisted ]] || continue
echo "[$index] $(basename "$script")"
((index++))
done
echo
}
# run a random script
function _random() {
scripts=($(find "${DIR_COLORSCRIPTS}" -maxdepth 1 -type f ! -path "${DIR_BLACKLIST}/*"))
count=${#scripts[@]}
if [[ $count -eq 0 ]]; then
echo "No color scripts available."
exit 1
fi
random_index=$((RANDOM % count))
exec "${scripts[$random_index]}"
}
# execute a specific script by name or index
function _exec() {
if [[ "$1" =~ ^[0-9]+$ ]]; then
script=$(find "${DIR_COLORSCRIPTS}" -maxdepth 1 -type f ! -path "${DIR_BLACKLIST}/*" | sed -n "${1}p")
else
script="${DIR_COLORSCRIPTS}/$1"
fi
if [[ -f "$script" ]]; then
exec "$script"
else
echo "Error: Script not found."
exit 1
fi
}
# run all scripts
function _run_all() {
for script in "${DIR_COLORSCRIPTS}"/*; do
[[ -f "$script" && ! "$script" =~ blacklisted ]] || continue
echo "$(basename "$script"):"
"$script"
echo
done
}
# blacklist a script
function _blacklist() {
if [[ "$1" =~ ^[0-9]+$ ]]; then
script=$(find "${DIR_COLORSCRIPTS}" -maxdepth 1 -type f ! -path "${DIR_BLACKLIST}/*" | sed -n "${1}p")
else
script="${DIR_COLORSCRIPTS}/$1"
fi
if [[ -f "$script" ]]; then
mv "$script" "${DIR_BLACKLIST}"
echo "Script $(basename "$script") has been blacklisted."
else
echo "Error: Script not found."
fi
}
# unblacklist a script
function _unblacklist() {
script="${DIR_BLACKLIST}/$1"
if [[ -f "$script" ]]; then
mv "$script" "${DIR_COLORSCRIPTS}"
echo "Script $(basename "$script") has been unblacklisted."
else
echo "Error: Script not found in blacklist."
fi
}
# parse command-line arguments
VERSION="1.0.0"
case "$1" in
-h|--help|help)
_help
;;
-l|--list|list)
_list
;;
-r|--random|random)
_random
;;
-e|--exec|exec)
_exec "$2"
;;
-a|--all|all)
_run_all
;;
-b|--blacklist|blacklist)
_blacklist "$2"
;;
-u|--unblacklist|unblacklist)
_unblacklist "$2"
;;
-v|--version|version)
echo "colorscripts version $VERSION"
;;
*)
echo "Invalid option. Use -h or --help for usage."
exit 1
;;
esac