Skip to content

Latest commit

 

History

History
117 lines (84 loc) · 5.56 KB

lib.md

File metadata and controls

117 lines (84 loc) · 5.56 KB

render/lib.sh

Revision: 1 Last modified: 2026-12-04T19:00:00Z

The guide filename is lib.md because the documentation convention keys each guide by its script's basename without extension. This guide is for scripts/label-sync/render/lib.sh specifically.

Overview

This is a SOURCE-ONLY library. It is never executed; sourcing it defines functions only, with no side effects beyond two read-only variable assignments.

render/lib.sh holds the shared helpers for the label-sync renderers: statusline.sh, title.sh, the shell integration, tmux-status.sh, watch-labels.sh and the per-run labeller. Pure bash plus jq.

Its most important function is ls_sanitize. Rendering a label means interpolating text derived from branch names and prompts into an escape sequence, a terminal title, or a status line. Unsanitised text there is how a branch name containing control characters corrupts a display or injects a terminal escape. The sanitiser is therefore strict and locale-independent, and the _into variant exists so the hot render path does not fork a subshell for every sanitisation.

The second is that "no data" has a defined answer: functions return 1 when there is nothing to report, and callers must render an honest ? — never an invented value (§11.4.6).

Prerequisites

  • bash, jq.
  • Sourced from the renderers, resolved relative to the sourcing script.
  • No files are required at source time; ls_entity_label reads a snapshot at call time.

Environment variables (names only):

Variable Meaning
LABEL_SYNC_DIR State dir (honoured by ls_dir).
LABEL_SYNC_LABELCTL Overrides the labelctl.sh path (honoured by ls_labelctl).
LABEL_SYNC_TITLE_SINK Default sink file for ls_emit_title.
TERM, TMUX, STY Terminal / multiplexer detection.

Usage examples

. "$(dirname "$0")/lib.sh"

# the state dir, respecting LABEL_SYNC_DIR
dir="$(ls_dir)"

# a rendered label, with a status marker when not plainly live
label="$(ls_entity_label "$entity_id")" || label="(T?/? - ? - ? - ?)"

# sanitise without forking
ls_sanitize_into SAFE "$raw_branch_name" 64

# write an OSC 0 title through the standard sink
ls_emit_title "$label"

Exported surface

Function Contract
ls_dir Prints the state dir (honours $LABEL_SYNC_DIR).
ls_labelctl Prints the path of labelctl.sh (honours $LABEL_SYNC_LABELCTL).
ls_entity_label ID Prints the rendered label of entity ID, plus a [stale] / [done] marker when applicable. Returns 1 when there is no such entity.
ls_emit_title TEXT [SINK] [PASSTHROUGH] Writes an OSC 0 title sequence to the sink (see title.sh).
ls_sanitize TEXT [MAX] Strict, locale-independent output sanitiser. Truncates to MAX.
ls_sanitize_into VAR TEXT [MAX] Same, assigned to VAR without a fork — the hot-path form.

Exit codes

Functions return 0 on success and 1 when there is nothing to report — no snapshot, or no such entity.

That 1 is a finding about absence, not an error. Callers are required to treat it as "unknown" and render an honest ? (for example (T?/? - ? - ? - ?)), never an invented value (§11.4.6). A renderer that swallowed the 1 and printed a default label would be converting "I do not know" into a false statement.

Edge cases

  • No snapshot on disk. ls_entity_label returns 1; the renderer prints the honest-unknown form.
  • A missing entity in an existing snapshot. Also 1 — "state is up, this entity is gone" is distinct from "state is down", and the renderers mark them differently ([no-session] vs [no-state]).
  • Control characters or newlines in a branch name. Removed by ls_sanitize; the sanitiser is locale-independent, so the same input yields the same output regardless of the caller's locale.
  • A very long label. Truncated to the MAX argument rather than overflowing a status line.
  • A non-tty. ls_emit_title writes nothing (a title is cosmetic).
  • jq absent. Sourcing still succeeds; functions that parse JSON fail at call time.
  • LABEL_SYNC_LABELCTL pointing nowhere. ls_labelctl prints it as given; the caller's build/exec then fails, surfacing the misconfiguration.

Internal behaviour

  1. On source, defines the functions and two read-only variables. Nothing else runs.
  2. ls_dir / ls_labelctl resolve state paths with environment overrides taking precedence.
  3. ls_entity_label reads snapshot.json from the state dir, looks up the entity, renders the §11.4.182 label, appends a [stale] / [done] marker when applicable, and returns 1 when the entity or snapshot is absent.
  4. ls_sanitize strips control sequences and enforces a length bound without consulting locale.
  5. ls_emit_title writes the OSC 0 escape sequence, gated on a tty or an explicit sink.

Related scripts

Sourced by the renderers: scripts/label-sync/render/statusline.sh, scripts/label-sync/render/title.sh, scripts/label-sync/render/shell-hook.sh, scripts/label-sync/render/tmux-status.sh, scripts/label-sync/render/watch-labels.sh.

Related: scripts/label-sync/labelctl.sh (reached via ls_labelctl).

Cross-reference: docs/label-sync/CONTRACT.md, docs/label-sync/RENDERERS.md.

See also docs/scripts/statusline.md, docs/scripts/title.md and docs/scripts/labelctl.md.

Last verified

2026-12-04 — bash -n scripts/label-sync/render/lib.sh → exit 0. Sourcing it in a bash shell and running type ls_sanitize prints the function definition (source succeeds, nothing executes).