-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuz.sh
243 lines (205 loc) · 5.68 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
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
# https://github.com/yanivmo/shuz
#
# ██████ ██░ ██ █ ██ ▒███████▒
# ▒██ ▒ ▓██░ ██▒ ██ ▓██▒▒ ▒ ▒ ▄▀░
# ░ ▓██▄ ▒██▀▀██░▓██ ▒██░░ ▒ ▄▀▒░
# ▒ ██▒░▓█ ░██ ▓▓█ ░██░ ▄▀▒ ░
# ▒██████▒▒░▓█▒░██▓▒▒█████▓ ▒███████▒
# ▒ ▒▓▒ ▒ ░ ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░▒▒ ▓░▒░▒
# ░ ░▒ ░ ░ ▒ ░▒░ ░░░▒░ ░ ░ ░░▒ ▒ ░ ▒
# ░ ░ ░ ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░
# ░ ░ ░ ░ ░ ░ ░
# ░
# ----------------[ v2.0.1 ]----------------
# Shell utilz for ergonomic shell scripting.
#
# Environment
# ------------------------------------------
# THIS_OS is "macos" on Mac and "linux" everywhere else
# naive but works for my simple needs :)
[ "$(uname)" = "Darwin" ] && THIS_OS=macos || THIS_OS=linux
readonly THIS_OS
# THIS_SHELL is "zsh" on Zsh and "bash" everywhere else
# even more naive, sorry -\_(ツ)_/-
[ -n "$ZSH_VERSION" ] && THIS_SHELL=zsh || THIS_SHELL=bash
readonly THIS_SHELL
# Terminal colors
# ------------------------------------------
# Clears styling
readonly noc='\033[0m'
# Foreground colors
readonly bold='\033[0;1m'
readonly black='\033[0;30m'
readonly red='\033[0;31m'
readonly green='\033[0;32m'
readonly yellow='\033[0;33m'
readonly blue='\033[0;34m'
readonly magenta='\033[0;35m'
readonly cyan='\033[0;36m'
readonly 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
shuz::ecn "▼FG BG►│"
for ((bg=0; bg < ${#bg_names[*]}; bg++)); do
printf ' %8s ' "${bg_names[bg]}"
done
shuz::br
shuz::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]}"
shuz::ecn " ${color} ******* ${noc}"
done
shuz::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.
#
shuz::ec() {
IFS=' ' printf "%b\n" "$*"
}
##
# More consistent echo; without the new line.
#
shuz::ecn() {
IFS=' ' printf "%b" "$*"
}
##
# Outputs new line.
# No parameters.
#
shuz::br() {
shuz::ec ''
}
##
# Informs that everything goes as planned.
#
shuz::success() {
shuz::ec "${SUCCESS_COLOR}$*${noc}"
}
##
# Outputs a menasing message.
#
shuz::warn() {
shuz::ec "${WARN_COLOR}$*${noc}"
}
##
# Outputs a scary message.
#
shuz::error() {
>&2 shuz::ec "${ERROR_COLOR}ERROR: $*${noc}"
}
##
# Outputs a message and kills the script.
#
shuz::fail() {
shuz::error "$@"
exit 1
}
# Text manipulation
# ------------------------------------------
##
# Reads multiline text from stdin into the specified variable.
#
# Parameters:
# 1: target variable name
#
shuz::multiline() {
IFS= read -r -d '' $1
}
##
# Reads multiline text from stdin and outputs it indented.
#
shuz::indent() {
sed "s/^/${INDENTATION}/"
}
# User input
# ------------------------------------------
##
# Read one character from stdin.
#
# Parameters:
# The prompt to show the user.
#
shuz::read_char() {
if [ "${THIS_SHELL}" = "zsh" ]; then
read -r -q "READ_CHAR?$@"
else
read -p "$@" -r -n 1 READ_CHAR
fi
shuz::ecn "${READ_CHAR}"
}
##
# 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.
#
shuz::are_you_sure() {
local char
shuz::ecn "${WARN_COLOR}$@${noc}"
char=$(shuz::read_char " (y/n) ")
shuz::br
if [[ "${char}" =~ ^[Yy]$ ]]; then
return 0
fi
shuz::fail ABORTED
}
# Assertions and expectations
# ------------------------------------------
##
# Kill the script with error code if the previous command did not succeed.
#
# Parameters:
# Interpreted as the failure massage to be presented to the user.
#
shuz::assert_success() {
if [[ "$?" != "0" ]]; then
shuz::fail "$@"
fi
}
##
# Returns error if the requiested command could not be found.
#
# Parameters:
# The command to search for. Could be a shell built-in command
# or an executable found in PATH.
#
shuz::expect_command() {
command -v "$@" &> /dev/null
}
##
# Kill the script with error code if the requiested command could
# not be found.
#
# Parameters:
# The command to search for. Could be a shell built-in command
# or an executable found in PATH.
#
shuz::assert_command() {
shuz::expect_command "$@" || fail "${noc}Failed to find command ${ERROR_COLOR}$@${noc}"
}