-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshuz.sh
182 lines (148 loc) · 3.43 KB
/
shuz.sh
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
# Environment
# ------------------------------------------
# Stores sourcing script location in script_dir variable
script_dir=$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd)
# Terminal colors
# ------------------------------------------
# Clears styling
noc='\033[0m'
# Foreground colors
bold='\033[0;1m'
black='\033[0;30m'
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
magenta='\033[0;35m'
cyan='\033[0;36m'
white='\033[0;37m'
# Complex colors declaration magic
fg_names=("bold" "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white")
fg_vals=(1 30 31 32 33 34 35 36 37)
bg_names=("none" "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white")
bg_vals=(0 40 41 42 43 44 45 46 47)
for ((fg=0; fg < ${#fg_names[*]}; fg++)); do
for ((bg=0; bg < ${#bg_names[*]}; bg++)); do
declare "${fg_names[fg]}_on_${bg_names[bg]}"="\\033[${bg_vals[bg]};${fg_vals[fg]}m"
done
done
# Draws a table of the available coloring options
color_map () {
# Draw the column titles
ecn "▼FG BG►│"
for ((bg=0; bg < ${#bg_names[*]}; bg++)); do
printf ' %8s ' "${bg_names[bg]}"
done
br
ec "─────────┼──────────────────────────────────────────────────────────────────────────────────────────"
for ((fg=0; fg < ${#fg_names[*]}; fg++)); do
printf '%-8s │' "${fg_names[fg]}"
for ((bg=0; bg < ${#bg_names[*]}; bg++)); do
eval color="\$${fg_names[fg]}_on_${bg_names[bg]}"
ecn " ${color} ******* ${noc}"
done
br
done
}
# Configuration
# ------------------------------------------
# Changing these value to affect the logging functions
SUCCESS_COLOR=${green}
WARN_COLOR=${yellow}
ERROR_COLOR=${red}
# Change this value to define the indentation size
INDENTATION=' '
# Echos
# ------------------------------------------
##
# More consistent echo.
#
ec() {
IFS=' ' printf "%b\n" "$*"
}
##
# More consistent echo; without the new line.
#
ecn() {
IFS=' ' printf "%b" "$*"
}
##
# Outputs new line.
# No parameters.
#
br() {
ec ''
}
##
# Informs that everything goes as planned.
#
success() {
ec "${SUCCESS_COLOR}$*${noc}"
}
##
# Outputs a menasing message.
#
warn() {
ec "${WARN_COLOR}$*${noc}"
}
##
# Outputs a scary message.
#
error() {
>&2 ec "${ERROR_COLOR}ERROR: $*${noc}"
}
##
# Outputs a message and kills the script.
#
fail() {
error "$@"
exit 1
}
# Text manipulation
# ------------------------------------------
##
# Reads multiline text from stdin into the specified variable.
#
# Parameters:
# 1: target variable name
#
multiline() {
IFS= read -r -d '' $1
}
##
# Reads multiline text from stdin and outputs it indented.
#
indent() {
sed "s/^/${INDENTATION}/"
}
# User input
# ------------------------------------------
##
# Ask user a yes/no question.
# Aborts the script if the answer is no.
#
# Parameters:
# Interpreted as the question text to be presented to the user.
#
are_you_sure() {
printf "${WARN_COLOR}$@${noc}"
read -p " (y/n) " -n 1
printf "\n"
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
return 0
fi
return 1
}
#
# ------------------------------------------
##
# Exit if the previous command did not succeed.
#
# Parameters:
# Interpreted as the failure massage to be presented to the user.
#
assert() {
if [[ "$?" != "0" ]]; then
fail "$@"
fi
}