Skip to content

Commit a2839bd

Browse files
committed
Padding space with printf
1 parent 9718b4f commit a2839bd

File tree

1 file changed

+26
-65
lines changed

1 file changed

+26
-65
lines changed

args.sh

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ args_clean() {
4848
__ARGS[argument.size]=0
4949
# optional argument
5050
__ARGS[option.size]=0
51-
# indicate if argument is sorted
51+
# indicate if arguments are sorted
5252
__ARGS[sorted]="false"
5353
return 0
5454
}
@@ -1069,6 +1069,8 @@ args_usage_line() {
10691069
str="usage: ${1##*/}"
10701070
fi
10711071
local usage_basename_length="${#str}"
1072+
local jump_spaces
1073+
printf -v jump_spaces "%*s" "${usage_basename_length}" ""
10721074
current_col="${usage_basename_length}"
10731075
i=0
10741076
while [[ "${i}" -lt "${__ARGS[option.size]}" ]]; do
@@ -1108,11 +1110,7 @@ args_usage_line() {
11081110
if [[ "$((current_col + ${#option}))" -gt "${max_col}" ]]; then
11091111
has_max_col="true"
11101112
str+=$'\n'
1111-
j=0
1112-
while [[ "${j}" -lt "${usage_basename_length}" ]]; do
1113-
str+=" "
1114-
j=$((j + 1))
1115-
done
1113+
str+="${jump_spaces}"
11161114
current_col="${usage_basename_length}"
11171115
fi
11181116
str+="${option}"
@@ -1122,18 +1120,10 @@ args_usage_line() {
11221120
if [[ "${__ARGS[argument.size]}" -ne 0 ]]; then
11231121
if [[ "true" == "${has_max_col}" ]] || [[ "$((current_col + 3))" -gt "${max_col}" ]]; then
11241122
str+=$'\n'
1125-
j=0
1126-
while [[ "${j}" -lt "${usage_basename_length}" ]]; do
1127-
str+=" "
1128-
j=$((j + 1))
1129-
done
1123+
str+="${jump_spaces}"
11301124
str+=" --"
11311125
str+=$'\n'
1132-
j=0
1133-
while [[ "${j}" -lt "${usage_basename_length}" ]]; do
1134-
str+=" "
1135-
j=$((j + 1))
1136-
done
1126+
str+="${jump_spaces}"
11371127
current_col="${usage_basename_length}"
11381128
else
11391129
str+=" --"
@@ -1159,11 +1149,7 @@ args_usage_line() {
11591149
fi
11601150
if [[ "$((current_col + ${#option}))" -gt "${max_col}" ]]; then
11611151
str+=$'\n'
1162-
j=0
1163-
while [[ "${j}" -lt "${usage_basename_length}" ]]; do
1164-
str+=" "
1165-
j=$((j + 1))
1166-
done
1152+
str+="${jump_spaces}"
11671153
current_col="${usage_basename_length}"
11681154
fi
11691155
str+="${option}"
@@ -1189,6 +1175,11 @@ args_usage() {
11891175
local max_col
11901176
local current_col=0
11911177
local has_max_col="false"
1178+
local jump_spaces
1179+
local jump_spaces_padding
1180+
local jump_spaces_helper
1181+
printf -v jump_spaces_padding "%*s" "${__ARGS[usage.width.padding]}" ""
1182+
printf -v jump_spaces_helper "%*s" "$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))" ""
11921183
max_col="$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} + ${__ARGS[usage.width.help]}))"
11931184
args_usage_line "$1"
11941185
str=""
@@ -1204,11 +1195,7 @@ args_usage() {
12041195
fi
12051196
i=0
12061197
while [[ "${i}" -lt "${__ARGS[argument.size]}" ]]; do
1207-
j=0
1208-
while [[ "${j}" -lt "${__ARGS[usage.width.padding]}" ]]; do
1209-
str+=" "
1210-
j=$((j + 1))
1211-
done
1198+
str+="${jump_spaces_padding}"
12121199
local option=""
12131200
if [[ -n "${__ARGS[argument.${i}.choices]}" ]]; then
12141201
option+="{${__ARGS[argument.${i}.choices]// /,}}"
@@ -1219,19 +1206,12 @@ args_usage() {
12191206
if [[ -n "${__ARGS[argument.${i}.help]}" ]]; then
12201207
if [[ "${#option}" -gt "${__ARGS[usage.width.argument]}" ]]; then
12211208
str+=$'\n'
1222-
j=0
1223-
while [[ "${j}" -lt "$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))" ]]; do
1224-
str+=" "
1225-
j=$((j + 1))
1226-
done
1209+
str+="${jump_spaces_helper}"
12271210
else
1228-
j=0
1229-
while [[ "${j}" -lt "$((${__ARGS[usage.width.argument]} - ${#option} + ${__ARGS[usage.width.separator]} - 1))" ]]; do
1230-
str+=" "
1231-
j=$((j + 1))
1232-
done
1211+
printf -v jump_spaces "%*s" "$((${__ARGS[usage.width.argument]} - ${#option} + ${__ARGS[usage.width.separator]} - 1))" ""
1212+
str+="${jump_spaces}"
12331213
fi
1234-
current_col="$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))"
1214+
current_col="${#jump_spaces_helper}"
12351215
# save last IFS
12361216
local old_ifs
12371217
old_ifs="${IFS}"
@@ -1240,12 +1220,8 @@ args_usage() {
12401220
for word in ${__ARGS[argument.${i}.help]}; do
12411221
if [[ "$((current_col + ${#word} + 1))" -gt "$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} + ${__ARGS[usage.width.help]}))" ]]; then
12421222
str+=$'\n'
1243-
j=0
1244-
while [[ "${j}" -lt "$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))" ]]; do
1245-
str+=" "
1246-
j=$((j + 1))
1247-
done
1248-
current_col="$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))"
1223+
str+="${jump_spaces_helper}"
1224+
current_col="${#jump_spaces_helper}"
12491225
fi
12501226
str+=" ${word}"
12511227
current_col="$((current_col + ${#word} + 1))"
@@ -1262,11 +1238,7 @@ args_usage() {
12621238
fi
12631239
i=0
12641240
while [[ "${i}" -lt "${__ARGS[option.size]}" ]]; do
1265-
j=0
1266-
while [[ "${j}" -lt "${__ARGS[usage.width.padding]}" ]]; do
1267-
str+=" "
1268-
j=$((j + 1))
1269-
done
1241+
str+="${jump_spaces_padding}"
12701242
local option=""
12711243
local type
12721244
for type in "short" "long"; do
@@ -1305,19 +1277,12 @@ args_usage() {
13051277
if [[ -n "${__ARGS[option.${i}.help]}" ]]; then
13061278
if [[ "${#option}" -gt "${__ARGS[usage.width.argument]}" ]]; then
13071279
str+=$'\n'
1308-
j=0
1309-
while [[ "${j}" -lt "$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))" ]]; do
1310-
str+=" "
1311-
j=$((j + 1))
1312-
done
1280+
str+="${jump_spaces_helper}"
13131281
else
1314-
j=0
1315-
while [[ "${j}" -lt "$((${__ARGS[usage.width.argument]} - ${#option} + ${__ARGS[usage.width.separator]} - 1))" ]]; do
1316-
str+=" "
1317-
j=$((j + 1))
1318-
done
1282+
printf -v jump_spaces "%*s" "$((${__ARGS[usage.width.argument]} - ${#option} + ${__ARGS[usage.width.separator]} - 1))" ""
1283+
str+="${jump_spaces}"
13191284
fi
1320-
current_col="$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))"
1285+
current_col="${#jump_spaces_helper}"
13211286
# save last IFS
13221287
local old_ifs
13231288
old_ifs="${IFS}"
@@ -1326,12 +1291,8 @@ args_usage() {
13261291
for word in ${__ARGS[option.${i}.help]}; do
13271292
if [[ "$((current_col + ${#word} + 1))" -gt "$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} + ${__ARGS[usage.width.help]}))" ]]; then
13281293
str+=$'\n'
1329-
j=0
1330-
while [[ "${j}" -lt "$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))" ]]; do
1331-
str+=" "
1332-
j=$((j + 1))
1333-
done
1334-
current_col="$((${__ARGS[usage.width.padding]} + ${__ARGS[usage.width.argument]} + ${__ARGS[usage.width.separator]} - 1))"
1294+
str+="${jump_spaces_helper}"
1295+
current_col="${#jump_spaces_helper}"
13351296
fi
13361297
str+=" ${word}"
13371298
current_col="$((current_col + ${#word} + 1))"

0 commit comments

Comments
 (0)