Align rule table display widths

This commit is contained in:
2026-04-17 12:17:00 +08:00
parent 87ee5283c8
commit 818c52c10d
6 changed files with 29 additions and 16 deletions

View File

@@ -57,18 +57,16 @@ log_ok() { _log_with_color "${CLR_GREEN}" OK "$@"; }
display_width() {
local input=${1-}
awk -v str="${input}" 'BEGIN {
n = split(str, chars, "")
width = 0
for (i = 1; i <= n; i++) {
if (chars[i] ~ /[ -~]/) {
width += 1
} else {
width += 2
}
}
print width
}'
local char code width=0
while IFS= read -r -n1 char; do
code=$(printf '%d' "'${char}")
if (( code >= 32 && code <= 126 )); then
((width += 1))
else
((width += 2))
fi
done < <(printf '%s' "${input}")
printf '%s\n' "${width}"
}
repeat_char() {

View File

@@ -146,6 +146,12 @@ rule_health_mark() {
fi
}
render_rules_row() {
printf '%s %s %s %s %s %s %s %s\n' \
"$(pad_right "${1-}" 3)" "$(pad_right "${2-}" 10)" "$(pad_right "${3-}" 10)" "$(pad_right "${4-}" 32)" \
"$(pad_right "${5-}" 10)" "$(pad_right "${6-}" 12)" "$(pad_right "${7-}" 4)" "${8-}"
}
render_rules_plain() {
local -a lines=("$@")
local line idx uuid proto lport tip tport ipver desc health
@@ -154,7 +160,7 @@ render_rules_plain() {
return 0
fi
printf '%-3s %-10s %-10s %-32s %-10s %-12s %-4s %s\n' '#' '协议' '本地端口' '目标地址' '目标端口' 'IP版本' '状态' '描述'
render_rules_row '#' '协议' '本地端口' '目标地址' '目标端口' 'IP版本' '状态' '描述'
for ((idx = 0; idx < ${#lines[@]}; idx++)); do
line=${lines[idx]}
uuid=$(rule_field "${line}" uuid)
@@ -165,8 +171,7 @@ render_rules_plain() {
ipver=$(rule_ipver_label "$(rule_field "${line}" ipver)")
desc=$(rule_field "${line}" desc || true)
health=$(rule_health_mark "${line}")
printf '%-3s %-10s %-10s %-32s %-10s %-12s %-4s %s\n' \
"$((idx + 1))" "${proto}" "${lport}" "${tip}" "${tport}" "${ipver}" "${health}" "${desc}"
render_rules_row "$((idx + 1))" "${proto}" "${lport}" "${tip}" "${tport}" "${ipver}" "${health}" "${desc}"
done
}