Skip to content
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

Add term renderer option for chroma formatter #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

williammartin
Copy link

Description

This relates to #376

Previously, code blocks were hardcoded to render 8-bit color depth regardless of user preferences. This made it difficult to provide accessible experiences for users who depend upon native terminals' ability to change how base 16 ANSI colors are viewed.

Now, with the functional options pattern, WithChromaFormatter term renderer allows users to choose a different chroma formatter, which allows for selection of one that downsamples colors when rendering code blocks.

Credit @andyfeller who actually wrote this, I'm really just the PR monkey.

Previously, code blocks were hardcoded to render 8-bit color depth
regardless of user preferences. This made it difficult to provide
accessible experiences for users who depend upon native terminals'
ability to change how base 16 ANSI colors are viewed.

Now, with the functional options pattern, `WithChromaFormatter` term
renderer allows users to choose a different chroma formatter, which
allows for selection of one that downsamples colors when rendering code
blocks.
@andyfeller
Copy link

andyfeller commented Mar 7, 2025

One other addition in this pull request is WithOptions function, which I want to highlight why we thought it was important to include. Let's look at how this is used and why we think it's important for charmbracelet/glamour

WithOptions

The functional option pattern is useful for gathering options to apply later, however it only allows logic to return a single option.

In the GitHub CLI extension module, extension authors can choose whether to allow markdown to render themes or colors. However the accessible-colors work in cli/go-gh needed to apply multiple options around 1) glamour style and 2) chroma formatter:

// WithTheme is a rendering option that sets the theme to use while rendering the markdown.
// It can be used in conjunction with [term.Theme].
// If the environment variable GLAMOUR_STYLE is set, it will take precedence over the provided theme.
func WithTheme(theme string) glamour.TermRendererOption {
	style := os.Getenv("GLAMOUR_STYLE")
	accessible := accessibility.IsEnabled()
	if style == "" || style == "auto" {
		switch theme {
		case "light", "dark":
			if accessible {
				return glamour.WithOptions(
					glamour.WithStyles(AccessibleStyleConfig(theme)),
					glamour.WithChromaFormatter("terminal16"),
				)
			}
			style = theme
		default:
			style = "notty"
		}
	}
	return glamour.WithStylePath(style)
}

The new WithOptions was based on NewTermRenderer handling of options:

glamour/glamour.go

Lines 66 to 87 in bdc4ec5

// NewTermRenderer returns a new TermRenderer the given options.
func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error) {
tr := &TermRenderer{
md: goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.DefinitionList,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
),
ansiOptions: ansi.Options{
WordWrap: defaultWidth,
ColorProfile: termenv.TrueColor,
},
}
for _, o := range options {
if err := o(tr); err != nil {
return nil, err
}
}

We thought this could benefit other glamour users in similar situations.

@caarlos0 caarlos0 added the enhancement New feature or request label Mar 7, 2025
@caarlos0 caarlos0 requested a review from andreynering March 7, 2025 17:07
Copy link
Member

@aymanbagabas aymanbagabas left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants