Skip to content

Commit 2f413f2

Browse files
committed
lib/format: Change @go.pad_items interface
Seems more natural to pass the items as positional parameters.
1 parent 750febb commit 2f413f2

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

lib/format

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Assigns `printf` transformations of its arguments to an array
88
#
99
# @go.pad_items
10-
# Pads each string in an array to match the length of the longest element
10+
# Right-pads each string with spaces to match the length of the longest
1111
#
1212
# @go.zip_items
1313
# Concatenates parallel elements from each input array
@@ -44,26 +44,26 @@
4444
@go.split "$__go_array_printf_delim" "$__tmp_go_array_printf" "$1"
4545
}
4646

47-
# Right-pads each string in an array to match the length of the longest element
47+
# Right-pads each string with spaces to match the length of the longest
4848
#
4949
# Globals:
5050
# _GO_ARRAY_PRINTF_DELIMITER: See the comments for `@go.array_printf`
5151
#
5252
# Arguments:
53-
# items: Name of the input array in the caller's scope
5453
# result: Name of the caller-declared output array
54+
# ...: Items to right-pad with spaces to match the longest one
5555
@go.pad_items() {
56-
@go.validate_identifier_or_die 'Result array name' "$2"
57-
local items_reference="${1}[@]"
58-
local item
56+
@go.validate_identifier_or_die 'Result array name' "$1"
57+
local __go_pad_items_items=("${@:2}")
58+
local __item
5959
local padding_size=0
6060

61-
for item in "${!items_reference}"; do
62-
while [[ "$padding_size" -lt "${#item}" ]]; do
63-
padding_size="${#item}"
61+
for __item in "${__go_pad_items_items[@]}"; do
62+
while [[ "$padding_size" -lt "${#__item}" ]]; do
63+
padding_size="${#__item}"
6464
done
6565
done
66-
@go.array_printf "$2" "%-${padding_size}s" "${!items_reference}"
66+
@go.array_printf "$1" "%-${padding_size}s" "${__go_pad_items_items[@]}"
6767
}
6868

6969
# Concatenates parallel elements from each input array

libexec/modules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ _@go.modules_produce_listing() {
154154
local padded_modules=()
155155
local zipped_modules=()
156156

157-
@go.pad_items modules padded_modules
157+
@go.pad_items padded_modules "${modules[@]}"
158158
modules=("${padded_modules[@]}")
159159

160160
case "$action" in

tests/format.bats

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ run_array_printf_script() {
2525

2626
run_pad_items_script() {
2727
create_go_format_script \
28-
'declare items=("$@")' \
2928
'declare padded=()' \
30-
'@go.pad_items items padded' \
29+
'@go.pad_items padded "$@"' \
3130
'IFS="|"' \
3231
'printf "%s\n" "${padded[*]}"'
3332
run "$TEST_GO_SCRIPT" "$@"
@@ -80,7 +79,7 @@ run_strip_formatting_codes_script() {
8079

8180
@test "$SUITE: pad_items validates result array name" {
8281
create_test_go_script '. "$_GO_USE_MODULES" format' \
83-
'@go.pad_items items "3foobar"'
82+
'@go.pad_items "3foobar"'
8483
run "$TEST_GO_SCRIPT"
8584

8685
local err_msg='Result array name "3foobar" for @go.pad_items '

0 commit comments

Comments
 (0)