termtitle 0.1.0

Intelligently sets terminal window/tab titles based on configurable rules
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# termtitle

Automatically sets terminal window and tab titles based on configurable rules as
you cd between directories.

## Installation

```bash
cargo install termtitle
```

Or build from source:

```bash
cargo build --release
cp target/release/termtitle ~/.local/bin/  # or somewhere in your PATH
```

Add the shell hook to your shell config:

**Zsh** (`~/.zshrc`):
```zsh
eval "$(termtitle hook zsh)"
```

**Bash** (`~/.bashrc`):
```bash
eval "$(termtitle hook bash)"
```

**Fish** (`~/.config/fish/config.fish`):
```fish
termtitle hook fish | source
```

## Usage

termtitle uses a rule-based system to determine titles. Rules are evaluated in
order, and the first match wins.

> **Note:** termtitle uses OSC escape sequences for setting terminal titles.
> These work in most modern terminal emulators including iTerm2, Terminal.app,
> Windows Terminal, and others.

### Commands

```bash
termtitle hook <shell>    # Output shell hook (zsh, bash, or fish)
termtitle apply           # Apply title based on rules for current directory
termtitle reset           # Reset title to default
termtitle inspect         # Show what title would be set and why
termtitle set <target> <title>  # Directly set a terminal title
termtitle config          # Show current configuration
termtitle config --edit   # Open config file in $EDITOR
termtitle config --path   # Print config file path
```

## How It Works

1. Shell hook calls `termtitle apply` on every directory change
2. `apply` evaluates rules in config order, first match wins
3. Matching rule provides template variables (name, branch, etc.)
4. Template is rendered and emitted as OSC escape sequences

## Configuration

Configuration is stored at `~/.config/termtitle/config.toml`:

```toml
shell_timeout_ms = 500      # Timeout for shell_command rules (milliseconds)
fallback_title = "{dir}"    # Title when no rules match
default_targets = ["both"]  # Options: "window", "tab", "both"

# Rules are evaluated in order, first match wins
[[rules]]
filename = "package.json"
path = "name"

[[rules]]
filename = "Cargo.toml"
path = "package.name"

[[rules]]
type = "git"
template = "{repo}:{branch}"
```

## Rule Types

Each rule type detects specific conditions and provides template variables.

### Structured Files (JSON/TOML)

Read values from JSON or TOML files. Type is inferred from extension when omitted.

```toml
[[rules]]
filename = "package.json"
path = "name"                    # Single value → {value} or {name}
template = "{name}"

[[rules]]
filename = "Cargo.toml"
path = "package.name"
```

**Multiple values with `paths`:**

```toml
[[rules]]
filename = "package.json"
paths = ["name", "version"]      # Creates {name} and {version}
template = "{name}{ v{version}}" # Use conditional for optional version
```

With `paths`, all specified paths must exist for the rule to match (unless used
in conditional segments).

**Search modes:**
- `search = "up"` (default) — search current and parent directories
- `search = "current"` — only current directory
- `search = "parent"` — skip current, search parents only

### Git Repository

```toml
[[rules]]
type = "git"
template = "{repo}:{branch}"
```

Variables: `{repo}`, `{branch}`, `{commit}`, `{status}` (shows `*` if dirty),
`{remote}`, `{upstream}`

### Directory Name

```toml
[[rules]]
type = "directory_name"
search = "current"
template = "{dir}"
```

### File Exists

```toml
[[rules]]
type = "file_exists"
filename = ".termtitle"
use_content = true               # Read first line into {content}
template = "{content}"
```

Variables: `{dir}`, `{filename}`, `{content}` (if `use_content = true`)

**Glob pattern matching:**

Use glob patterns to match files dynamically. When a pattern matches, the `{match}` variable contains the matched portion without extension:

```toml
[[rules]]
type = "file_exists"
filename = "*.xcodeproj"
template = "{match}"             # Shows "MyApp" for "MyApp.xcodeproj"
```

Variables:
- `{match}` — matched portion without extension (e.g., `"MyApp"` from `"MyApp.xcodeproj"`)
- `{filename}` — original pattern (e.g., `"*.xcodeproj"`)
- `{dir}` — directory name

Exact filenames (no wildcards) still work for backward compatibility.

### Environment Variable

```toml
[[rules]]
type = "env_var"
env_name = "PROJECT_NAME"
template = "{value}"
```

### Shell Command

```toml
[[rules]]
type = "shell_command"
command = "basename $(pwd)"
timeout_ms = 200                 # Override global timeout
template = "{value}"
```

## Template Syntax

Templates use `{variable}` placeholders that are replaced with values from rule
providers.

### Basic Variables

```toml
template = "{name}"              # Simple variable
template = "{repo}:{branch}"     # Multiple variables
```

### Fallback Values

Use `{var:fallback}` to provide a default when the variable is missing:

```toml
template = "{env:production}"    # Shows "production" if env is unset
```

### Conditional Segments

Use `{ segment }` (space after opening brace) to hide content when variables are
missing. If any variable in the segment lacks a fallback, the entire segment
disappears:

```toml
template = "{name}{ v{version}}{ by {author}}"
#          ^required  ^optional    ^optional
```

Output examples:
- All present: `"myapp v1.0.0 by Tom"`
- No author: `"myapp v1.0.0"`
- No version or author: `"myapp"`

### Format Modifiers

Apply transformations using `{var|modifier}` or `{var|modifier:arg}`:

| Modifier | Arguments | Example | Result |
|----------|-----------|---------|--------|
| `truncate` | max length | `{name\|truncate:20}` | `"very-long-name-he..."` |
| `upper` | none | `{branch\|upper}` | `"MAIN"` |
| `lower` | none | `{name\|lower}` | `"myapp"` |
| `title` | none | `{name\|title}` | `"My App"` |
| `icon` | value=symbol map | `{status\|icon:ok=✓,fail=✗}` | `"✓"` |
| `prefix` | string | `{branch\|prefix:[}` | `"[main"` |
| `suffix` | string | `{branch\|suffix:]}` | `"main]"` |
| `basename` | none | `{dir\|basename}` | `"project"` |
| `dirname` | none | `{path\|dirname}` | `"/home/user"` |
| `ext` | none | `{file\|ext}` | `"rs"` |
| `stem` | none | `{file\|stem}` | `"main"` |
| `parent` | depth (default 1) | `{dir\|parent:2}` | grandparent name |

Modifiers can be chained: `{dir|basename|upper}` → `"PROJECT"`

## Compound Rules

Compound rules merge multiple providers into a single title, combining data from
different sources.

```toml
[[rules]]
type = "compound"
template = "{pkg.name}{ [{git.branch}]}"
require_all = false              # At least one component must match (default)

  [rules.components.pkg]
  filename = "package.json"
  path = "name"

  [rules.components.git]
  type = "git"
```

### How It Works

- Each component is a nested rule configuration
- Component names become variable prefixes: `{pkg.name}`, `{git.branch}`
- With `require_all = false`: at least one component must match
- With `require_all = true`: all components must match or rule is skipped

### Multiple Values from One File

Use `paths` (plural) to extract multiple values:

```toml
[rules.components.pkg]
filename = "package.json"
paths = ["name", "version"]      # Creates {pkg.name} and {pkg.version}
```

### Component Search Modes

Each component can have its own search mode, useful for monorepos:

```toml
[[rules]]
type = "compound"
template = "{workspace.name} → {project.name}"

  [rules.components.workspace]
  filename = "package.json"
  path = "name"
  search = "parent"              # Find workspace root

  [rules.components.project]
  filename = "package.json"
  path = "name"
  search = "current"             # Current package only
```

## Title Targets

termtitle can set different terminal title elements:

| Target | Description | OSC Code |
|--------|-------------|----------|
| `window` | Window title | OSC 2 |
| `tab` | Tab/icon name | OSC 1 |
| `both` | Both window and tab | OSC 0 |

Rules continue evaluating until all targets are satisfied:

```toml
# Short name in tab
[[rules]]
filename = "package.json"
path = "name"
template = "{name}"
targets = ["tab"]

# Full context in window
[[rules]]
type = "git"
template = "{repo}:{branch}"
targets = ["window"]
```

## Examples

### Web Development

```toml
# Package name in tab, git context in window
[[rules]]
filename = "package.json"
path = "name"
targets = ["tab"]

[[rules]]
type = "git"
template = "{repo}:{branch}"
targets = ["window"]
```

### Microservices with Environment

```toml
[[rules]]
type = "compound"
template = "{pkg.name}{ • {env:dev}}"
require_all = false

  [rules.components.pkg]
  filename = "package.json"
  path = "name"

  [rules.components.env]
  type = "env_var"
  env_name = "NODE_ENV"
```

### Monorepo (Workspace + Package)

```toml
[[rules]]
type = "compound"
template = "{workspace.name} → {project.name}"
require_all = false

  [rules.components.workspace]
  filename = "package.json"
  path = "name"
  search = "parent"

  [rules.components.project]
  filename = "package.json"
  path = "name"
  search = "current"
```

### Git with Dirty Indicator

```toml
[[rules]]
type = "git"
template = "{repo}:{branch}{status}"  # Shows * when dirty
```

### Version-Tagged Project

```toml
[[rules]]
filename = "package.json"
paths = ["name", "version"]
template = "{name}{ v{version}}"
```

### Multi-Language Detection

```toml
[[rules]]
filename = "package.json"
path = "name"
template = "{name} [Node]"

[[rules]]
filename = "Cargo.toml"
path = "package.name"
template = "{value} [Rust]"

[[rules]]
filename = "pyproject.toml"
path = "project.name"
template = "{value} [Python]"

[[rules]]
type = "git"
template = "{repo}"
```

### Custom Project Marker

```bash
echo "my-project" > ~/Code/my-project/.termtitle
```

```toml
[[rules]]
type = "file_exists"
filename = ".termtitle"
use_content = true
template = "{content}"
```

### DevOps/Infrastructure

```toml
[[rules]]
type = "file_exists"
filename = "main.tf"
template = "{dir} [Terraform]"

[[rules]]
filename = "Chart.yaml"
path = "name"
template = "{value} [Helm]"
```

### Fallback Chain Pattern

When you need strict matching with graceful fallback:

```toml
# First: try with all fields (strict)
[[rules]]
filename = "package.json"
paths = ["name", "version", "author"]
template = "{name} v{version} by {author}"

# Fallback: just name and version
[[rules]]
filename = "package.json"
paths = ["name", "version"]
template = "{name} v{version}"
```

Or use conditional segments for the same effect in one rule:

```toml
[[rules]]
filename = "package.json"
paths = ["name", "version", "author"]
template = "{name} v{version}{ by {author}}"
```

## License

MIT