-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-mgr-common-functions.zsh
379 lines (310 loc) · 9.1 KB
/
zsh-mgr-common-functions.zsh
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/zsh
if [ "$ZSH_FUNCTIONS_ZSH" != yes ]; then
ZSH_FUNCTIONS_ZSH=yes
else
return 0
fi
# Prints a message given a max length. It fills the remaining space with "#"
# $1: Message to be printed
# $2: Max length of the message (including the message itself)
# $3: Character to fill the remaining space. It can be colored
# $4 (optional): Message length. Useful when it has ANSI escape codes, since it detects them as characters.
print_message() {
local MSG_LENGTH=${#1}
[ $# -eq 4 ] && MSG_LENGTH="$4"
local -r MAX_LENGTH="$2"
local -r HASHTAG_NRO=$(((MAX_LENGTH - MSG_LENGTH - 2) / 2))
printf "\n"
printf "%0.s$3" $(seq 1 "$2")
printf "\n"
printf "%0.s$3" $(seq 1 "$HASHTAG_NRO")
if [ $((MSG_LENGTH % 2)) -ne 0 ]; then
printf " %b " "$1"
else
printf " %b " "$1"
fi
printf "%0.s$3" $(seq 1 "$HASHTAG_NRO")
printf "\n"
printf "%0.s$3" $(seq 1 "$2")
printf "\n"
}
# Expands possible aliases to home. Also included in install script.
# $1: Path
# return: Returns an expanded string
_expand_home() {
echo "${1//(\~|\$HOME)/"$HOME"}"
}
# Converts a path to file to an absolute one.
#
# $1: The path to file to be converted.
#
# return: Just the absolute path to the file, without the filename.
#
# note: The given path does not have to exist in order to work.
_absolute_dirname(){
local dirname_var
local -r FILENAME=$(basename "$1")
dirname_var=$(_expand_home "$1") # Expands all home references
dirname_var=${dirname_var/%\//} # Strips the last "/"
dirname_var=${dirname_var/%"\/$FILENAME"/} # Removes the filename
[ "$dirname_var" = "" ] && dirname_var="/" # Adds back the root directory
echo "$dirname_var"
}
_sanitize_location(){
local dirname_var
dirname_var=$(_expand_home "$1") # Expands all home references
dirname_var=${dirname_var/%\//} # Strips the last "/"
[ "$dirname_var" = "" ] && dirname_var="/" # Adds back the root directory
echo "$dirname_var"
}
# Gets the column length from character separated string.
#
# $1: String.
#
# $2: Delimiter used in $1.
#
# return: The number of of columns necessary.
#
# Example of entry: hello:hola:bonjour
_get_column_length(){
local -r DELIM="$2"
local -r RES=$((${#${1//[^$DELIM]/}} + 1))
echo $RES
}
# Gets the string saved at column number.
#
# $1: Text separated by delimiter.
#
# $2: Column number.
#
# $3: Delimiter.
#
# Example of entry: hello:hola:bonjour
_get_column_text_at(){
local -r TEXT="$1"
local -r COL_NUM="$2"
local -r DELIM="$3"
echo "$TEXT" | cut -d "$DELIM" -f "$COL_NUM"
}
# Gets the highest character count on every column.
#
# $1: Table, encoded using delimiter for columns and newlines for rows.
#
# $2: Delimiter for columns.
#
# $3 (Optional): Number to be added to final result.
#
# return: The maximum character length of every column. Using the same delimiter.
#
# Output example fro 3 columns: 1:2:3
#
# example: 1:2:3\n11:22:33\ncomo:1:4\n33:testing:33
# _max_char_table "1:2:3\n11:22:33\ncomo:1:4\n33:testing:33" ":"
#
#
_max_char_table(){
# Only works in zsh
local -r DELIM="$2"
# local -r TABLE=("${(@f)$(echo "$1")}")
TABLE=("${(@f)$(echo "$1")}")
local -r FIL="${#TABLE[@]}"
local -r COL=$(_get_column_length "${TABLE[1]}" "$DELIM")
local max
local res=""
local entry
local entry_len
local extra="0"
[ "$#" -eq "3" ] && extra="$3"
# echo -e "${TABLE[1]}\nY su tamaño es: $FIL\nY el otro es: $COL"
local j
local i
# {1..$FIL}: Only works in zsh
for j in {1..$COL}
do
max="-1"
entry_len="-1"
for i in {1..$FIL}
do
entry=$(_get_column_text_at "${TABLE[$i]}" "$j" "$DELIM")
entry_len=${#entry}
[ "$entry_len" -gt "$max" ] && max="$entry_len"
done
unset i
max=$((max+extra))
if [ "$j" -eq "$COL" ]; then
res+="$max"
else
res+="$max$DELIM"
fi
done
unset j
echo "$res"
}
# Colors a row with the given color.
#
# $1: String to be colored.
#
# $2: Delimiter.
#
# $3: Color.
# _color_row "123:33:try me:fecha" ":" "$RED"
_color_row(){
local -r ROW="$1"
local -r DELIM="$2"
local -r COLOR="$3"
local -r COL_NUM=$(_get_column_length "$ROW" "$DELIM")
local res=""
local aux
local i
for i in {1..$COL_NUM}
do
if [ "$i" -eq "$COL_NUM" ];then
aux=$(_get_column_text_at "$ROW" "$i" "$DELIM")
res+="${COLOR}$aux${NO_COLOR}"
else
aux=$(_get_column_text_at "$ROW" "$i" "$DELIM")
res+="${COLOR}$aux${NO_COLOR}$DELIM"
fi
done
unset i
echo -e "$res"
}
# Changes column entry to specified string.
#
# $1: Row to be changed.
#
# $2: Column position.
#
# $3: New string.
#
# $4: Delimiter.
_change_column_entry(){
local -r ROW="$1"
local -r COL_POS="$2"
local -r NEW_STR="$3"
local -r DELIM="$4"
local -r COL_NUM=$(_get_column_length "$ROW" "$DELIM")
local res
if [ "$COL_POS" -gt "$COL_NUM" ];
then
echo "$ROW"
return 0
fi
local i
for i in {1..$COL_NUM}
do
if [ "$i" -eq "$COL_POS" ];
then
res+="$NEW_STR"
else
res+=$(_get_column_text_at "$ROW" "$i" "$DELIM")
fi
if [ "$i" -ne "$COL_NUM" ];
then
res+="$DELIM"
fi
done
unset i
echo "$res"
}
# Creates a table with the array passed as input. It can be colored.
#
# $1: Array with every entry separated by a delimiter and newlines.
# Example: (hello:hola:bonjour bye:adios:aurevoir)
#
# $2: Delimiter
#
# $3: Table color. Leave empty or empty string to paint it in the default terminal color.
#
# $4 (Optional): Same array as $1, but colored. It only needs to be used when you want color in the output.
#
# _create_table "hola:queso:adios" ":" ""
# _create_table "hola:queso:adios\nhola:bola:cola" ":" ""
# _create_table "hola:queso:adios\nhola:bola:colaxddasdasdd" ":" ""
_create_table(){
local -r RAW_TABLE=("${(@f)$(echo "$1")}")
local -r DELIM="$2"
local -r MAX_CHAR_COL=$(_max_char_table "$1" "$DELIM" "2")
local -r COL_NUM=$(_get_column_length "${RAW_TABLE[1]}" "$DELIM")
local i
if [ "${#RAW_TABLE[@]}" -eq "0" ];
then
echo "${RED}No array found${NO_COLOR}"
return 1
fi
local -r COLOR="${3:-"${NO_COLOR}"}"
# Check if there is colored text
if [ "$#" -eq "4" ];
then
local -r TABLE=("${(@f)$(echo "$4")}")
else
local -r TABLE=("${RAW_TABLE[@]}")
fi
# Top of table
printf "${COLOR}┌${NO_COLOR}"
for i in {1..$COL_NUM}
do
printf "%0.s${COLOR}─${NO_COLOR}" $(seq 1 "$(_get_column_text_at "$MAX_CHAR_COL" "$i" "$DELIM")")
[ "$COL_NUM" -gt "1" ] && [ "$i" -lt "$COL_NUM" ] && printf "${COLOR}┬${NO_COLOR}"
done
unset i
printf "${COLOR}┐${NO_COLOR}\n"
# Content of table
local spaces="0"
local max_length="0"
local msg_length="0"
local -r SPACE_CHAR=" "
local j
local k
for i in {1..${#TABLE[@]}}
do
printf "${COLOR}│${NO_COLOR}"
for j in {1..$COL_NUM}
do
# Max character length of this column
max_length=$(_get_column_text_at "$MAX_CHAR_COL" "$j" "$DELIM" )
# Message length for this cell
msg_length=$(_get_column_text_at "${RAW_TABLE[$i]}" "$j" "$DELIM")
msg_length=${#msg_length}
# Number of spaces needed
spaces=$(( ( max_length - msg_length) / 2 ))
printf "%0.s$SPACE_CHAR" $(seq 1 $spaces)
printf "%b" "$(_get_column_text_at "${TABLE[$i]}" "$j" "$DELIM")"
# If there was a remainder, we add 1 to the number of spaces
(( (( max_length - msg_length) % 2) != 0 )) && spaces=$(( spaces + 1 ))
printf "%0.s$SPACE_CHAR" $(seq 1 $spaces)
printf "${COLOR}│${NO_COLOR}"
done
unset j
# Print the middle line
printf "\n"
if [ "$i" -lt "${#TABLE[@]}" ];
then
printf "${COLOR}├${NO_COLOR}"
for k in {1..$COL_NUM}
do
printf "%0.s${COLOR}─${NO_COLOR}" $(seq 1 "$(_get_column_text_at "$MAX_CHAR_COL" "$k" "$DELIM")")
[ "$COL_NUM" -gt "1" ] && [ "$k" -lt "$COL_NUM" ] && printf "${COLOR}┼${NO_COLOR}"
done
unset k
printf "${COLOR}┤${NO_COLOR}\n"
fi
done
unset i
# Bottom of table
printf "${COLOR}└${NO_COLOR}"
for i in {1..$COL_NUM}
do
printf "%0.s${COLOR}─${NO_COLOR}" $(seq 1 "$(_get_column_text_at "$MAX_CHAR_COL" "$i" "$DELIM")")
[ "$COL_NUM" -gt "1" ] && [ "$i" -lt "$COL_NUM" ] && printf "${COLOR}┴${NO_COLOR}"
done
unset i
printf "${COLOR}┘${NO_COLOR}\n"
}
# Checks if a command exists.
# $1: Command name.
# return: returns 0 if the command exists. 1 otherwise.
check_cmd_exists(){
local -r CMD="$1"
command -v "$CMD" > /dev/null
}