Skip to content

feat(options): template expressions in segment options#7455

Open
MO2k4 wants to merge 8 commits intoJanDeDobbeleer:mainfrom
MO2k4:worktree-template-aware-options
Open

feat(options): template expressions in segment options#7455
MO2k4 wants to merge 8 commits intoJanDeDobbeleer:mainfrom
MO2k4:worktree-template-aware-options

Conversation

@MO2k4
Copy link
Copy Markdown
Contributor

@MO2k4 MO2k4 commented Apr 10, 2026

Summary

  • Segment options (Int, Float64, Bool, Color) now accept Go template expressions as string values, resolved at render time using the segment writer as context
  • Users can compute option values dynamically — e.g. max_width = '{{ sub .TerminalWidth 30 }}' to size a path based on terminal width
  • The path segment's getMaxWidth drops ~20 lines of manual template handling in favor of the new template-aware Int() accessor
  • JSON schema updated so max_depth and mixed_threshold accept string alongside integer
  • Docs added for template expressions in segment options

How it works

After Init, the segment writer is stored as a template context in the options map. When a typed accessor finds a string value with {{ }}, it resolves the template before parsing. If resolution or parsing fails, the default value is used.

Test plan

  • go test ./segments/options/ — template resolution tests for Int, Float64, Bool, Color
  • go test ./segments/ -run TestGetMaxWidth — path segment uses template-aware Int() correctly
  • Existing option accessor tests stay green (no regressions in non-template code paths)

Closes #7243

MO2k4 added 8 commits April 10, 2026 12:24
Add SetContext/getContext for storing template context on options.Map
via a sentinel key. Add resolveTemplate helper that checks for {{
syntax and resolves via template.Render before type conversion.
…ccessors

Each typed accessor now checks for string values containing {{ template
syntax and resolves them via template.Render before type conversion.
On failure, the default value is returned.
…check

- Extract setupTemplateTestEnv() to deduplicate mock environment setup
  repeated across 6 test functions (~100 lines removed)
- Remove redundant strings.Contains check in Color() since
  resolveTemplate() already performs that check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to use templating on segment options

1 participant