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 --force-color flag to zed validate #435

Merged
merged 7 commits into from
Nov 7, 2024
Merged

Add --force-color flag to zed validate #435

merged 7 commits into from
Nov 7, 2024

Conversation

tstirrat15
Copy link
Contributor

Fixes #434

Description

See #434. There are non-terminal environments (e.g. some CI environments) that support color, but lipgloss assumes that a non-terminal environment is uncolored. This provides a --force-color flag that allows the user to override this behavior and get color output for zed validate, which is the only place we're using lipgloss.

Changes

  • Add flag for color forcing
  • Add PreRunE that interprets the flag
  • Thunk the functions that produce output so that they correctly pick up the changes to the DefaultRenderer
  • Propagate the thunks

Testing

Review. See that things are green. The way that I was able to test this locally was comparing:

docker run -v './zed:/zed' -v '/etc/ssl/certs:/etc/ssl/certs' busybox ./zed validate https://gist.github.com/ecordell/8e3b613a677e3c844742cf24421c08b6

docker run -v './zed:/zed' -v '/etc/ssl/certs:/etc/ssl/certs' busybox ./zed validate https://gist.github.com/ecordell/8e3b613a677e3c844742cf24421c08b6 --force-color

Where the command is run from a folder that has a built zed binary.

setForceColor := cobrautil.MustGetBool(cmd, "force-color")
if setForceColor {
lipgloss.SetColorProfile(termenv.ANSI256)
fmt.Println(lipgloss.DefaultRenderer().ColorProfile() == termenv.ANSI256)
Copy link
Contributor

Choose a reason for hiding this comment

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

why the need to print here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch 🤦

traceStyle = lipgloss.NewStyle().Bold(true)
// NOTE: these need to be set *after* the renderer has been set, otherwise
// the forceColor setting can't work, hence the thunking.
success = func() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the cost of initializing this each time? alternatively, these could be left as uninitialized vars, and initialize them after we've checked the flag

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that, but that spreads the definitions around the file, and some of the definitions are used by multiple functions. I don't think it's particularly expensive, though that isn't because I've profiled it.

vroldanbet
vroldanbet previously approved these changes Nov 7, 2024
@tstirrat15 tstirrat15 merged commit 74289dc into main Nov 7, 2024
11 checks passed
@tstirrat15 tstirrat15 deleted the 434-force-color branch November 7, 2024 20:29
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to Force Color Output in zed validate Command
3 participants