gistfile1.txt
· 5.1 KiB · Text
Raw
#!/usr/bin/env bash
# ui/peer.module.sh — rendering for peer list data
# Both compact (tableless) and table layouts kept for future config switching.
_LIST_STYLE="${LIST_STYLE:-compact}"
function ui::peer::list_style() {
echo "$_LIST_STYLE"
}
# ======================================================
# Compact layout (tableless)
# ======================================================
function ui::peer::list_row_compact() {
local w_name="${1:-22}" w_ip="${2:-14}" w_type="${3:-9}" \
w_rule="${4:-6}" w_group="${5:-6}"
shift 5
local name="${1:-}" ip="${2:-}" type="${3:-}" rule="${4:-}" \
group="${5:-}" status="${6:-}" last_seen="${7:-}" \
is_blocked="${8:-false}" is_restricted="${9:-false}"
local status_color="\033[0;37m"
if [[ "$is_blocked" == "true" ]]; then
status_color="\033[1;31m"
elif [[ "$is_restricted" == "true" ]]; then
status_color="\033[1;33m"
elif [[ "$status" == "online" ]]; then
status_color="\033[1;32m"
fi
# Last seen mirrors status color
local ls_color="$status_color"
local rule_val="${rule:--}"
local group_val="${group:--}"
local name_pad ip_pad type_pad status_pad
name_pad=$(printf "%-${w_name}s" "$name")
ip_pad=$(printf "%-${w_ip}s" "$ip")
type_pad=$(printf "%-${w_type}s" "$type")
status_pad=$(printf "%-8s" "$status")
local rule_pad_n group_pad_n
rule_pad_n=$(( w_rule - ${#rule_val} ))
group_pad_n=$(( w_group - ${#group_val} ))
[[ $rule_pad_n -lt 0 ]] && rule_pad_n=0
[[ $group_pad_n -lt 0 ]] && group_pad_n=0
printf " %s %s %s \033[2mrule:\033[0m %s%*s \033[2mgroup:\033[0m %s%*s %b%s\033[0m %b%s\033[0m\n" \
"$name_pad" "$ip_pad" "$type_pad" \
"$rule_val" "$rule_pad_n" "" \
"$group_val" "$group_pad_n" "" \
"$status_color" "$status_pad" \
"$ls_color" "$last_seen"
}
# ======================================================
# Table layout (kept for config switching)
# ======================================================
function ui::peer::list_header_table() {
local has_groups="${1:-false}"
if $has_groups; then
printf "\n %-28s %-15s %-13s %-12s %-12s %-22s %s\n" \
"NAME" "IP" "TYPE" "RULE" "GROUP" "STATUS" "LAST SEEN"
printf " %s\n" "$(printf '─%.0s' {1..135})"
else
printf "\n %-28s %-15s %-13s %-12s %-22s %s\n" \
"NAME" "IP" "TYPE" "RULE" "STATUS" "LAST SEEN"
printf " %s\n" "$(printf '─%.0s' {1..107})"
fi
}
function ui::peer::list_footer_table() {
local has_groups="${1:-false}"
if $has_groups; then
printf " %s\n" "$(printf '─%.0s' {1..135})"
else
printf " %s\n" "$(printf '─%.0s' {1..107})"
fi
}
function ui::peer::list_row_table() {
local has_groups="${1:-false}"
shift
local name="${1:-}" ip="${2:-}" type="${3:-}" rule="${4:-}" \
group="${5:-}" status="${6:-}" last_seen="${7:-}"
local padded_status
padded_status=$(ui::pad_status "$status" 25)
if $has_groups; then
printf " %-28s %-15s %-13s %-12s %-12s %s %s\n" \
"$name" "$ip" "$type" "${rule:-—}" "${group:-—}" \
"$padded_status" "$last_seen"
else
printf " %-28s %-15s %-13s %-12s %s %s\n" \
"$name" "$ip" "$type" "${rule:-—}" \
"$padded_status" "$last_seen"
fi
}
# ======================================================
# Detailed layout (grouped by identity)
# ======================================================
function ui::peer::list_identity_header() {
local identity_name="${1:-}"
printf "\n \033[1m%s\033[0m\n" "$identity_name"
}
function ui::peer::list_row_detailed() {
local w_name="${1:-22}" w_ip="${2:-14}" w_type="${3:-9}" \
w_rule="${4:-6}" w_group="${5:-6}" w_subnet="${6:-8}"
shift 6
local name="${1:-}" ip="${2:-}" type="${3:-}" rule="${4:-}" \
group="${5:-}" subnet="${6:-}" status="${7:-}" last_seen="${8:-}" \
is_blocked="${9:-false}" is_restricted="${10:-false}"
local status_color="\033[0;37m"
if [[ "$is_blocked" == "true" ]]; then
status_color="\033[1;31m"
elif [[ "$is_restricted" == "true" ]]; then
status_color="\033[1;33m"
elif [[ "$status" == "online" ]]; then
status_color="\033[1;32m"
fi
# Last seen mirrors status color
local ls_color="$status_color"
local rule_val="${rule:--}"
local group_val="${group:--}"
local subnet_val="${subnet:--}"
local name_pad ip_pad type_pad status_pad
name_pad=$(printf "%-${w_name}s" "$name")
ip_pad=$(printf "%-${w_ip}s" "$ip")
type_pad=$(printf "%-${w_type}s" "$type")
status_pad=$(printf "%-8s" "$status")
local rule_pad_n group_pad_n subnet_pad_n
rule_pad_n=$(( w_rule - ${#rule_val} ))
group_pad_n=$(( w_group - ${#group_val} ))
subnet_pad_n=$(( w_subnet - ${#subnet_val} ))
[[ $rule_pad_n -lt 0 ]] && rule_pad_n=0
[[ $group_pad_n -lt 0 ]] && group_pad_n=0
[[ $subnet_pad_n -lt 0 ]] && subnet_pad_n=0
printf " · %s %s %s \033[2mrule:\033[0m %s%*s \033[2mgroup:\033[0m %s%*s \033[2msubnet:\033[0m %s%*s %b%s\033[0m %b%s\033[0m\n" \
"$name_pad" "$ip_pad" "$type_pad" \
"$rule_val" "$rule_pad_n" "" \
"$group_val" "$group_pad_n" "" \
"$subnet_val" "$subnet_pad_n" "" \
"$status_color" "$status_pad" \
"$ls_color" "$last_seen"
}
| 1 | #!/usr/bin/env bash |
| 2 | # ui/peer.module.sh — rendering for peer list data |
| 3 | # Both compact (tableless) and table layouts kept for future config switching. |
| 4 | |
| 5 | _LIST_STYLE="${LIST_STYLE:-compact}" |
| 6 | |
| 7 | function ui::peer::list_style() { |
| 8 | echo "$_LIST_STYLE" |
| 9 | } |
| 10 | |
| 11 | # ====================================================== |
| 12 | # Compact layout (tableless) |
| 13 | # ====================================================== |
| 14 | |
| 15 | function ui::peer::list_row_compact() { |
| 16 | local w_name="${1:-22}" w_ip="${2:-14}" w_type="${3:-9}" \ |
| 17 | w_rule="${4:-6}" w_group="${5:-6}" |
| 18 | shift 5 |
| 19 | local name="${1:-}" ip="${2:-}" type="${3:-}" rule="${4:-}" \ |
| 20 | group="${5:-}" status="${6:-}" last_seen="${7:-}" \ |
| 21 | is_blocked="${8:-false}" is_restricted="${9:-false}" |
| 22 | |
| 23 | local status_color="\033[0;37m" |
| 24 | if [[ "$is_blocked" == "true" ]]; then |
| 25 | status_color="\033[1;31m" |
| 26 | elif [[ "$is_restricted" == "true" ]]; then |
| 27 | status_color="\033[1;33m" |
| 28 | elif [[ "$status" == "online" ]]; then |
| 29 | status_color="\033[1;32m" |
| 30 | fi |
| 31 | |
| 32 | # Last seen mirrors status color |
| 33 | local ls_color="$status_color" |
| 34 | |
| 35 | local rule_val="${rule:--}" |
| 36 | local group_val="${group:--}" |
| 37 | |
| 38 | local name_pad ip_pad type_pad status_pad |
| 39 | name_pad=$(printf "%-${w_name}s" "$name") |
| 40 | ip_pad=$(printf "%-${w_ip}s" "$ip") |
| 41 | type_pad=$(printf "%-${w_type}s" "$type") |
| 42 | status_pad=$(printf "%-8s" "$status") |
| 43 | |
| 44 | local rule_pad_n group_pad_n |
| 45 | rule_pad_n=$(( w_rule - ${#rule_val} )) |
| 46 | group_pad_n=$(( w_group - ${#group_val} )) |
| 47 | [[ $rule_pad_n -lt 0 ]] && rule_pad_n=0 |
| 48 | [[ $group_pad_n -lt 0 ]] && group_pad_n=0 |
| 49 | |
| 50 | printf " %s %s %s \033[2mrule:\033[0m %s%*s \033[2mgroup:\033[0m %s%*s %b%s\033[0m %b%s\033[0m\n" \ |
| 51 | "$name_pad" "$ip_pad" "$type_pad" \ |
| 52 | "$rule_val" "$rule_pad_n" "" \ |
| 53 | "$group_val" "$group_pad_n" "" \ |
| 54 | "$status_color" "$status_pad" \ |
| 55 | "$ls_color" "$last_seen" |
| 56 | } |
| 57 | |
| 58 | # ====================================================== |
| 59 | # Table layout (kept for config switching) |
| 60 | # ====================================================== |
| 61 | |
| 62 | function ui::peer::list_header_table() { |
| 63 | local has_groups="${1:-false}" |
| 64 | if $has_groups; then |
| 65 | printf "\n %-28s %-15s %-13s %-12s %-12s %-22s %s\n" \ |
| 66 | "NAME" "IP" "TYPE" "RULE" "GROUP" "STATUS" "LAST SEEN" |
| 67 | printf " %s\n" "$(printf '─%.0s' {1..135})" |
| 68 | else |
| 69 | printf "\n %-28s %-15s %-13s %-12s %-22s %s\n" \ |
| 70 | "NAME" "IP" "TYPE" "RULE" "STATUS" "LAST SEEN" |
| 71 | printf " %s\n" "$(printf '─%.0s' {1..107})" |
| 72 | fi |
| 73 | } |
| 74 | |
| 75 | function ui::peer::list_footer_table() { |
| 76 | local has_groups="${1:-false}" |
| 77 | if $has_groups; then |
| 78 | printf " %s\n" "$(printf '─%.0s' {1..135})" |
| 79 | else |
| 80 | printf " %s\n" "$(printf '─%.0s' {1..107})" |
| 81 | fi |
| 82 | } |
| 83 | |
| 84 | function ui::peer::list_row_table() { |
| 85 | local has_groups="${1:-false}" |
| 86 | shift |
| 87 | local name="${1:-}" ip="${2:-}" type="${3:-}" rule="${4:-}" \ |
| 88 | group="${5:-}" status="${6:-}" last_seen="${7:-}" |
| 89 | |
| 90 | local padded_status |
| 91 | padded_status=$(ui::pad_status "$status" 25) |
| 92 | |
| 93 | if $has_groups; then |
| 94 | printf " %-28s %-15s %-13s %-12s %-12s %s %s\n" \ |
| 95 | "$name" "$ip" "$type" "${rule:-—}" "${group:-—}" \ |
| 96 | "$padded_status" "$last_seen" |
| 97 | else |
| 98 | printf " %-28s %-15s %-13s %-12s %s %s\n" \ |
| 99 | "$name" "$ip" "$type" "${rule:-—}" \ |
| 100 | "$padded_status" "$last_seen" |
| 101 | fi |
| 102 | } |
| 103 | |
| 104 | # ====================================================== |
| 105 | # Detailed layout (grouped by identity) |
| 106 | # ====================================================== |
| 107 | |
| 108 | function ui::peer::list_identity_header() { |
| 109 | local identity_name="${1:-}" |
| 110 | printf "\n \033[1m%s\033[0m\n" "$identity_name" |
| 111 | } |
| 112 | |
| 113 | function ui::peer::list_row_detailed() { |
| 114 | local w_name="${1:-22}" w_ip="${2:-14}" w_type="${3:-9}" \ |
| 115 | w_rule="${4:-6}" w_group="${5:-6}" w_subnet="${6:-8}" |
| 116 | shift 6 |
| 117 | local name="${1:-}" ip="${2:-}" type="${3:-}" rule="${4:-}" \ |
| 118 | group="${5:-}" subnet="${6:-}" status="${7:-}" last_seen="${8:-}" \ |
| 119 | is_blocked="${9:-false}" is_restricted="${10:-false}" |
| 120 | |
| 121 | local status_color="\033[0;37m" |
| 122 | if [[ "$is_blocked" == "true" ]]; then |
| 123 | status_color="\033[1;31m" |
| 124 | elif [[ "$is_restricted" == "true" ]]; then |
| 125 | status_color="\033[1;33m" |
| 126 | elif [[ "$status" == "online" ]]; then |
| 127 | status_color="\033[1;32m" |
| 128 | fi |
| 129 | |
| 130 | # Last seen mirrors status color |
| 131 | local ls_color="$status_color" |
| 132 | |
| 133 | local rule_val="${rule:--}" |
| 134 | local group_val="${group:--}" |
| 135 | local subnet_val="${subnet:--}" |
| 136 | |
| 137 | local name_pad ip_pad type_pad status_pad |
| 138 | name_pad=$(printf "%-${w_name}s" "$name") |
| 139 | ip_pad=$(printf "%-${w_ip}s" "$ip") |
| 140 | type_pad=$(printf "%-${w_type}s" "$type") |
| 141 | status_pad=$(printf "%-8s" "$status") |
| 142 | |
| 143 | local rule_pad_n group_pad_n subnet_pad_n |
| 144 | rule_pad_n=$(( w_rule - ${#rule_val} )) |
| 145 | group_pad_n=$(( w_group - ${#group_val} )) |
| 146 | subnet_pad_n=$(( w_subnet - ${#subnet_val} )) |
| 147 | [[ $rule_pad_n -lt 0 ]] && rule_pad_n=0 |
| 148 | [[ $group_pad_n -lt 0 ]] && group_pad_n=0 |
| 149 | [[ $subnet_pad_n -lt 0 ]] && subnet_pad_n=0 |
| 150 | |
| 151 | printf " · %s %s %s \033[2mrule:\033[0m %s%*s \033[2mgroup:\033[0m %s%*s \033[2msubnet:\033[0m %s%*s %b%s\033[0m %b%s\033[0m\n" \ |
| 152 | "$name_pad" "$ip_pad" "$type_pad" \ |
| 153 | "$rule_val" "$rule_pad_n" "" \ |
| 154 | "$group_val" "$group_pad_n" "" \ |
| 155 | "$subnet_val" "$subnet_pad_n" "" \ |
| 156 | "$status_color" "$status_pad" \ |
| 157 | "$ls_color" "$last_seen" |
| 158 | } |