Documentation
¶
Overview ¶
Package ui owns the terminal styling primitives shared across every compliancekit subcommand: the severity-and-status color palette, the status glyph set, the TTY/NO_COLOR detector, and a thin Styler over lipgloss that subcommands ask for their colors instead of hand-coding ANSI escapes.
One source of truth keeps the look consistent across `scan`, `diff`, `doctor`, `checks list`, `waivers list`, `mapping list`, `notify --list`, and `policy validate`. A palette tweak lands in one place; snapshot tests under both color and plain modes catch accidental drift.
Disabling color ¶
Output is plain (no ANSI, no Unicode glyphs beyond ASCII fallbacks) when any of the following is true:
- stdout is not a TTY (CI runs, piped through tee, redirected to a file)
- the NO_COLOR environment variable is set, per the no-color.org spec
- the --no-color CLI flag is passed (forces plain even on a TTY)
- the CLICOLOR=0 environment variable is set
Subcommands query IsColorEnabled once at startup and pass the result through to a Styler (via NewStyler). The Styler renders either ANSI-colored or plain output depending on that one decision — no per-call branching in the call sites.
Audience for this package ¶
Authors of internal/cli/* subcommands. Not part of pkg/compliancekit; internal/ui is implementation detail and may evolve freely.
Index ¶
- Constants
- func IsColorEnabled(w io.Writer, forceOff bool) bool
- type Styler
- func (s *Styler) Accent(text string) string
- func (s *Styler) Bold(text string) string
- func (s *Styler) DiffMark(kind string) string
- func (s *Styler) Glyph(token string) string
- func (s *Styler) InSeverity(text string, sev compliancekit.Severity) string
- func (s *Styler) InStatus(text string, st compliancekit.Status) string
- func (s *Styler) Muted(text string) string
- func (s *Styler) Severity(sev compliancekit.Severity) string
- func (s *Styler) SeverityChip(sev compliancekit.Severity) string
- func (s *Styler) Status(st compliancekit.Status) string
- type Table
Constants ¶
const ( DiffKindAdded = "added" DiffKindRemoved = "removed" DiffKindExisting = "existing" )
DiffKindAdded / Removed / Existing are the three valid kind values for DiffMark + the Glyph map. Exported so callers don't repeat the string literals (and goconst keeps its peace about the triple occurrence in DiffMark).
Variables ¶
This section is empty.
Functions ¶
func IsColorEnabled ¶
IsColorEnabled reports whether the calling subcommand should emit ANSI-colored output to w. The decision walks the following gates in order; the first negative wins.
- The --no-color CLI flag (passed in by the caller via forceOff) forces plain output even on a TTY.
- NO_COLOR set to any value (per no-color.org) → plain.
- CLICOLOR=0 → plain (legacy convention from BSDs / git).
- w is not a *os.File whose Fd() is a terminal → plain. CI runs and piped output land here.
The default when none of the above triggers is color-on.
Subcommands call this once at startup, pass the result to NewStyler, and never branch on it themselves.
Types ¶
type Styler ¶
type Styler struct {
// Color reports whether the receiver emits ANSI escapes.
// Determined once at construction via [IsColorEnabled].
Color bool
// contains filtered or unexported fields
}
Styler renders short strings with consistent color + glyph treatment across compliancekit subcommands. It is the only thing subcommands need to hold to render styled output; pass one constructed via NewStyler down to the renderer.
A Styler with Color==false strips ANSI and substitutes ASCII fallback glyphs so its output is suitable for CI logs, file redirects, and `grep` pipelines. Width-stable: the column a glyph occupies is the same in both modes.
func NewStyler ¶
NewStyler returns a Styler whose Color flag matches the result of IsColorEnabled for w with the given --no-color flag. Pass the same w the caller will be writing styled output to.
func (*Styler) Accent ¶
Accent highlights the one thing the reader's eye should land on first — a command name in help text, a count of new findings.
func (*Styler) Bold ¶
Bold is plain bold without color. Used for section headers in `--help` output and the doctor pretty-printer.
func (*Styler) DiffMark ¶
DiffMark renders the leading +/-/= column of `compliancekit diff` with the new-green / resolved-grey / existing-muted treatment.
func (*Styler) Glyph ¶
Glyph returns the unicode glyph for a known token (status name, "added", "removed", etc.) or the ASCII fallback when color / unicode is disabled.
func (*Styler) InSeverity ¶
func (s *Styler) InSeverity(text string, sev compliancekit.Severity) string
InSeverity renders text in the color of sev without the chip brackets — useful for rendering numeric scores / counts in a band-appropriate color. No-op when Color is false.
func (*Styler) InStatus ¶
func (s *Styler) InStatus(text string, st compliancekit.Status) string
InStatus renders text in the color of st without the glyph + name prefix that Status adds. Use this when the caller wants to color a glyph or short token (e.g. doctor's probe markers) by status. No-op when Color is false.
func (*Styler) Muted ¶
Muted dims structural text — frame characters, separator dashes, "(empty)" hints. No-op when Color is false.
func (*Styler) Severity ¶
func (s *Styler) Severity(sev compliancekit.Severity) string
Severity renders the severity name padded for column alignment and colored per the palette. Unknown severities render in muted grey.
func (*Styler) SeverityChip ¶
func (s *Styler) SeverityChip(sev compliancekit.Severity) string
SeverityChip renders the severity as a compact uppercase chip (e.g. "[HIGH]") suitable for inline use in a finding row.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a small Unicode box-drawing table renderer. Use it for structured-list output across subcommands (`checks list`, `doctor`, `waivers list`, `mapping list`, etc.) so the look is consistent without re-templating each command.
Add header columns once, append rows, then call Render(styler). The styler picks colors for the frame characters; in plain mode the renderer drops to a simple ASCII frame with no separators inside the data rows so the output stays grep-friendly.
Column widths auto-fit the longest cell. Cells wider than the per-column MaxWidth (when set) get truncated with the same ellipsis padRight uses.
func (*Table) AddRow ¶
AddRow appends one data row. The cell count must match the header count; extra cells are dropped, missing cells are filled with "".