ui

package
v1.19.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
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

func IsColorEnabled(w io.Writer, forceOff bool) bool

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.

  1. The --no-color CLI flag (passed in by the caller via forceOff) forces plain output even on a TTY.
  2. NO_COLOR set to any value (per no-color.org) → plain.
  3. CLICOLOR=0 → plain (legacy convention from BSDs / git).
  4. 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

func NewStyler(w io.Writer, forceOff bool) *Styler

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

func (s *Styler) Accent(text string) string

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

func (s *Styler) Bold(text string) string

Bold is plain bold without color. Used for section headers in `--help` output and the doctor pretty-printer.

func (*Styler) DiffMark

func (s *Styler) DiffMark(kind string) string

DiffMark renders the leading +/-/= column of `compliancekit diff` with the new-green / resolved-grey / existing-muted treatment.

func (*Styler) Glyph

func (s *Styler) Glyph(token string) string

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

func (s *Styler) Muted(text string) string

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.

func (*Styler) Status

func (s *Styler) Status(st compliancekit.Status) string

Status renders the status glyph + colored name. ASCII fallback substitutes the glyph and drops color when Color is false. Pad width is stable across modes so columns line up in both forms.

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 NewTable

func NewTable(headers ...string) *Table

NewTable returns an empty table with the given headers.

func (*Table) AddRow

func (t *Table) AddRow(cells ...string)

AddRow appends one data row. The cell count must match the header count; extra cells are dropped, missing cells are filled with "".

func (*Table) MaxWidth

func (t *Table) MaxWidth(i, n int) *Table

MaxWidth sets the truncation width for column index i (0-based). Cells wider than n get truncated with the project's standard ellipsis. Zero means "unlimited"; columns left unset have no cap.

func (*Table) Render

func (t *Table) Render(s *Styler) string

Render writes the table to a string using s's color decision. In color mode the frame is rendered in the muted color and the header is bold. In plain mode the frame uses plain ASCII (`+-|`).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL