nuno revised this gist 1 month ago. Go to revision
1 file changed, 87 insertions
gistfile1.txt(file created)
| @@ -0,0 +1,87 @@ | |||
| 1 | + | #!/usr/bin/env bash | |
| 2 | + | # ui/activity.module.sh — rendering for wgctl activity | |
| 3 | + | ||
| 4 | + | # ui::activity::peer_row <name_pad> <rx_pad> <tx_pad> <drops> <drop_word> <w_drops> | |
| 5 | + | function ui::activity::peer_row() { | |
| 6 | + | local name_pad="${1:-}" rx_pad="${2:-}" tx_pad="${3:-}" \ | |
| 7 | + | drops="${4:-0}" drop_word="${5:-drops}" w_drops="${6:-1}" | |
| 8 | + | ||
| 9 | + | printf " \033[1m%s\033[0m \033[2m↓\033[0m%s \033[2m↑\033[0m%s %${w_drops}s %s\n" \ | |
| 10 | + | "$name_pad" "$rx_pad" "$tx_pad" "$drops" "$drop_word" | |
| 11 | + | } | |
| 12 | + | ||
| 13 | + | # ui::activity::service_row <dest_display> <drop_count> <drop_word> <drops_col> <w_drops> | |
| 14 | + | function ui::activity::service_row() { | |
| 15 | + | local dest_display="${1:-}" drop_count="${2:-0}" drop_word="${3:-drops}" \ | |
| 16 | + | drops_col="${4:-30}" w_count="${5:-1}" | |
| 17 | + | ||
| 18 | + | local arrow_prefix=" → " | |
| 19 | + | local prefix_bytes=${#arrow_prefix} | |
| 20 | + | local prefix_len=$(( prefix_bytes + ${#dest_display} )) | |
| 21 | + | local pad_n=$(( drops_col - prefix_len )) | |
| 22 | + | [[ $pad_n -lt 1 ]] && pad_n=1 | |
| 23 | + | ||
| 24 | + | printf " \033[0;31m→ %s%*s %${w_count}s %s\033[0m\n" \ | |
| 25 | + | "$dest_display" "$pad_n" "" "$drop_count" "$drop_word" | |
| 26 | + | } | |
| 27 | + | ||
| 28 | + | # Table versions (kept for future display config) | |
| 29 | + | function ui::activity::header_table() { | |
| 30 | + | printf "\n %-24s %-14s %-14s %s\n" "PEER" "↓ RX" "↑ TX" "DROPS" | |
| 31 | + | printf " %s\n" "$(printf '─%.0s' {1..65})" | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | function ui::activity::peer_row_table() { | |
| 35 | + | local name="${1:-}" rx_fmt="${2:-}" tx_fmt="${3:-}" \ | |
| 36 | + | drops="${4:-0}" drop_word="${5:-drops}" | |
| 37 | + | printf " %-24s %-14s %-14s %s %s\n" \ | |
| 38 | + | "$name" "↓$rx_fmt" "↑$tx_fmt" "$drops" "$drop_word" | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | function ui::activity::service_row_table() { | |
| 42 | + | local dest_display="${1:-}" drop_count="${2:-0}" drop_word="${3:-drops}" | |
| 43 | + | printf " → %-30s %s %s\n" "$dest_display" "$drop_count" "$drop_word" | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | function ui::activity::accept_row() { | |
| 47 | + | local name_pad="${1:-}" bytes_in="${2:-}" bytes_out="${3:-}" \ | |
| 48 | + | conns="${4:-0}" w_count="${5:-4}" | |
| 49 | + | ||
| 50 | + | local conn_word="conns" | |
| 51 | + | [[ "$conns" -eq 1 ]] && conn_word="conn" | |
| 52 | + | ||
| 53 | + | local spaces | |
| 54 | + | spaces=$(printf '%*s' "${#name_pad}" '') | |
| 55 | + | ||
| 56 | + | printf " \033[0;32m%s ↓%-10s ↑%-10s %${w_count}s %s\033[0m\n" \ | |
| 57 | + | "$spaces" "$bytes_in" "$bytes_out" "$conns" "$conn_word" | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | ||
| 61 | + | function ui::activity::accept_dest_row() { | |
| 62 | + | local dest="${1:-}" bytes_orig="${2:-0}" bytes_reply="${3:-0}" \ | |
| 63 | + | count="${4:-0}" drops_col="${5:-40}" w_count="${6:-4}" | |
| 64 | + | ||
| 65 | + | local conn_word="conns" | |
| 66 | + | [[ "$count" -eq 1 ]] && conn_word="conn" | |
| 67 | + | ||
| 68 | + | local arrow_prefix=" → " | |
| 69 | + | local prefix_bytes=${#arrow_prefix} | |
| 70 | + | local prefix_len=$(( prefix_bytes + ${#dest} )) | |
| 71 | + | local pad_n=$(( drops_col - prefix_len )) | |
| 72 | + | [[ $pad_n -lt 1 ]] && pad_n=1 | |
| 73 | + | ||
| 74 | + | # Only show bytes if non-zero | |
| 75 | + | local bytes_display="" | |
| 76 | + | if [[ "$bytes_orig" -gt 0 || "$bytes_reply" -gt 0 ]]; then | |
| 77 | + | local bytes_display=" " | |
| 78 | + | [[ "$bytes_orig" -gt 0 ]] && bytes_display+="↓$(fmt::bytes "$bytes_orig") " | |
| 79 | + | [[ "$bytes_reply" -gt 0 ]] && bytes_display+="↑$(fmt::bytes "$bytes_reply")" | |
| 80 | + | bytes_display="${bytes_display% }" # trim trailing space | |
| 81 | + | fi | |
| 82 | + | ||
| 83 | + | printf " \033[0;32m→\033[0m \033[0;32m%b" "$dest" | |
| 84 | + | printf "%*s %${w_count}s %-5s%s\033[0m\n" "$pad_n" "" "$count" "$conn_word" "$bytes_display" | |
| 85 | + | # printf " \033[0;32m→\033[0m \033[0;32m%s%*s %${w_count}s %-5s%s\033[0m\n" \ | |
| 86 | + | # "$dest" "$pad_n" "" "$count" "$conn_word" "$bytes_display" | |
| 87 | + | } | |
Newer
Older