Skip to content

Commit b91a7e6

Browse files
akalipetisAntonis Kalipetis
andauthored
feat(installer): fix styling to scale according to the name width (#282)
Co-authored-by: Antonis Kalipetis <[email protected]>
1 parent 7a9fea8 commit b91a7e6

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

installer.sh

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,65 @@ function output {
9696
builtin echo -e "${style_start}${1}${style_end}"
9797
}
9898

99+
function create_table_line() {
100+
local text="${1:-}"
101+
local width="${2:-}"
102+
103+
if [ -z "$width" ]; then
104+
# Calculate width based on text length, minimum 50
105+
local text_length=${#text}
106+
width=$((text_length < 50 ? 50 : text_length + 4))
107+
fi
108+
109+
if [ "$text" = "border" ] || [ -z "$text" ]; then
110+
# Border line
111+
printf "+"
112+
for ((i=0; i<width-2; i++)); do
113+
printf "-"
114+
done
115+
printf "+"
116+
elif [ "$text" = "empty" ]; then
117+
# Empty line
118+
printf "|"
119+
for ((i=0; i<width-2; i++)); do
120+
printf " "
121+
done
122+
printf "|"
123+
else
124+
# Text line
125+
local text_length=${#text}
126+
local padding=$(((width - 2 - text_length) / 2))
127+
local right_padding=$((width - 2 - text_length - padding))
128+
printf "|"
129+
for ((i=0; i<padding; i++)); do
130+
printf " "
131+
done
132+
printf "%s" "$text"
133+
for ((i=0; i<right_padding; i++)); do
134+
printf " "
135+
done
136+
printf "|"
137+
fi
138+
printf "\n"
139+
}
140+
141+
function create_table() {
142+
local text="$1"
143+
local min_width="${2:-50}"
144+
local text_length=${#text}
145+
local table_width=$((text_length < min_width ? min_width : text_length + 4))
146+
147+
create_table_line "border" "$table_width"
148+
create_table_line "empty" "$table_width"
149+
create_table_line "$text" "$table_width"
150+
create_table_line "empty" "$table_width"
151+
create_table_line "border" "$table_width"
152+
}
153+
99154
function exit_with_error() {
100-
output "+--------------------------------------------------+" "error"
101-
output "| |" "error"
102-
output "| Installation failed |" "error"
103-
output "| |" "error"
104-
output "+--------------------------------------------------+" "error"
155+
local title="Installation failed"
156+
157+
output "$(create_table "$title")" "error"
105158

106159
output "\nGet help with your $vendor_name CLI installation:" "heading"
107160
output " Inspect the logs: ${INSTALL_LOG}"
@@ -112,22 +165,18 @@ function exit_with_error() {
112165
}
113166

114167
function intro() {
115-
output "+--------------------------------------------------+" "heading"
116-
output "| |" "heading"
117-
output "| $vendor_name CLI Installer |" "heading"
118-
output "| |" "heading"
119-
output "+--------------------------------------------------+" "heading"
168+
local title="$vendor_name CLI Installer"
169+
170+
output "$(create_table "$title")" "heading"
120171

121172
output "\nChecking environment" "heading"
122173
}
123174

124175
function outro() {
176+
local title="$vendor_name CLI has been installed successfully."
177+
125178
output ""
126-
output "+--------------------------------------------------+" "success"
127-
output "| |" "success"
128-
output "| $vendor_name CLI has been installed successfully. |" "success"
129-
output "| |" "success"
130-
output "+--------------------------------------------------+" "success"
179+
output "$(create_table "$title")" "success"
131180

132181
output "\nWhat's next?" "heading"
133182

0 commit comments

Comments
 (0)