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

Colorize severity in table output #1284

Merged
merged 6 commits into from
Oct 30, 2023

Conversation

shanedell
Copy link
Contributor

@shanedell shanedell commented May 9, 2023

Colorize severity in table output

  • Create flag "--no-color" to allow disabling the color. By default its enabled. Responds to the NO_COLOR and CLICOLOR_FORCE environment variables.
  • default output behavior for the table presenter is to highlight severity in its color:
    • Critical -> Bold Red
    • High -> Red
    • Medium -> Yellow
    • Low -> Green
    • Negligible -> Blue
    • Note: Golang doesn't have all colors available. Also, doesn't seem to be able use hex codes properly.

Closes #225

Note:

When you look at https://github.com/olekukonko/tablewriter/blob/master/table_with_color.go#L63 there are only so many colors available. Through testing I couldn't take the color hex codes from https://github.com/anchore/grype-vscode/blob/main/src/ui/react-components/FindingsList.tsx#L148-L162 and make them work in Golang. So I went with the below colors based on ones available:

  • Critical -> Bold Red
  • High -> Red
  • Medium -> Yellow
  • Low -> Green
  • Negligible -> Blue
  • Others -> Non-colored

Open to other suggestions if anyone is able to point me to other ways of coloring the tablewriter.

@spiffcs
Copy link
Contributor

spiffcs commented May 30, 2023

Here is a quick sample output to show what this looks like in the new table output.

I think it's a great change and 👍
Screenshot 2023-05-30 at 11 11 07 AM

cc @wagoodman for a final comment. I thought we might want to inverse so --no-color is default and users could add --color to stay consistent with our current default, but after running it a couple times to test and look at the output I like the color =)

@spiffcs
Copy link
Contributor

spiffcs commented May 30, 2023

@shanedell let me try and get these conflicts updated for you

@wagoodman
Copy link
Contributor

wagoodman commented May 30, 2023

Re: --color vs --no-color: I'm a fan of including both (spf13/pflag#214 with example workaround of knative/client#346), but we don't have support for this with other options today. Do we like this option (CC @anchore/tools )?

As for the default, I feel like the default should be to show colors by default. However, what's missing is the ability to know when colors are not supported by the underlying terminal. This can be done a few different ways / heuristics, but there are some underlying libs that support this already... I'd recommend using termenv for this:

This way the functionality gracefully degrades based on what the environment supports or hints at.

@shanedell
Copy link
Contributor Author

shanedell commented Jun 12, 2023

@spiffcs I fixed the merge conflicts but did you also want me to incorporate what @wagoodman mentions with termenv before merging? Shouldn't to much of an issue as I believe there would just need to be check if !noColor && termenv.ColorProfile() == termenv.Ascii and then sets noColor = true when that condition is true right?

- Create flag "--no-color" to allow disabling the color. By default its enabled.
- When "--no-color" not specified highlight severity in its color:
  - Critical -> Bold Red
  - High -> Red
  - Medium -> Yellow
  - Low -> Green
  - Negligible -> Blue
  - Note: Golang doesn't have all colors available. Also, doesn't seem to be able use hex codes properly.
- Add termenv to check if the terminal color profile supports colored output. If it doesn't default to noColor

Closes anchore#225

Signed-off-by: Shane Dell <shanedell100@gmail.com>
@shanedell shanedell force-pushed the table-output-color-severity branch from 6a81737 to 27962c1 Compare June 12, 2023 17:34
@shanedell
Copy link
Contributor Author

@spiffcs I added termenv to the PR to check if color output should be enabled by default or not

@willmurphyscode
Copy link
Contributor

Should we also respect the NO_COLOR environment variable? See https://no-color.org/. (Sorry if we are and I didn't realize it; I just did a search in the diff for NO_COLOR and didn't see anything.)

@wagoodman
Copy link
Contributor

wagoodman commented Jun 22, 2023

actually, we're about to move to bubbletea/lipgloss which might already respond to NO_COLOR env vars. I'll try it out and post back here. In the meantime let's hold off before adding a switch for this until we have an answer.

* main: (23 commits)
  Remove Docker section from DEVELOPING.md (anchore#1384)
  chore(deps): update bootstrap tools to latest versions (anchore#1381)
  chore(deps): bump github.com/docker/docker (anchore#1382)
  Port to new syft source API (anchore#1376)
  chore(deps): bump golang.org/x/term from 0.9.0 to 0.10.0 (anchore#1375)
  chore: bump quality gate labels and images (anchore#1374)
  chore(deps): update bootstrap tools to latest versions (anchore#1368)
  Add a simple CSV format template to the templates/ directory and tweak docs (anchore#1366)
  chore(deps): update Syft to v0.84.1 (anchore#1372)
  fix: Add more log4j-adjacent package ignore rules (anchore#1358)
  chore: bump the quality gate labels (anchore#1369)
  add oss community board auto-add workflow (anchore#1364)
  fix: totals for vulnerability matches (anchore#1359)
  chore(deps): bump ossf/scorecard-action from 2.1.3 to 2.2.0 (anchore#1363)
  chore(deps): bump anchore/sbom-action from 0.14.2 to 0.14.3 (anchore#1357)
  Configure chronicle to pre-1.0 mode (anchore#1356)
  chore(deps): update Syft to v0.84.0 (anchore#1354)
  chore(deps): update bootstrap tools to latest versions (anchore#1353)
  chore(deps): update Syft to v0.83.1 (anchore#1352)
  chore(deps): bump golang.org/x/term from 0.8.0 to 0.9.0 (anchore#1350)
  ...
@spiffcs spiffcs force-pushed the table-output-color-severity branch from 1c28195 to 74cc9be Compare July 11, 2023 17:39
Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
@wagoodman
Copy link
Contributor

@spiffcs indeed, but there is still one more case to cover. There is a related issue in syft that shows the case anchore/syft#1893 .. here's the offending line https://github.com/anchore/go-logger/blob/d65da42248673d1596c5c144a974db4a10d6af60/adapter/logrus/logger.go#L47

@spiffcs spiffcs added the blocked Progress is being stopped by something label Jul 11, 2023
@spiffcs
Copy link
Contributor

spiffcs commented Jul 11, 2023

Added the blocked label so that we can rebase this work on top of some WIP regarding a refactor of the UI library

@tgerla tgerla removed the blocked Progress is being stopped by something label Oct 26, 2023
…verity

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman
Copy link
Contributor

@shanedell I've updated the branch with the latest from main and also removed the CLI flag since this should already be determined from the underlying lipgloss lib. I still need to verify the correctness, but this at least gets the branch updated.

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
…verity

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman
Copy link
Contributor

wagoodman commented Oct 30, 2023

Here's an example of the behavior when there is no TTY on stdout, using CLICOLOR_FORCE to force showing colors anyway, and using NO_COLOR to suppress output of colors.

Screenshot 2023-10-30 at 9 47 55 AM

@wagoodman wagoodman enabled auto-merge (squash) October 30, 2023 13:50
@wagoodman wagoodman merged commit 81edd50 into anchore:main Oct 30, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Colorize severity in table output
5 participants