1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-12 00:04:00 +00:00

feat: dynamically adjust column widths in UI and analysis output based on terminal size for improved readability

This commit is contained in:
Tw93
2025-12-20 22:18:06 +08:00
parent 499a1ff646
commit 2a0fe88fd3
6 changed files with 217 additions and 25 deletions

View File

@@ -505,17 +505,75 @@ paginated_multi_select() {
)
_print_wrapped_controls "$sep" "${_segs_all[@]}"
else
# Normal: show full controls
local -a _segs_all=(
# Normal: show full controls (without O ↑/↓ to save space)
# Dynamically reduce controls if they won't fit
local term_width="${COLUMNS:-}"
[[ -z "$term_width" ]] && term_width=$(tput cols 2>/dev/null || echo 80)
# Helper to calculate display length without ANSI codes
_calc_len() {
local text="$1"
local stripped
stripped=$(printf "%s" "$text" | LC_ALL=C awk '{gsub(/\033\[[0-9;]*[A-Za-z]/,""); print}')
printf "%d" "${#stripped}"
}
# Build full controls and calculate total width
local -a _segs_full=(
"${GRAY}${ICON_NAV_UP}${ICON_NAV_DOWN}${NC}"
"${GRAY}Space Select${NC}"
"${GRAY}Enter${NC}"
"${GRAY}R Refresh${NC}"
"${GRAY}${filter_text}${NC}"
"${GRAY}S ${sort_status}${NC}"
"${GRAY}O ${reverse_arrow}${NC}"
"${GRAY}Q Exit${NC}"
)
# Calculate total width with separators
local total_len=0 seg_count=${#_segs_full[@]}
local i
for i in "${!_segs_full[@]}"; do
total_len=$((total_len + $(_calc_len "${_segs_full[i]}")))
# Add separator width (3 chars: " | ")
[[ $i -lt $((seg_count - 1)) ]] && total_len=$((total_len + 3))
done
# Multi-level fallback: progressively remove items if too wide
if [[ $total_len -gt $term_width ]]; then
# Level 1: Remove "R Refresh" (least critical)
local -a _segs_reduced=(
"${GRAY}${ICON_NAV_UP}${ICON_NAV_DOWN}${NC}"
"${GRAY}Space Select${NC}"
"${GRAY}Enter${NC}"
"${GRAY}${filter_text}${NC}"
"${GRAY}S ${sort_status}${NC}"
"${GRAY}Q Exit${NC}"
)
# Recalculate length
total_len=0
seg_count=${#_segs_reduced[@]}
for i in "${!_segs_reduced[@]}"; do
total_len=$((total_len + $(_calc_len "${_segs_reduced[i]}")))
[[ $i -lt $((seg_count - 1)) ]] && total_len=$((total_len + 3))
done
# Level 2: If still too wide, also remove "S ${sort_status}"
if [[ $total_len -gt $term_width ]]; then
local -a _segs_all=(
"${GRAY}${ICON_NAV_UP}${ICON_NAV_DOWN}${NC}"
"${GRAY}Space Select${NC}"
"${GRAY}Enter${NC}"
"${GRAY}${filter_text}${NC}"
"${GRAY}Q Exit${NC}"
)
else
local -a _segs_all=("${_segs_reduced[@]}")
fi
else
local -a _segs_all=("${_segs_full[@]}")
fi
_print_wrapped_controls "$sep" "${_segs_all[@]}"
fi
else