feat: golang-cli v1.0.0 — generic command-tree CLI library
Reusable, generics-based CLI extracted from vpp-evpn's cmd/evpnc: a declarative command tree (Node[C]) from which dispatch, '?'-help and TAB-completion are derived, an interactive Shell[C], dynamic slot resolvers (context-dependent via captured args), text-or-JSON output (Emit), and color helpers (Paint/Label/KV). Builds on Linux and OpenBSD (readline termios override). Includes a self-contained example and a design proposal under docs/. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// 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
|
||||
)
|
||||
|
||||
// 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) }
|
||||
Reference in New Issue
Block a user