term.sh 19 KB

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