d35e1f2832
Render(v) treats JSON as the model: in -json mode it prints v as JSON; otherwise it paints it as text — object scalars on one line as key=value (keys blue, values bright white: a structural key/value distinction, not semantic color), nested objects indented, arrays one block per element, field order preserved via an order-keeping JSON decode. EmitJSON(v) is the JSON-only arm for commands that paint their own text. Operates on JSON only, so core stays protobuf-free (NFR-4). Adds White to the palette. The example gains an `inspect` command demoing Render (text vs -json). design.md FR-4.5/4.6 document the renderer and the "JSON is always the full record; synopsis-vs-detail is text-only" principle. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
43 lines
1.6 KiB
Go
43 lines
1.6 KiB
Go
// SPDX-FileCopyrightText: (C) Copyright 2026 Pim van Pelt <pim@ipng.ch>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cli
|
|
|
|
// Bright (high-intensity) ANSI color codes, plus the reset sequence. These are
|
|
// the palette consumed by Paint and Label; status words pop while unremarkable
|
|
// "normal" states can stay uncolored.
|
|
const (
|
|
Reset = "\x1b[0m"
|
|
Red = "\x1b[91m" // bright red
|
|
Green = "\x1b[92m" // bright green
|
|
Blue = "\x1b[94m" // bright blue
|
|
Yellow = "\x1b[93m" // bright yellow
|
|
Cyan = "\x1b[96m" // bright cyan
|
|
White = "\x1b[97m" // bright white
|
|
)
|
|
|
|
// colorEnabled is process-global, toggled once at startup via SetColor. It
|
|
// defaults to false so output is script-safe unless a program opts in.
|
|
var colorEnabled bool
|
|
|
|
// SetColor turns colorized output on or off for Paint and Label. Call it once
|
|
// at startup (e.g. from a -color flag): color is useful in an interactive shell
|
|
// but noise when piping one-shot output into scripts.
|
|
func SetColor(on bool) { colorEnabled = on }
|
|
|
|
// ColorEnabled reports whether colorization is currently on.
|
|
func ColorEnabled() bool { return colorEnabled }
|
|
|
|
// Paint wraps s in an ANSI color when color is enabled; otherwise it returns s
|
|
// unchanged, so the word still reads in scripts and on no-color terminals.
|
|
func Paint(s, code string) string {
|
|
if !colorEnabled {
|
|
return s
|
|
}
|
|
return code + s + Reset
|
|
}
|
|
|
|
// Label wraps s in blue when color is enabled. Use it for static field labels —
|
|
// the "key=" half of a "key=value" pair — so the value stands out in normal font.
|
|
func Label(s string) string { return Paint(s, Blue) }
|