-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure codeblocks use accessible colors #183
Ensure codeblocks use accessible colors #183
Conversation
This commit leverages `cli` fork of `charmbracelet/glamour` with enhancement for configuring `chroma` formatter. It includes simple tests that enabling `gh` accessible features causes codeblocks to be downsampled to base 16 ANSI colors.
Looking back at the changes in the PR versus cli/cli#9821, I'm confirming the need to remove the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you link the fork changes, @andyfeller? I'd find it helpful so we understand what this is introducing from the fork.
Going to close this PR for the moment because there is a bit more work to do with changing this to use our fork of Will continue responding to comments in the mean time. 👍 |
This commit is v2 approach to testing for accessible colors and codeblock rendering using an in-house approach to identifying ANSI escape sequences and analyzing them for color depth. After talking with @williammartin, this is likely going to be refactored a third time leveraging a module to help with parsing escape sequences from text.
This commit is focused on PR feedback around codeblock testing and simplifying related code: 1. Use of new `WithOptions(...TermRendererOption) TermRendererOption` to clean up `WithTheme()` The `glamour` TermRendererOption pattern has a limitation that users cannot compose multiple options without duplicating code or building one-off anonymous functions. However, this commit takes advantage of an enhancement in cli/glamour#3 that allows users to leverage a helper to avoid building one-offs. 1. Use of new `leaanthony/go-ansi-parser` dependency for parsing ANSI escape sequences and display attributes In v1 of `markdown_test.go`, the codeblock tests were very simple, testing the result of output of markdown rendering against a string of ANSI escape sequences. The concern raised is that this was testing the result rather than behavior wanted. In v2 of `markdown_test.go`, the codeblock tests were refactored to use regex to extract and analyze ANSI escape sequences and display attributes. The concern raised was that this was a lot of logic to build and maintain and might benefit from a dependency that could do it. In v3 of `markdown_test.go`, a combination of v1 and v2 approaches for 1) testing that theme appropriate colors are used and 2) testing that ensures accessible display options are used when accessible experience is enabled
@jtmcg @williammartin : Thank you both for feedback while iterating on this PR! 🙇 I'm reopening it after iterating on feedback to make some improvements but also wanting to make the case for certain concerns. Important This PR is dependent on changes in cli/glamour#3 and has been updated to use the latest commit SHA from Changes
|
Thoughts about testing codeblocksOne of the concerns originally raised around the codeblock color testing was that it was hard to grok and maintain: go-gh/pkg/markdown/markdown_test.go Lines 153 to 178 in 2b16a44
The concern being the difficulty of reading ANSI escape sequences like: "\x1b[0m\x1b[38;5;235mfmt\x1b[0m\x1b[38;5;210m.\x1b[0m\x1b[38;5;35mPrintln\x1b[0m\x1b[38;5;210m(\x1b[0m\x1b[38;5;95m\"Hello, world!\"\x1b[0m\x1b[38;5;210m)\x1b[0m" I 💯 agree this isn't ideal and thus experimented with a couple different forms which I'm unsure are legitimately better:
I have 2 concerns with the above alternatives:
For these reasons, I kept the original v1 forms of the codeblock tests checking for specific color usage for confidence that reasonably contrasting colors relative to the theme are being displayed. If there is some other form these can take that is appropriate for testing without being a significant lift, then I appreciate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of questions and two suggestions, but otherwise this is looking good 👍 I have no further ideas about making the escape codes easier to grok other than what we already discussed, so 🤷
After discussing the changing nature of the tests with @jtmcg, this commit renames the test functions to make it apparent their purposes: testing we get the colors we want separate from checking for accessibility concerns. In addition, there were a number of other improvements to be more idiomatic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff! Thanks for all the back and forth. This really came together 👍
goCodeBlock := heredoc.Docf(` | ||
%[1]s%[1]s%[1]sgo | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Hello, world!") | ||
} | ||
%[1]s%[1]s%[1]s | ||
`, "`") | ||
|
||
shellCodeBlock := heredoc.Docf(` | ||
%[1]s%[1]s%[1]sshell | ||
# list all repositories for a user | ||
$ gh api graphql --paginate -f query=' | ||
query($endCursor: String) { | ||
viewer { | ||
repositories(first: 100, after: $endCursor) { | ||
nodes { nameWithOwner } | ||
pageInfo { | ||
hasNextPage | ||
endCursor | ||
} | ||
} | ||
} | ||
} | ||
' | ||
%[1]s%[1]s%[1]s | ||
`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: These big, formatted text blocks give me anxiety in testing 😅 However, as long as we aren't asserting text matching on either of these (which we aren't currently) I feel alright about it. Trying to sort through the ANSI output of this to debug tests would be a nightmare and is why the matching strings I added in Test_RenderColor
are so short
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a fair line in the sand :nod: I tend to value DRYing things up a little where reasonable that doesn't add brittleness. If these tests change, then I would hope this approach is revisited and changed to fit the evolving need. 💯
This commit leverages
cli
fork ofcharmbracelet/glamour
with enhancement for configuringchroma
formatter.It includes simple tests that enabling
gh
accessible features causes codeblocks to be downsampled to base 16 ANSI colors.