feat(bonfire): SSU widgets on charm v2 behind the Output interface

- Spinner, gradient progress bar and the multi-row process view ported
  from SSU to bubbletea v2 (input off, no signal handler, command
  context); boxed tables through lipgloss/table
- Prompts use huh fields and a single-select port of SSU's selector with
  its confirmation screen on an interactive terminal; line prompts off a
  terminal and under --no-ansi are unchanged (piped passwords still work)
- -q hides decoration but keeps data and errors; -n returns defaults and
  Secret fails with a hint on a terminal stdin; --no-ansi and non-TTY
  runs print today's plain lines
- Helpers: GlobalsOf, VerbosityOf, IsQuiet, Interactive, ConfirmProceed,
  Process, Current, Failed, PrintAbove, ItemProgress, ErrAborted
- diary's styled console lines print above a running live view
This commit is contained in:
Jakub Zych
2026-10-21 12:30:53 +02:00
parent cb38c493ce
commit 3f38862b22
15 changed files with 1266 additions and 198 deletions

View File

@@ -4,18 +4,14 @@ import (
"fmt"
"io"
"os"
"strings"
"charm.land/lipgloss/v2"
"github.com/charmbracelet/colorprofile"
"golang.org/x/term"
)
const (
ansiReset = "\x1b[0m"
ansiRed = "\x1b[31m"
ansiGreen = "\x1b[32m"
ansiYellow = "\x1b[33m"
ansiCyan = "\x1b[36m"
ansiDim = "\x1b[90m"
symCheck = "✓"
symCross = "✗"
symWarn = "⚠"
@@ -48,16 +44,25 @@ type Progress interface {
}
type console struct {
in io.Reader
out io.Writer
err io.Writer
lookup func(string) string
tty bool
in io.Reader
out io.Writer
err io.Writer
lookup func(string) string
// tty is true when stdout is a terminal; it picks animated widgets
// and boxed tables.
tty bool
// interactive is true when stdin and stdout are terminals.
interactive bool
color bool
// inTTY is true when stdin is a terminal; Secret then reads a hidden
// value instead of a line.
inTTY bool
// color is the environment's color policy (NO_COLOR, TERM=dumb,
// FORCE_COLOR, terminal) for ColorAuto.
color bool
// globals are the parsed global options of the running command, set
// by the root before the command runs.
globals Globals
palette Palette
}
// NewOutput builds Output from injected streams and the process color policy.
@@ -78,16 +83,17 @@ func newConsole(in io.Reader, out, errW io.Writer, lookup func(string) string, t
if lookup == nil {
lookup = os.Getenv
}
c := &console{in: in, out: out, err: errW, lookup: lookup}
c := &console{in: in, out: out, err: errW, lookup: lookup, palette: DefaultPalette()}
if tty != nil {
c.tty = *tty
} else {
c.tty = isTerminal(out)
}
c.inTTY = isTerminal(in)
if interactive != nil {
c.interactive = *interactive
} else {
c.interactive = isTerminal(in) && c.tty
c.interactive = c.inTTY && c.tty
}
c.color = supportsColor(lookup, c.tty)
return c
@@ -115,6 +121,67 @@ func isTerminal(rw any) bool {
return term.IsTerminal(int(f.Fd()))
}
// setGlobals records the parsed global options of the running command.
func (c *console) setGlobals(g Globals) {
c.globals = g
}
// live reports whether widgets animate: stdout is a terminal and none of
// -q, --no-ansi and -n is set (D-03). A live view that is already running
// makes nested widgets print plain lines.
func (c *console) live() bool {
g := c.globals
return c.tty && !g.Quiet && g.Color != ColorNever && !g.NoInteraction && !liveActive()
}
// canPrompt reports whether prompts may wait for input from a person:
// stdin and stdout are terminals and -n (or -q) is not set.
func (c *console) canPrompt() bool {
return c.interactive && !c.globals.NoInteraction
}
// profile is the color profile of styled output: none for --no-ansi or
// when the environment turns color off, at least 256 colors otherwise.
func (c *console) profile() colorprofile.Profile {
switch c.globals.Color {
case ColorNever:
return colorprofile.NoTTY
case ColorAuto:
if !c.color {
return colorprofile.NoTTY
}
}
return DetectProfile(c.out, ColorAlways, c.environ())
}
// environ rebuilds the color-related environment from lookup.
func (c *console) environ() []string {
var env []string
for _, k := range []string{"TERM", "COLORTERM", "NO_COLOR", "FORCE_COLOR", "CLICOLOR", "CLICOLOR_FORCE"} {
if v := c.lookup(k); v != "" {
env = append(env, k+"="+v)
}
}
return env
}
// styled wraps w so palette styles follow the color profile.
func (c *console) styled(w io.Writer) io.Writer {
return &colorprofile.Writer{Forward: w, Profile: c.profile()}
}
// emit writes s, a styled line or block, to w. While a live view runs on
// stdout, lines for stdout print above it instead of tearing it.
func (c *console) emit(w io.Writer, s string) {
if w == nil {
return
}
if w == c.out && printAboveLines(s) {
return
}
_, _ = io.WriteString(c.styled(w), s)
}
func (c *console) Write(p []byte) (int, error) {
if c.out == nil {
return 0, io.ErrClosedPipe
@@ -126,22 +193,38 @@ func (c *console) Println(a ...any) {
if c.out == nil {
return
}
fmt.Fprintln(c.out, a...)
s := fmt.Sprintln(a...)
if printAboveLines(s) {
return
}
_, _ = io.WriteString(c.out, s)
}
func (c *console) Printf(format string, a ...any) {
if c.out == nil {
return
}
fmt.Fprintf(c.out, format, a...)
s := fmt.Sprintf(format, a...)
if strings.HasSuffix(s, "\n") && printAboveLines(s) {
return
}
_, _ = io.WriteString(c.out, s)
}
// status prints a status line unless -q is set.
func (c *console) status(style lipgloss.Style, symbol, message string) {
if c.globals.Quiet {
return
}
c.emit(c.out, style.Render(symbol)+" "+message+"\n")
}
func (c *console) Info(message string) {
c.Println(c.style(ansiCyan, symInfo) + " " + message)
c.status(c.palette.Current, symInfo, message)
}
func (c *console) Success(message string) {
c.Println(c.style(ansiGreen, symCheck) + " " + message)
c.status(c.palette.Success, symCheck, message)
}
func (c *console) Error(message string) {
@@ -149,30 +232,11 @@ func (c *console) Error(message string) {
if w == nil {
w = c.out
}
if w == nil {
return
}
fmt.Fprintln(w, c.style(ansiRed, symCross)+" "+message)
c.emit(w, c.palette.Error.Render(symCross)+" "+message+"\n")
}
func (c *console) Warning(message string) {
c.Println(c.style(ansiYellow, symWarn) + " " + message)
}
func (c *console) style(code, s string) string {
if !c.color {
return s
}
return code + s + ansiReset
}
func (c *console) dim(s string) string {
return c.style(ansiDim, s)
}
// setGlobals records the parsed global options of the running command.
func (c *console) setGlobals(g Globals) {
c.globals = g
c.status(c.palette.Warning, symWarn, message)
}
func boolPtr(v bool) *bool { return &v }