-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolorscript.sh
More file actions
executable file
·112 lines (101 loc) · 3.01 KB
/
Copy pathcolorscript.sh
File metadata and controls
executable file
·112 lines (101 loc) · 3.01 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
#!/usr/bin/env bash
# Simple CLI for shell-color-scripts
DIR_COLORSCRIPTS="/opt/shell-color-scripts/colorscripts"
fmt_help=" %-20s\t%-54s\n"
# Find ls command
if [ -x "/bin/ls" ]; then
LS_CMD="/bin/ls"
elif [ -x "/usr/bin/ls" ]; then
LS_CMD="/usr/bin/ls"
else
echo "Error: Could not find ls command"
exit 1
fi
# Get list of scripts and count
if [ -d "${DIR_COLORSCRIPTS}" ]; then
list_colorscripts="$(${LS_CMD} "${DIR_COLORSCRIPTS}" | cut -d ' ' -f 1 | nl)"
length_colorscripts="$(${LS_CMD} "${DIR_COLORSCRIPTS}" | wc -l)"
else
echo "Error: Color scripts directory not found"
exit 1
fi
function _help() {
echo "Description: A collection of terminal color scripts."
echo ""
echo "Usage: colorscript [OPTION] [SCRIPT NAME/INDEX]"
printf "${fmt_help}" \
"-h, --help, help" "Print this help." \
"-l, --list, list" "List all installed color scripts." \
"-r, --random, random" "Run a random color script." \
"-e, --exec, exec" "Run a specified color script by SCRIPT NAME or INDEX."
}
function _list() {
echo "There are ${length_colorscripts} installed color scripts:"
echo "${list_colorscripts}"
}
function _random() {
if [ "$length_colorscripts" -gt 0 ]; then
declare -i random_index=$(($RANDOM % $length_colorscripts + 1))
random_colorscript="$(echo "${list_colorscripts}" | sed -n ${random_index}p \
| tr -d ' ' | tr '\t' ' ' | cut -d ' ' -f 2)"
if [ -n "$random_colorscript" ]; then
exec "${DIR_COLORSCRIPTS}/${random_colorscript}"
else
echo "Error: Could not select random script"
exit 1
fi
else
echo "Error: No color scripts found"
exit 1
fi
}
function ifhascolorscipt() {
[[ -e "${DIR_COLORSCRIPTS}/$1" ]] && echo "Has this color script."
}
function _run_by_name() {
if [[ "$1" == "random" ]]; then
_random
elif [[ -n "$(ifhascolorscipt "$1")" ]]; then
exec "${DIR_COLORSCRIPTS}/$1"
else
echo "Input error, Don't have color script named $1."
exit 1
fi
}
function _run_by_index() {
if [[ "$1" -gt 0 && "$1" -le "${length_colorscripts}" ]]; then
colorscript="$(echo "${list_colorscripts}" | sed -n ${1}p \
| tr -d ' ' | tr '\t' ' ' | cut -d ' ' -f 2)"
if [ -n "$colorscript" ]; then
exec "${DIR_COLORSCRIPTS}/${colorscript}"
else
echo "Error: Could not find script at index $1"
exit 1
fi
else
echo "Input error, Don't have color script indexed $1."
exit 1
fi
}
case "$1" in
-h|--help|help)
_help
;;
-l|--list|list)
_list
;;
-r|--random|random)
_random
;;
-e|--exec|exec)
[[ -z "$2" ]] && echo "Input error, see help." && exit 1
if [[ "$2" =~ ^[0-9]+$ ]]; then
_run_by_index "$2"
else
_run_by_name "$2"
fi
;;
*)
echo "Input error, see help." && exit 1
;;
esac