feat(options): template expressions in segment options#7455
Conversation
Resubmitting with corrected formatting
JanDeDobbeleer
left a comment
There was a problem hiding this comment.
Three issues to address before merging:
1. 🔴 The .TerminalWidth example in the docs won't work
The example in the new "Template Expressions in Options" section uses:
max_width = '{{ sub .TerminalWidth 30 }}'and the prose below it says "segment-specific fields (like .TerminalWidth on the path segment) are available."
Neither is accurate. .TerminalWidth is not a field on Path (or any segment writer). TerminalWidth() is a method on runtime.Environment, stored in the unexported env field of Base — not reachable from a template. The global template cache (cache.Template) also has no terminal-width value. At runtime this template silently fails and max_width falls back to 0.
Working alternatives:
{{ .Env.COLUMNS }}— if the shell exports it (not guaranteed cross-platform)- Add a
TerminalWidth() intgetter toPaththat delegates topt.env.TerminalWidth()
2. 🟡 Sentinel key pollutes the user-facing options map
Storing the runtime context inside options.Map via "__template_context__" mixes internal state with user options. Consequences:
m.Any("__template_context__", nil)returns the live writer — any code that iterates all options (debug logging, serialisation, future tooling) will encounter it unexpectedly.- It creates a reference cycle:
Map → writer → Map(safe today because gob encoding happens beforeMapSegmentWithWriter, but fragile if that order ever changes).
A cleaner approach: wrap Map in a small struct with a separate ctx any field, keeping user options and runtime state cleanly separated.
3. 🟡 SetContext is absent from the Provider interface
SetContext only exists on the concrete Map type. Any mock Provider in tests silently never gets template resolution, and segment code that holds a Provider variable can't call SetContext without a type assertion. If template-aware options are a first-class feature, SetContext(ctx any) should be added to the Provider interface so all implementations are required to handle it.
|
Thanks for the detailed review. Before I start, I want to flag the blast radius of the "clean" fix for #2 so we can agree on scope. Issue 2 — wrap Today Ripple count from a quick grep:
It's a single mechanical commit, but it's a big one. Slimmer alternative If the goal is just to stop the sentinel key leaking into serialised output, we can keep The map-alias approach keeps the sentinel mechanism internally — slightly less elegant — but addresses the user-visible concern (no leak into JSON/YAML/TOML) and the reference cycle stays contained inside the package. Question Which do you prefer?
Happy to go either way, just want to make sure the bigger one is what you actually want before I touch every test fixture in the repo. |
|
@MO2k4 ignore the second bullet, however, I want to make sure it doesn't export when the user uses |
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
bf8bd4e to
5df92b3
Compare
|
@JanDeDobbeleer Pushed fixes for the three review items:
I've also run some local testing to see if there was a leak of the ctx, but it never was leaked. 🙂 |
Summary
Int,Float64,Bool,Color) now accept Go template expressions as string values, resolved at render time using the segment writer as contextmax_width = '{{ sub .TerminalWidth 30 }}'to size a path based on terminal widthgetMaxWidthdrops ~20 lines of manual template handling in favor of the new template-awareInt()accessormax_depthandmixed_thresholdacceptstringalongsideintegerHow 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, Colorgo test ./segments/ -run TestGetMaxWidth— path segment uses template-aware Int() correctlyCloses #7243