nuno revised this gist 1 month ago. Go to revision
1 file changed, 67 insertions
gistfile1.txt(file created)
| @@ -0,0 +1,67 @@ | |||
| 1 | + | function ui::logs::fw_row() { | |
| 2 | + | local ts="${1:-}" client="${2:-}" dest_ip="${3:-}" dest_port="${4:-}" \ | |
| 3 | + | proto="${5:-}" svc_name="${6:-}" count="${7:-1}" \ | |
| 4 | + | w_client="${8:-20}" w_dest="${9:-30}" \ | |
| 5 | + | src_endpoint="${10:-}" w_endpoint="${11:-0}" | |
| 6 | + | ||
| 7 | + | local ts_pad client_pad | |
| 8 | + | ts_pad=$(printf "%-11s" "$ts") | |
| 9 | + | client_pad=$(printf "%-${w_client}s" "$client") | |
| 10 | + | ||
| 11 | + | # ── Source endpoint column ── | |
| 12 | + | local src_col="" | |
| 13 | + | if [[ "$w_endpoint" -gt 0 ]]; then | |
| 14 | + | local src_pad_n | |
| 15 | + | if [[ -n "$src_endpoint" ]]; then | |
| 16 | + | local src_resolved | |
| 17 | + | src_resolved=$(resolve::ip "$src_endpoint" 2>/dev/null || echo "") | |
| 18 | + | [[ "$src_resolved" == "$src_endpoint" ]] && src_resolved="" | |
| 19 | + | if [[ -n "$src_resolved" ]]; then | |
| 20 | + | src_col="${src_endpoint} \033[2m→ ${src_resolved}\033[0m" | |
| 21 | + | else | |
| 22 | + | src_col="${src_endpoint}" | |
| 23 | + | fi | |
| 24 | + | src_pad_n=$(( w_endpoint - ${#src_endpoint} )) | |
| 25 | + | else | |
| 26 | + | src_pad_n="$w_endpoint" | |
| 27 | + | fi | |
| 28 | + | [[ $src_pad_n -lt 0 ]] && src_pad_n=0 | |
| 29 | + | src_col="${src_col}$(printf '%*s' "$src_pad_n" '')" | |
| 30 | + | fi | |
| 31 | + | ||
| 32 | + | # ── Destination column ── | |
| 33 | + | # svc_display: "npm/tcp" or "pihole (icmp)" — used for padding | |
| 34 | + | # raw_suffix: dim "(10.0.0.101:3260)" — appended after padding | |
| 35 | + | local svc_display raw_suffix="" | |
| 36 | + | if [[ -n "$svc_name" ]]; then | |
| 37 | + | [[ -n "$dest_port" ]] && svc_display="${svc_name}/${proto}" || svc_display="${svc_name} (${proto})" | |
| 38 | + | [[ -n "$dest_port" ]] && raw_suffix=" \033[2m(${dest_ip}:${dest_port})\033[0m" \ | |
| 39 | + | || raw_suffix=" \033[2m(${dest_ip})\033[0m" | |
| 40 | + | else | |
| 41 | + | [[ -n "$dest_port" ]] && svc_display="${dest_ip}:${dest_port}/${proto}" \ | |
| 42 | + | || svc_display="${dest_ip} (${proto})" | |
| 43 | + | fi | |
| 44 | + | ||
| 45 | + | local dest_pad_n=$(( w_dest - ${#svc_display} )) | |
| 46 | + | [[ $dest_pad_n -lt 0 ]] && dest_pad_n=0 | |
| 47 | + | ||
| 48 | + | # ── Count ── | |
| 49 | + | local count_suffix="" | |
| 50 | + | [[ "$count" -gt 1 ]] && count_suffix=" \033[2m(x${count})\033[0m" | |
| 51 | + | ||
| 52 | + | # ── Render ── | |
| 53 | + | local src_padded | |
| 54 | + | if [[ -n "$src_endpoint" ]]; then | |
| 55 | + | local src_colored="$src_endpoint" | |
| 56 | + | [[ -n "$src_resolved" ]] && src_colored="${src_endpoint} \033[2m→ ${src_resolved}\033[0m" | |
| 57 | + | src_padded=$(ui::pad_mb "$src_colored" "$w_endpoint") | |
| 58 | + | else | |
| 59 | + | src_padded=$(printf "%${w_endpoint}s" "") | |
| 60 | + | fi | |
| 61 | + | ||
| 62 | + | printf " %s %s %b \033[1;31m→\033[0m %s%*s%b%b\n" \ | |
| 63 | + | "$ts_pad" "$client_pad" \ | |
| 64 | + | "$src_padded" \ | |
| 65 | + | "$svc_display" "$dest_pad_n" "" \ | |
| 66 | + | "$raw_suffix" "$count_suffix" | |
| 67 | + | } | |
Newer
Older