Skip to content

Latest commit

 

History

History
126 lines (91 loc) · 5.04 KB

title.md

File metadata and controls

126 lines (91 loc) · 5.04 KB

render/title.sh

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

Overview

title.sh sets the terminal window/tab title (OSC 0) to the live §11.4.182 label of an entity, a session, or an explicit string.

It exists so that with several shells, agents and sessions open, the title bar tells you which is which — (T1/main - claude4 - opus - high) rather than a bare bash.

Three properties make it safe to call from anywhere:

  • Safe in a non-tty — it writes nothing and exits 0.
  • Safe under tmux and screen — the escape sequence is emitted through the right sink rather than being mangled by the multiplexer.
  • 0 always — a title is cosmetic and must never fail a caller. A failed title.sh inside a prompt hook would be a bug about decoration taking down a session.

Prerequisites

  • bash, jq; render/lib.sh beside it (sourced).
  • A label-sync snapshot in $LABEL_SYNC_DIR for --entity / --session forms.
  • No files are required for the --text form.

Environment variables (names only):

Variable Meaning
LABEL_SYNC_DIR State dir holding snapshot.json (see CONTRACT.md).
LABEL_SYNC_TITLE_SINK Default sink file for the escape sequence.
TERM, TMUX, STY Terminal / multiplexer detection.
LABEL_SYNC_SHELL_ID Default entity when --entity is omitted.
CLAUDE_CODE_SESSION_ID Session used when neither --entity nor --session is given.

Usage examples

# set the title from an explicit string
bash scripts/label-sync/render/title.sh --text "build: gates"

# title this shell's entity (default from LABEL_SYNC_SHELL_ID)
bash scripts/label-sync/render/title.sh

# title a specific entity or session
bash scripts/label-sync/render/title.sh --entity "$entity_id"
bash scripts/label-sync/render/title.sh --session "$CLAUDE_CODE_SESSION_ID"

# write to a sink file instead of the tty
bash scripts/label-sync/render/title.sh --sink /tmp/title.out

# pass the sequence through unwrapped
bash scripts/label-sync/render/title.sh --passthrough

Flags

Flag Meaning
--text T Use an explicit title string.
--entity ID Title the named entity.
--session SID Title the named session.
--sink FILE Write the escape sequence to FILE instead of a tty.
--passthrough Emit the sequence without the standard wrapping.

The three selectors are mutually exclusive in intent; the resolution order is: explicit --text, then --entity, then --session, then the defaults ($LABEL_SYNC_SHELL_ID, else $CLAUDE_CODE_SESSION_ID).

Exit codes

Code Meaning
0 Always. A title is cosmetic and must never fail a caller.

There is no failure code by design. A caller that needs to know whether a title was written must inspect the sink, because "no title written" and "title written" both exit 0.

Output

The OSC 0 escape sequence, written to the sink file when --sink is given, or to stdout/stderr only if one of them is a tty. Otherwise nothing is written at all.

Edge cases

  • Not a tty and no --sink. Writes nothing, exits 0 — the reason it is safe in CI and in hooks.
  • Under tmux / screen. The sequence is routed appropriately rather than being interpreted by the outer terminal.
  • Nothing resolvable (no --text, no entity, no LABEL_SYNC_SHELL_ID, no session id). The title is left alone — it does not clear or overwrite an existing title with a placeholder.
  • A snapshot that is down. The entity/session form cannot resolve, so the title is left alone.
  • A label containing control characters. Sanitised via ls_sanitize — a title is an escape-sequence context, which is exactly where unsanitised text is dangerous.
  • --sink to an unwritable path. The write fails but the exit status stays 0; titles are cosmetic.
  • --passthrough with a non-tty. Still writes nothing unless a tty or sink is present.

Internal behaviour

  1. Sources render/lib.sh.
  2. Resolves the title text: --text, else ls_entity_label for --entity/--session, else the default ids.
  3. If nothing resolves, returns without writing (title untouched).
  4. Sanitises the text with ls_sanitize.
  5. Delegates to ls_emit_title, which selects the sink (explicit file, else a tty) and writes the OSC 0 sequence.
  6. Exits 0.

Related scripts

Sources scripts/label-sync/render/lib.sh.

Sibling renderers: scripts/label-sync/render/statusline.sh, scripts/label-sync/render/tmux-status.sh, scripts/label-sync/render/shell-hook.sh (which typically calls title.sh on each prompt), scripts/label-sync/render/watch-labels.sh.

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

See also docs/scripts/lib.md, docs/scripts/statusline.md and docs/scripts/track_branch_label.md.

Last verified

2026-12-04 — bash -n scripts/label-sync/render/title.sh → exit 0. bash scripts/label-sync/render/title.sh --text "probe" --sink /tmp/title-probe.out exits 0 and writes the OSC 0 sequence to the sink file.