term.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. #!/usr/bin/env bash
  2. # term.sh — terminal panel design system for claude-mods skills.
  3. #
  4. # Source from any skill script:
  5. # LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../_lib" && pwd)"
  6. # . "$LIB/term.sh"
  7. # term_init
  8. #
  9. # Honors: NO_COLOR, FORCE_COLOR, TERM_ASCII=1, FLEET_ASCII=1 (legacy).
  10. # Status: experimental — see docs/TERMINAL-DESIGN.md.
  11. # Guard against double-sourcing.
  12. [[ -n "${__TERM_SH_LOADED:-}" ]] && return 0
  13. __TERM_SH_LOADED=1
  14. # ─── Globals (populated by term_init) ──────────────────────────────────────
  15. TERM_TTY=0
  16. TERM_COLOR=0
  17. TERM_ASCII_MODE=0
  18. TERM_WIDTH=80
  19. # ─── ANSI escapes (empty when color disabled) ─────────────────────────────
  20. TERM_C_GREEN=""
  21. TERM_C_YELLOW=""
  22. TERM_C_ORANGE=""
  23. TERM_C_RED=""
  24. TERM_C_CYAN=""
  25. TERM_C_MAGENTA=""
  26. TERM_C_DIM=""
  27. TERM_C_OFF=""
  28. # ─── Tree connectors (set by term_init based on TERM_ASCII_MODE) ──────────
  29. TERM_TREE_BRANCH="" # ├─ / +-
  30. TERM_TREE_LAST="" # └─ / `-
  31. TERM_TREE_VERT="" # │ / |
  32. # ─── Panel chrome ─────────────────────────────────────────────────────────
  33. TERM_PANEL_TL="" # ╭ / +
  34. TERM_PANEL_BL="" # ╰ / +
  35. TERM_PANEL_HRULE="" # ─ / -
  36. TERM_PANEL_TERM="" # ● / *
  37. # ─── Legacy state icons (kept for backwards-compat with fleet.sh) ─────────
  38. TERM_ICON_PENDING=""
  39. TERM_ICON_READY=""
  40. TERM_ICON_DONE=""
  41. TERM_ICON_FAILED=""
  42. TERM_ICON_WARN=""
  43. TERM_ICON_HINT=""
  44. # ─── Registries (Unicode|ASCII) ───────────────────────────────────────────
  45. # Implemented as case statements in __term_lookup below (bash 3.2 compatible —
  46. # stock macOS bash lacks associative arrays).
  47. # Header indicator glyph (branch/⎇)
  48. TERM_GLYPH_BRANCH=""
  49. # Inline alert glyph (▲)
  50. TERM_GLYPH_ALERT=""
  51. # Empty-state tip glyph (💡)
  52. TERM_GLYPH_TIP=""
  53. # Spinner frame banks (set by term_init; arrays keep order).
  54. TERM_SPIN_WORKING=()
  55. TERM_SPIN_HEARTBEAT=()
  56. # ─── term_init ────────────────────────────────────────────────────────────
  57. term_init() {
  58. # TTY detection — stdout only.
  59. if [[ -t 1 ]]; then TERM_TTY=1; else TERM_TTY=0; fi
  60. # ASCII fallback: explicit env, or non-UTF locale.
  61. if [[ "${TERM_ASCII:-}" == "1" ]] || [[ "${FLEET_ASCII:-}" == "1" ]]; then
  62. TERM_ASCII_MODE=1
  63. elif [[ "${LC_ALL:-${LANG:-}}" != *[Uu][Tt][Ff]* ]] && [[ -z "${LC_ALL:-${LANG:-}}" || "${TERM:-}" == "dumb" ]]; then
  64. TERM_ASCII_MODE=1
  65. else
  66. TERM_ASCII_MODE=0
  67. fi
  68. # Color: TTY + not NO_COLOR, or FORCE_COLOR overrides.
  69. if [[ -n "${FORCE_COLOR:-}" ]]; then
  70. TERM_COLOR=1
  71. elif [[ -n "${NO_COLOR:-}" ]] || [[ "$TERM_TTY" -eq 0 ]] || [[ "${TERM:-}" == "dumb" ]]; then
  72. TERM_COLOR=0
  73. else
  74. TERM_COLOR=1
  75. fi
  76. # Terminal width — fall back to 80.
  77. if [[ "$TERM_TTY" -eq 1 ]] && command -v tput >/dev/null 2>&1; then
  78. TERM_WIDTH=$(tput cols 2>/dev/null || echo 80)
  79. fi
  80. [[ "$TERM_WIDTH" -lt 40 ]] && TERM_WIDTH=80
  81. if [[ "$TERM_ASCII_MODE" -eq 1 ]]; then
  82. TERM_ICON_PENDING="[.]"
  83. TERM_ICON_READY="[+]"
  84. TERM_ICON_DONE="[*]"
  85. TERM_ICON_FAILED="[x]"
  86. TERM_ICON_WARN="[!]"
  87. TERM_ICON_HINT="[i]"
  88. TERM_TREE_BRANCH="+-"
  89. TERM_TREE_LAST="\`-"
  90. TERM_TREE_VERT="|"
  91. TERM_PANEL_TL="+"
  92. TERM_PANEL_BL="+"
  93. TERM_PANEL_HRULE="-"
  94. TERM_PANEL_TERM="*"
  95. TERM_GLYPH_BRANCH="(b)"
  96. TERM_GLYPH_ALERT="!"
  97. TERM_GLYPH_TIP="(i)"
  98. TERM_SPIN_WORKING=('|' '/' '-' '\')
  99. TERM_SPIN_HEARTBEAT=('.' ':' '*' ':')
  100. else
  101. TERM_ICON_PENDING="⏳"
  102. TERM_ICON_READY="✅"
  103. TERM_ICON_DONE="🚀"
  104. TERM_ICON_FAILED="❌"
  105. TERM_ICON_WARN="⚠️ "
  106. TERM_ICON_HINT="💡"
  107. TERM_TREE_BRANCH="├─"
  108. TERM_TREE_LAST="└─"
  109. TERM_TREE_VERT="│"
  110. TERM_PANEL_TL="╭"
  111. TERM_PANEL_BL="╰"
  112. TERM_PANEL_HRULE="─"
  113. TERM_PANEL_TERM="●"
  114. TERM_GLYPH_BRANCH="⎇"
  115. TERM_GLYPH_ALERT="▲"
  116. TERM_GLYPH_TIP="💡"
  117. TERM_SPIN_WORKING=('⠋' '⠙' '⠹' '⠸' '⠼' '⠴' '⠦' '⠧' '⠇' '⠏')
  118. TERM_SPIN_HEARTBEAT=('·' '∙' '•' '●' '•' '∙')
  119. fi
  120. if [[ "$TERM_COLOR" -eq 1 ]]; then
  121. TERM_C_GREEN=$'\033[32m'
  122. TERM_C_YELLOW=$'\033[33m'
  123. TERM_C_ORANGE=$'\033[38;5;208m'
  124. TERM_C_RED=$'\033[31m'
  125. TERM_C_CYAN=$'\033[36m'
  126. TERM_C_MAGENTA=$'\033[35m'
  127. TERM_C_DIM=$'\033[2m'
  128. TERM_C_OFF=$'\033[0m'
  129. else
  130. TERM_C_GREEN=""; TERM_C_YELLOW=""; TERM_C_ORANGE=""
  131. TERM_C_RED=""; TERM_C_CYAN=""; TERM_C_MAGENTA=""
  132. TERM_C_DIM=""; TERM_C_OFF=""
  133. fi
  134. }
  135. # ─── Color helper ─────────────────────────────────────────────────────────
  136. # term_color <name> <text...>
  137. term_color() {
  138. local name=$1; shift
  139. local code=""
  140. case "$name" in
  141. green) code="$TERM_C_GREEN" ;;
  142. yellow) code="$TERM_C_YELLOW" ;;
  143. orange) code="$TERM_C_ORANGE" ;;
  144. red) code="$TERM_C_RED" ;;
  145. cyan) code="$TERM_C_CYAN" ;;
  146. magenta) code="$TERM_C_MAGENTA" ;;
  147. dim) code="$TERM_C_DIM" ;;
  148. esac
  149. printf '%s%s%s' "$code" "$*" "$TERM_C_OFF"
  150. }
  151. # ─── Registry lookup ──────────────────────────────────────────────────────
  152. # term_emoji <registry_name> <key> — returns Unicode glyph or ASCII fallback.
  153. # Internal helper; pass "BRAND", "HEALTH_GLYPH", "DIAGRAM_ICON".
  154. __term_lookup() {
  155. local map=$1 key=$2 entry="" uni ascii
  156. case "${map}::${key}" in
  157. BRAND::fleet) entry="⚡|[F]" ;;
  158. BRAND::forge) entry="🔨|[B]" ;;
  159. BRAND::psql) entry="🐘|[P]" ;;
  160. BRAND::watch) entry="📡|[M]" ;;
  161. BRAND::deploy) entry="🚀|[D]" ;;
  162. BRAND::git) entry="🌿|[G]" ;;
  163. BRAND::windows-ops) entry="🩺|[H]" ;;
  164. BRAND::mac-ops) entry="🩺|[M]" ;;
  165. HEALTH_GLYPH::healthy) entry="•|(+)" ;;
  166. HEALTH_GLYPH::pending) entry="•|(.)" ;;
  167. HEALTH_GLYPH::warning) entry="•|(!)" ;;
  168. HEALTH_GLYPH::critical) entry="•|(!!)" ;;
  169. HEALTH_GLYPH::alarm) entry="•|(!!)" ;;
  170. HEALTH_GLYPH::busted) entry="⬤|(X)" ;;
  171. HEALTH_GLYPH::unknown) entry="•|(?)" ;;
  172. DIAGRAM_ICON::user) entry="👤|(U)" ;;
  173. DIAGRAM_ICON::web) entry="🌐|(W)" ;;
  174. DIAGRAM_ICON::mobile) entry="📱|(M)" ;;
  175. DIAGRAM_ICON::auth) entry="🔐|(A)" ;;
  176. DIAGRAM_ICON::database) entry="🗄|(D)" ;;
  177. DIAGRAM_ICON::cache) entry="⚡|(C)" ;;
  178. DIAGRAM_ICON::queue) entry="📨|(Q)" ;;
  179. DIAGRAM_ICON::storage) entry="📦|(P)" ;;
  180. DIAGRAM_ICON::service) entry="⚙|*" ;;
  181. DIAGRAM_ICON::api) entry="🔌|(I)" ;;
  182. DIAGRAM_ICON::search) entry="🔍|(S)" ;;
  183. DIAGRAM_ICON::timer) entry="⏱|(T)" ;;
  184. DIAGRAM_ICON::build) entry="🔨|(B)" ;;
  185. DIAGRAM_ICON::hook) entry="🪝|(H)" ;;
  186. DIAGRAM_ICON::log) entry="📄|(F)" ;;
  187. esac
  188. [[ -z "$entry" ]] && { printf '%s' "?"; return; }
  189. uni="${entry%|*}"
  190. ascii="${entry#*|}"
  191. if [[ "$TERM_ASCII_MODE" -eq 1 ]]; then printf '%s' "$ascii"
  192. else printf '%s' "$uni"; fi
  193. }
  194. term_brand_glyph() { __term_lookup BRAND "$1"; }
  195. term_health_glyph() { __term_lookup HEALTH_GLYPH "$1"; }
  196. term_diagram_icon() { __term_lookup DIAGRAM_ICON "$1"; }
  197. # ─── Legacy state-icon helper (used by fleet.sh) ──────────────────────────
  198. term_state_icon() {
  199. case "$1" in
  200. RUNNING|PENDING) printf '%s' "$TERM_ICON_PENDING" ;;
  201. READY) printf '%s' "$TERM_ICON_READY" ;;
  202. LANDED|DONE|OK) printf '%s' "$TERM_ICON_DONE" ;;
  203. FAILED|ERROR) printf '%s' "$TERM_ICON_FAILED" ;;
  204. CONFLICT|WARN) printf '%s' "$TERM_ICON_WARN" ;;
  205. HINT|INFO) printf '%s' "$TERM_ICON_HINT" ;;
  206. *) printf '%s' "?" ;;
  207. esac
  208. }
  209. # ─── Primitives ───────────────────────────────────────────────────────────
  210. # term_repeat <char> <n>
  211. term_repeat() {
  212. local ch=$1 n=$2 i out=""
  213. for (( i=0; i<n; i++ )); do out="$out$ch"; done
  214. printf '%s' "$out"
  215. }
  216. # term_truncate <text> <max_cols> — ellipsis-truncate, append "…" or "..".
  217. term_truncate() {
  218. local text=$1 max=$2
  219. local len=${#text}
  220. if [[ $len -le $max ]]; then printf '%s' "$text"; return; fi
  221. local ell="…"
  222. [[ "$TERM_ASCII_MODE" -eq 1 ]] && ell=".."
  223. local elllen=${#ell}
  224. printf '%s%s' "${text:0:$((max - elllen))}" "$ell"
  225. }
  226. # ─── Panel ────────────────────────────────────────────────────────────────
  227. # term_panel_open <emoji_key> <name> [right_indicator]
  228. # ╭── ⚡ name ───────── <indicator> ───●
  229. term_panel_open() {
  230. local key=$1 name=$2 indicator=${3:-}
  231. local emoji
  232. emoji=$(term_brand_glyph "$key")
  233. local left="${TERM_PANEL_TL}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE} ${emoji} $(term_color cyan "$name") "
  234. local right=""
  235. if [[ -n "$indicator" ]]; then
  236. right=" $(term_color dim "$indicator") ${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}$(term_color cyan "$TERM_PANEL_TERM")"
  237. else
  238. right="${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}$(term_color cyan "$TERM_PANEL_TERM")"
  239. fi
  240. # Visible (color-stripped) widths to size the rule fill correctly.
  241. local left_vis="${TERM_PANEL_TL}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE} ${emoji} ${name} "
  242. local right_vis=""
  243. [[ -n "$indicator" ]] && right_vis=" ${indicator} ${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_TERM}" \
  244. || right_vis="${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_TERM}"
  245. local fill=$(( TERM_WIDTH - ${#left_vis} - ${#right_vis} ))
  246. [[ $fill -lt 4 ]] && fill=4
  247. local rule
  248. rule=$(term_repeat "$TERM_PANEL_HRULE" "$fill")
  249. printf '%s%s%s\n' "$left" "$(term_color cyan "$rule")" "$right"
  250. }
  251. # term_panel_close [hotkeys] [health_indicators]
  252. # ╰── R refresh · L land · ? help ───── • daemon • 17m ───●
  253. # `hotkeys`: pre-formatted "R refresh · L land · ? help" string.
  254. # `healths`: pre-formatted "• daemon • 17m" string.
  255. term_panel_close() {
  256. local hotkeys=${1:-} healths=${2:-}
  257. local left="${TERM_PANEL_BL}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE} ${hotkeys} "
  258. local right=""
  259. if [[ -n "$healths" ]]; then
  260. right=" ${healths} ${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}$(term_color cyan "$TERM_PANEL_TERM")"
  261. else
  262. right="${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}$(term_color cyan "$TERM_PANEL_TERM")"
  263. fi
  264. local left_vis="${TERM_PANEL_BL}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE} ${hotkeys} "
  265. local right_vis=""
  266. [[ -n "$healths" ]] && right_vis=" ${healths} ${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_TERM}" \
  267. || right_vis="${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_HRULE}${TERM_PANEL_TERM}"
  268. local fill=$(( TERM_WIDTH - ${#left_vis} - ${#right_vis} ))
  269. [[ $fill -lt 4 ]] && fill=4
  270. local rule
  271. rule=$(term_repeat "$TERM_PANEL_HRULE" "$fill")
  272. printf '%s%s%s\n' "$left" "$(term_color cyan "$rule")" "$right"
  273. }
  274. # term_panel_vert — emit a single body-line spacer "│"
  275. term_panel_vert() {
  276. printf '%s\n' "$(term_color dim "$TERM_TREE_VERT")"
  277. }
  278. # ─── Body components ──────────────────────────────────────────────────────
  279. # term_section <state> <label> <count>
  280. # ├── LABEL (n) (label colored by state)
  281. term_section() {
  282. local state=$1 label=$2 count=$3
  283. local color=""
  284. case "$state" in
  285. RUNNING|PENDING|CONFLICT|WARN|warning) color="yellow" ;;
  286. READY|LANDED|DONE|OK|healthy) color="green" ;;
  287. FAILED|ERROR|critical|alarm) color="red" ;;
  288. *) color="" ;;
  289. esac
  290. local rendered_label="$label"
  291. [[ -n "$color" ]] && rendered_label=$(term_color "$color" "$label")
  292. printf '%s%s %s %s\n' \
  293. "$(term_color dim "$TERM_TREE_VERT")" \
  294. "$(term_color dim "$TERM_TREE_BRANCH$TERM_PANEL_HRULE")" \
  295. "$rendered_label" \
  296. "$(term_color dim "($count)")"
  297. }
  298. # term_summary_line <text> — dim metadata branch
  299. # ├── text
  300. term_summary_line() {
  301. printf '%s%s %s\n' \
  302. "$(term_color dim "$TERM_TREE_VERT")" \
  303. "$(term_color dim "$TERM_TREE_BRANCH$TERM_PANEL_HRULE")" \
  304. "$(term_color dim "$*")"
  305. }
  306. # term_leaf_line <connector> <name> <leaf_glyph> <meta> <age>
  307. # │ ├── name ●─●─●─◉ M4 ?1 12m
  308. # `connector` = ├── or └──
  309. term_leaf_line() {
  310. local conn=$1 name=$2 leaf=$3 meta=${4:-} age=${5:-}
  311. local trunc_name
  312. trunc_name=$(term_truncate "$name" 28)
  313. printf '%s %s %-28s %-14s %-10s %s\n' \
  314. "$(term_color dim "$TERM_TREE_VERT")" \
  315. "$(term_color dim "$conn$TERM_PANEL_HRULE")" \
  316. "$trunc_name" \
  317. "$leaf" \
  318. "$(term_color dim "$meta")" \
  319. "$(term_color dim "$age")"
  320. }
  321. # term_toast <emoji_key> <text> — ├── ⚡ text (dim cyan)
  322. term_toast() {
  323. local key=$1; shift
  324. local emoji
  325. emoji=$(term_brand_glyph "$key")
  326. printf '%s%s %s\n' \
  327. "$(term_color dim "$TERM_TREE_VERT")" \
  328. "$(term_color dim "$TERM_TREE_BRANCH$TERM_PANEL_HRULE")" \
  329. "$(term_color cyan "$emoji $*")"
  330. }
  331. # term_alert <severity> <text> — ▲ message (orange/red), as a sub-row
  332. # `severity` = warning | critical
  333. term_alert() {
  334. local sev=$1; shift
  335. local color="orange"
  336. [[ "$sev" == "critical" ]] && color="red"
  337. printf '%s %s %s %s\n' \
  338. "$(term_color dim "$TERM_TREE_VERT")" \
  339. "$(term_color dim "$TERM_TREE_VERT")" \
  340. "$(term_color "$color" "$TERM_GLYPH_ALERT")" \
  341. "$*"
  342. }
  343. # ─── Leaf glyph builders ──────────────────────────────────────────────────
  344. # term_rail <commits_ahead> <head_state>
  345. # head_state: HEAD | CONFLICT | EMPTY
  346. # Examples:
  347. # term_rail 3 HEAD → ●─●─●─◉
  348. # term_rail 4 HEAD → ●─●─●─●─◉
  349. # term_rail 1 HEAD → ●─◉
  350. # term_rail 3 CONFLICT → ●─●─⊗
  351. # term_rail 0 EMPTY → ─
  352. term_rail() {
  353. local n=$1 head=${2:-HEAD}
  354. local commit="●"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && commit="*"
  355. local link="─"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && link="-"
  356. local headg="◉"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && headg="@"
  357. local conflict="⊗"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && conflict="X"
  358. if [[ $n -le 0 && "$head" == "EMPTY" ]]; then printf '%s' "$link"; return; fi
  359. local out=""
  360. local i
  361. # n landed commits, joined by links
  362. for (( i=0; i<n-1; i++ )); do
  363. out="${out}$(term_color green "$commit")${link}"
  364. done
  365. # final glyph
  366. case "$head" in
  367. HEAD)
  368. if [[ $n -ge 1 ]]; then out="${out}$(term_color green "$commit")${link}"; fi
  369. out="${out}$(term_color yellow "$headg")"
  370. ;;
  371. CONFLICT)
  372. if [[ $n -ge 1 ]]; then out="${out}$(term_color green "$commit")${link}"; fi
  373. out="${out}$(term_color red "$conflict")"
  374. ;;
  375. *)
  376. [[ $n -ge 1 ]] && out="${out}$(term_color green "$commit")"
  377. ;;
  378. esac
  379. printf '%s' "$out"
  380. }
  381. # term_pip_bar <metric_type> <filled> <total>
  382. # metric_type: progress | score | capacity
  383. # filled / total are integers (e.g., 30, 100)
  384. term_pip_bar() {
  385. local kind=$1 filled=$2 total=$3
  386. local pip_full="▰"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && pip_full="#"
  387. local pip_empty="▱"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && pip_empty="-"
  388. local width=10
  389. [[ "$total" -ne 100 && "$total" -gt 0 && "$total" -le 12 ]] && width=$total
  390. # Pip count
  391. local pips
  392. if [[ "$total" -eq 100 ]]; then
  393. pips=$(( filled / 10 ))
  394. else
  395. pips=$filled
  396. fi
  397. [[ $pips -lt 0 ]] && pips=0
  398. [[ $pips -gt $width ]] && pips=$width
  399. # Color selection
  400. local color="green"
  401. local pct=$(( total > 0 ? filled * 100 / total : 0 ))
  402. case "$kind" in
  403. progress) color="yellow"; [[ $pct -ge 100 ]] && color="green" ;;
  404. score) if [[ $pct -lt 33 ]]; then color="red"
  405. elif [[ $pct -lt 66 ]]; then color="yellow"
  406. else color="green"; fi ;;
  407. capacity) if [[ $pct -ge 80 ]]; then color="red"
  408. elif [[ $pct -ge 60 ]]; then color="yellow"
  409. else color="green"; fi ;;
  410. esac
  411. local i out=""
  412. for (( i=0; i<pips; i++ )); do out="${out}$(term_color "$color" "$pip_full")"; done
  413. for (( i=pips; i<width; i++ )); do out="${out}$(term_color dim "$pip_empty")"; done
  414. printf '%s' "$out"
  415. }
  416. # ─── Right-side furniture ─────────────────────────────────────────────────
  417. # term_health <state> <text> — • text (colored bullet, with ⬤ for busted)
  418. # state: healthy|pending|warning|critical|busted|unknown
  419. term_health() {
  420. local state=$1; shift
  421. local glyph
  422. glyph=$(term_health_glyph "$state")
  423. local color=""
  424. case "$state" in
  425. healthy) color="green" ;;
  426. pending) color="yellow" ;;
  427. warning) color="orange" ;;
  428. critical) color="red" ;;
  429. busted) color="dim" ;;
  430. *) color="dim" ;;
  431. esac
  432. printf '%s %s' "$(term_color "$color" "$glyph")" "$*"
  433. }
  434. # term_hotkey <key> <verb> — "R refresh" (key in cyan)
  435. term_hotkey() {
  436. printf '%s %s' "$(term_color cyan "$1")" "$2"
  437. }
  438. # ─── Spinners (live mode) ─────────────────────────────────────────────────
  439. # term_spinner_frame <family> <tick> — return frame at `tick % frames`.
  440. # family: working | heartbeat
  441. term_spinner_frame() {
  442. local fam=$1 tick=$2
  443. local -a frames
  444. case "$fam" in
  445. working) frames=("${TERM_SPIN_WORKING[@]}") ;;
  446. heartbeat) frames=("${TERM_SPIN_HEARTBEAT[@]}") ;;
  447. *) printf '?'; return ;;
  448. esac
  449. local n=${#frames[@]}
  450. printf '%s' "${frames[$(( tick % n ))]}"
  451. }
  452. # ─── Legacy / kept-for-compat helpers (used by older scripts) ─────────────
  453. # term_header <title> [meta] — "── title ────── meta" (legacy)
  454. term_header() {
  455. local title=$1 meta=${2:-}
  456. local glyph="─"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && glyph="-"
  457. local pad=$(( TERM_WIDTH - ${#title} - 6 ))
  458. [[ $pad -lt 4 ]] && pad=4
  459. local line
  460. line="$(term_repeat "$glyph" 2) $(term_color cyan "$title") $(term_repeat "$glyph" "$pad")"
  461. if [[ -n "$meta" ]]; then
  462. printf '%s %s\n' "$line" "$(term_color dim "$meta")"
  463. else
  464. printf '%s\n' "$line"
  465. fi
  466. }
  467. term_divider() {
  468. local w=${1:-$TERM_WIDTH}
  469. local glyph="─"; [[ "$TERM_ASCII_MODE" -eq 1 ]] && glyph="-"
  470. printf '%s\n' "$(term_repeat "$glyph" "$w")"
  471. }
  472. term_tree_item() {
  473. local icon=$1 label=$2 meta=${3:-}
  474. if [[ -n "$meta" ]]; then
  475. printf ' %s %-32s %s\n' "$icon" "$label" "$(term_color dim "$meta")"
  476. else
  477. printf ' %s %s\n' "$icon" "$label"
  478. fi
  479. }
  480. term_tree_connector() {
  481. if [[ "$1" -eq "$2" ]]; then printf '%s' "$TERM_TREE_LAST"
  482. else printf '%s' "$TERM_TREE_BRANCH"; fi
  483. }
  484. term_tree_indent() {
  485. if [[ "$1" -eq 1 ]]; then printf ' '
  486. else printf '%s ' "$TERM_TREE_VERT"; fi
  487. }
  488. term_tree_node() {
  489. local prefix=$1 conn=$2 label=$3 meta=${4:-}
  490. if [[ -n "$meta" ]]; then
  491. printf '%s%s %-32s %s\n' "$prefix" "$conn" "$label" "$(term_color dim "$meta")"
  492. else
  493. printf '%s%s %s\n' "$prefix" "$conn" "$label"
  494. fi
  495. }
  496. term_table_row() {
  497. printf ' %-2s %-32s %-10s %s\n' "${1:-}" "${2:-}" "${3:-}" "${4:-}"
  498. }
  499. term_empty() {
  500. printf ' %s\n' "$(term_color dim "($*)")"
  501. }