refactor: use dynamic Docker-style tables for stack listings

This commit is contained in:
lingkai5wu committed 2026-10-10 20:33:43 +08:00
1 parent fdcc47cb5b
commit 100caa0e88
1 file changed
+41 -23
+41 -23
View File
@@ -582,11 +582,34 @@ run_all_sequential() {
fi fi
} }
print_table() {
local columns="$1"
shift
local -a cells=("$@") widths=()
local index column row
for ((index = 0; index < ${#cells[@]}; index++)); do
column=$((index % columns))
if ((${#cells[index]} > ${widths[column]:-0})); then
widths[column]="${#cells[index]}"
fi
done
for ((row = 0; row < ${#cells[@]}; row += columns)); do
for ((column = 0; column < columns; column++)); do
((column > 0)) && printf ' '
if ((column == columns - 1)); then
printf '%s\n' "${cells[row + column]}"
else
printf '%-*s' "${widths[column]}" "${cells[row + column]}"
fi
done
done
}
list_stacks() { list_stacks() {
local stack stage_name status local stack stage_name status
local -a cells=(STACK STAGE STATUS)
printf '%-24s %-16s %s\n' "STACK" "STAGE" "STATUS"
printf '%-24s %-16s %s\n' "------------------------" "----------------" "---------------"
for stack in "${DISCOVERED_STACKS[@]}"; do for stack in "${DISCOVERED_STACKS[@]}"; do
if contains "$stack" "${EXCLUDED_STACKS[@]}"; then if contains "$stack" "${EXCLUDED_STACKS[@]}"; then
@@ -597,40 +620,35 @@ list_stacks() {
status="managed" status="managed"
fi fi
printf '%-24s %-16s %s\n' "$stack" "$stage_name" "$status" cells+=("$stack" "$stage_name" "$status")
done done
}
stack_images() { print_table 3 "${cells[@]}"
local stack="$1"
local output image
output="$(compose "$stack" config --images)" || return 1
while IFS= read -r image; do
[[ -n "$image" ]] || continue
printf '%-24s %s\n' "$stack" "$image"
done <<<"$output"
} }
list_images() { list_images() {
local target="${1:-}" local target="${1:-}"
local stack local stack output image
local failed=0 local failed=0
local -a stacks=("${MANAGED_STACKS[@]}") cells=(STACK IMAGE)
printf '%-24s %s\n' "STACK" "IMAGE"
printf '%-24s %s\n' "------------------------" "----------------------------------------"
if [[ -n "$target" ]]; then if [[ -n "$target" ]]; then
require_target "images" "$target" require_target "images" "$target"
stack_images "$target" stacks=("$target")
return
fi fi
for stack in "${MANAGED_STACKS[@]}"; do for stack in "${stacks[@]}"; do
stack_images "$stack" || ((failed += 1)) if ! output="$(compose "$stack" config --images)"; then
((failed += 1))
continue
fi
while IFS= read -r image; do
[[ -n "$image" ]] || continue
cells+=("$stack" "$image")
done <<<"$output"
done done
print_table 2 "${cells[@]}"
((failed == 0)) ((failed == 0))
} }