wgctl/modules/display.module.sh
Nuno Duque Nunes 7a544f9019 feat: display config system, table/compact toggle
- modules/display.module.sh: display config loader
- .wgctl/config/display.json: per-view style configuration
- ctx::display: points to .wgctl/config/display.json
- json_helper: display_load() reads view styles
- list: display::render dispatcher, _render_table with dynamic widths/colors
- ui::peer::_row_color/status_color: shared by both table and compact
- table layout: aligned columns, colored status/rows, dynamic separator
2026-05-27 02:48:51 +00:00

72 lines
No EOL
2 KiB
Bash

#!/usr/bin/env bash
# modules/display.module.sh
# Display configuration — controls layout style per view
# ============================================
# State — loaded once on first access
# ============================================
_DISPLAY_LOADED=false
declare -gA _DISPLAY_STYLES=()
# ============================================
# Load display config
# ============================================
function display::_load() {
$_DISPLAY_LOADED && return 0
_DISPLAY_LOADED=true
local display_file
display_file="$(ctx::display)"
[[ ! -f "$display_file" ]] && return 0
# Load styles per view via json_helper
local view style
while IFS='=' read -r view style; do
[[ -n "$view" && -n "$style" ]] && _DISPLAY_STYLES["$view"]="$style"
done < <(python3 "$(ctx::json_helper)" display_load "$display_file" 2>/dev/null)
}
# ============================================
# Accessors
# ============================================
# display::style <view>
# Returns: compact | table | minimal (default: compact)
function display::style() {
local view="${1:-}"
display::_load
echo "${_DISPLAY_STYLES[$view]:-compact}"
}
# display::is_compact <view>
function display::is_compact() {
[[ "$(display::style "$1")" == "compact" ]]
}
# display::is_table <view>
function display::is_table() {
[[ "$(display::style "$1")" == "table" ]]
}
# ============================================
# display::render <view> <data> <compact_fn> <table_fn> [extra_args...]
# Generic dispatcher — calls compact or table render function
# ============================================
function display::render() {
local view="${1:-}" data="${2:-}" compact_fn="${3:-}" table_fn="${4:-}"
shift 4 || true
case "$(display::style "$view")" in
table)
declare -f "$table_fn" >/dev/null 2>&1 && \
"$table_fn" "$data" "$@" || \
"$compact_fn" "$data" "$@" # fallback to compact if no table fn
;;
*)
"$compact_fn" "$data" "$@"
;;
esac
}