Repository navigation
fix(templatehelper): no styles in Ascii mode - #14
Conversation
I'm actually not sure this is the case, but I'm not aware of terminals which do support text style options that don't also support colors. And also, from the principle of least surprise -- I'd expect if my color profile is Ascii, I shouldn't be getting any ANSI escape codes in the output. (ie: my output is going to a file, not the terminal) |
2ec4253 to
a8fb303
Compare
If the color profile doesn't support ANSI color codes, then it likely shouldn't support any Style options either. This returns a no-op funcmap from termenv.TemplateFuncs when the color profile is Ascii.
a8fb303 to
7bf6236
Compare
| if !bytes.Equal(buf.Bytes(), expected) { | ||
| t.Fatalf("template output does not match golden file.\n--- Expected ---\n%s\n--- Actual ---\n%s\n", string(expected), string(actual)) | ||
| } |
There was a problem hiding this comment.
I'm not the biggest fan of doing this kind of comparison and output in tests, but I didn't want to introduce a test-only dependency unless you're ok with it.
A couple I had in mind to make these sorts of tests more readable/maintainable:
- https://github.com/sebdah/goldie for managing golden files (you can run the test with -update to update the files from the code output)
- https://github.com/stretchr/testify has test assertions with useful textual diffs.
There was a problem hiding this comment.
I agree, that's probably a sensible suggestion. I haven't looked into goldie yet, but I've used testify before.
|
Hi, First, really cool library 🚀 I wanted to use it and I ran into the same issue today. @muesli are you OK with this change? Do you consider merging it soon? It is just a helper function, so I just introduce my own. However, it would be to use built-in one ;) |
|
I'm not opposed to the change, it's the right thing to do. I didn't merge it so far, because it was consistent with the actual behavior of termenv's core API. That was always a design issue however and never the intended behavior. Recent API changes ( |
…re, docs/lint, and restore test coverage Resolves 13 of 14 review findings for the ANSI-safe truncation and preserve-resets milestone. ansi/truncate.go (muesli#5,muesli#6,muesli#7,muesli#13): Track the actual post-SGR terminal state by folding every SGR sequence's parameters in order (standalone 0 clears; extended-color 38/48/58 grouped as one color attribute; selective-resets remove their axis) so a zero-bearing real color such as 38;2;255;0;0 is no longer misread as a full reset and the final reset is driven by the true state, eliminating style bleed. Replace the lossy replay model (shared misc bucket, maxSGRParamLen cap) with per-code keyed attributes so independent attributes (e.g. bold+faint) and long valid groups replay faithfully. Tokenize and fold opts.Tail through the same state trackers, then close an OSC 8 hyperlink and/or append a final reset opened by the tail. Remove 14 stale //nolint:mnd via a selective-reset map and named color-introducer constants. ansi/tokenize.go (muesli#9,muesli#12): Correct the TokenSGR doc to describe the catch-all control bucket accurately; replace magic +2 introducer offsets with len(csi)/len(osc) and drop the unused //nolint:mnd. ansi.go (muesli#10): Correct facade docs to state the recognized CSI/OSC grammar precisely; signatures and the TruncateOptions alias unchanged. ansi/truncate_test.go (muesli#5,muesli#6,muesli#7,muesli#8): Require the trailing reset on compound/real-color cuts; assert faithful, linearly-bounded replay of long valid groups and independent attributes; add SGR- and OSC8-bearing tail-closure tests and grapheme-cluster/malformed-control cases. truncate_test.go (#1,#2,#3,#4,muesli#8,muesli#14): Restore the direct Output.String default-propagation test, the compile-time ansi/termenv TruncateOptions alias interop proof, ANSI-bearing tail tests through Output.Truncate and template helpers, the full preserve-resets truth table with option order/isolation, real TrueColor/ANSI256 color and cluster coverage; rename Ascii->ASCII locals and factor repeated literals into self-prefixed constants. Finding 11 (grammar broadening to RIS/DCS/SOS/PM/APC) is declined as it conflicts with the frozen AAP (SS 0.4.2, 0.1.1, 0.1.2, 0.5.2) and Rule C1, which scope the tokenizer to CSI/OSC; see resolution report. No public symbol removed or renamed; go.mod/go.sum, existing testdata goldens, and pre-existing tests unchanged. go build/vet/test (incl. -race) and golangci-lint (main+soft) all pass.
If the color profile doesn't support ANSI color codes, then it likely shouldn't support any Style options either. This returns a no-op funcmap from
termenv.TemplateFuncswhen the color profile isAscii.