wgctl/modules/ui/peer.module.sh

158 lines
No EOL
5.1 KiB
Bash

#!/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"
}