wgctl/core/ui.sh

55 lines
No EOL
1.4 KiB
Bash

#!/usr/bin/env bash
UI_ROW_WIDTH=${UI_ROW_WIDTH:-20}
UI_SECTION_WIDTH=${UI_SECTION_WIDTH:-44}
function ui::row() {
local label="$1" value="$2" width="${3:-$UI_ROW_WIDTH}"
printf " %-${width}s %s\n" "${label}:" "$value"
}
function ui::section() {
local title="$1" width="${2:-$UI_SECTION_WIDTH}"
local dashes
dashes=$(printf '─%.0s' $(seq 1 $(( width - ${#title} - 4 ))))
printf "\n \033[0;37m── %s %s\033[0m\n" "$title" "$dashes"
}
function ui::list_item() {
local prefix="$1" value="$2"
printf " %s %s\n" "$prefix" "$value"
}
function ui::print_list() {
local prefix="$1" input="$2"
[[ -z "$input" ]] && return 0 # early return for empty input
while IFS= read -r e; do
[[ -n "$e" ]] && ui::list_item "$prefix" "$e"
done <<< "$input"
}
function ui::divider() {
local width="${1:-48}"
printf " %s\n" "$(printf '─%.0s' $(seq 1 $width))"
}
function ui::pad() {
local text="$1" width="${2:-20}"
local visible
visible=$(echo -e "$text" | sed 's/\x1b\[[0-9;]*m//g')
local pad=$(( width - ${#visible} ))
printf "%b%${pad}s" "$text" ""
}
function ui::firewall_rule() {
local rule="$1"
if [[ "$rule" =~ ACCEPT|DNAT ]]; then
printf "\033[0;32m%s\033[0m\n" "$rule"
elif [[ "$rule" =~ DROP ]]; then
printf "\033[0;31m%s\033[0m\n" "$rule"
elif [[ "$rule" =~ NFLOG|LOG ]]; then
printf "\033[0;37m%s\033[0m\n" "$rule"
else
printf "%s\n" "$rule"
fi
}