-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper_func.sh
83 lines (65 loc) · 2.6 KB
/
helper_func.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
#!/bin/bash
center_text() {
local terminal_width
terminal_width=$(tput cols) # query the Terminfo database: number of columns
local text="${1:?}" # text to center
local glyph="${2:-=}" # glyph to compose the border
local padding="${3:-2}" # spacing around the text
local text_width=${#text}
local border_width=$(((terminal_width - (padding * 2) - text_width) / 2))
local border= # shape of the border
# create the border (left side or right side)
for ((i = 0; i < border_width; i++)); do
border+="${glyph}"
done
# a side of the border may be longer (e.g. the right border)
if (((terminal_width - (padding * 2) - text_width) % 2 == 0)); then
# the left and right borders have the same width
local left_border=$border
local right_border=$left_border
else
# the right border has one more character than the left border
# the text is aligned leftmost
local left_border=$border
local right_border="${border}${glyph}"
fi
# space between the text and borders
local spacing=
for ((i = 0; i < padding; i++)); do
spacing+=" "
done
# displays the text in the center of the screen, surrounded by borders.
printf "${left_border}${spacing}${text}${spacing}${right_border}\n"
}
center_text_prompt() {
local terminal_width
terminal_width=$(tput cols) # query the Terminfo database: number of columns
local text="${1:?}" # text to center
local glyph="${2:-=}" # glyph to compose the border
local padding="${3:-2}" # spacing around the text
local text_width=${#text}
local border_width=$(((terminal_width - (padding * 2) - text_width) / 2))
local border= # shape of the border
# create the border (left side or right side)
for ((i = 0; i < border_width; i++)); do
border+="${glyph}"
done
# a side of the border may be longer (e.g. the right border)
if (((terminal_width - (padding * 2) - text_width) % 2 == 0)); then
# the left and right borders have the same width
local left_border=$border
local right_border=$left_border
else
# the right border has one more character than the left border
# the text is aligned leftmost
local left_border=$border
local right_border="${border}${glyph}"
fi
# space between the text and borders
local spacing=
for ((i = 0; i < padding; i++)); do
spacing+=" "
done
# displays the text in the center of the screen, surrounded by borders.
printf "${left_border}${spacing}${text}"
}