Skip to content

fix(templatehelper): no styles in Ascii mode - #14

Merged
muesli merged 1 commit into
muesli:masterfrom
justenwalker:template-nocolor
Sep 22, 2022
Merged

muesli merged 1 commit into
muesli:masterfrom
justenwalker:template-nocolor

Conversation

@justenwalker

@justenwalker justenwalker commented Jul 24, 2020 •

Copy link
Copy Markdown
Contributor

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.

@justenwalker

justenwalker commented Jul 24, 2020 •

Copy link
Copy Markdown
Contributor Author

If the terminal doesn't support colors, also it doesn't support styles like Bold, Cross-out, etc.

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)

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.
Comment thread templatehelper_test.go
Comment on lines +38 to +40
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))
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that's probably a sensible suggestion. I haven't looked into goldie yet, but I've used testify before.

@mszostok

mszostok commented Aug 3, 2022 •

Copy link
Copy Markdown

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 ;)

@muesli

muesli commented Sep 22, 2022

Copy link
Copy Markdown
Owner

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 (termenv.Output) rectified this situation and as such I'm leaning towards (finally!) merging this PR.

@muesli
muesli merged commit 6dd5b52 into muesli:master Sep 22, 2022
blitzy Bot pushed a commit to blitzy-research/termenv that referenced this pull request Jul 24, 2026
…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.
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.

3 participants

⌂ 换网址