diff --git a/README.md b/README.md index 9c3e333..862e3f9 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ core + deck. | `theme` | `Theme` + `Use` / `Cur` palettes and status helpers | | `glyph` | Nerd/Uni/ASCII variants, status strip, severity vocabulary | | `console` | Output capability detection: TTY, ANSI, Unicode, Powerline/Nerd Fonts | +| `outputbox` | Borderless, status-shaded command output blocks for scrollback | | `keys` | Keybinding tables | | `forms` | Field builders | | `list` / `browser` | List/browser helpers | @@ -119,6 +120,28 @@ Interactive apps use `deck.Model` as the session tea root. Pass the scope with themes/keys/views before `deck.Run`. Full contract: [`deck/INTERFACE.md`](deck/INTERFACE.md). +### Command output boxes + +`outputbox.Render` renders borderless command output blocks with full-width +status backgrounds: neutral running, green success, yellow review, red failure. +Explicit status labels remain readable with color disabled. Rendering is pure: +append each returned box to keep earlier actions in scrollback; no cursor moves, +terminal detection, or global renderer changes are performed. + +```go +import "github.com/codyconfer/viewkit/outputbox" + +box := outputbox.Render("go test ./...", []string{"ok example/package"}, + 80, true, outputbox.Success) +fmt.Fprintln(os.Stdout, box) +``` + +Width includes padding and clamps to at least eight cells. Long text wraps without +truncation, including wide Unicode. Pass the color capability resolved by your +application (for example from `console.Detect`); use `false` for plain transcripts. +Content may include trusted ANSI styles; sanitize untrusted terminal controls +before rendering. The package has no Bubble Tea dependency. + ### Inline CLI workflows `deck.Inline` provides sequential setup/onboarding panels without entering a diff --git a/outputbox/box.go b/outputbox/box.go new file mode 100644 index 0000000..a7f5ad4 --- /dev/null +++ b/outputbox/box.go @@ -0,0 +1,63 @@ +// Package outputbox renders append-only, status-colored command output blocks. +package outputbox + +import ( + "io" + "strings" + + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" + "github.com/muesli/termenv" +) + +// Status identifies the outcome shown in a box header and background. +type Status string + +const ( + // Pending marks an action still in progress. + Pending Status = "RUNNING" + // Success marks a completed action. + Success Status = "DONE" + // Warning marks output that needs review. + Warning Status = "REVIEW" + // Failure marks a failed action. + Failure Status = "ERROR" +) + +// Render returns a borderless box. Width includes horizontal padding. Without +// color the status label remains visible, including in redirected transcripts. +// Widths below eight cells are clamped to eight. Content may contain trusted ANSI +// styling; callers must sanitize terminal control sequences from untrusted input. +// Render does not write output, move the cursor, or change global color settings. +func Render(title string, lines []string, width int, color bool, status Status) string { + width = max(8, width) + inner := width - 4 + r := lipgloss.NewRenderer(io.Discard) + profile := termenv.Ascii + if color { + profile = termenv.ANSI256 + } + r.SetColorProfile(profile) + bg, fg := lipgloss.Color("236"), lipgloss.Color("252") + switch status { + case Success: + bg = "22" + case Warning: + bg = "58" + case Failure: + bg = "52" + } + row := r.NewStyle().Background(bg).Foreground(fg).Width(width).Padding(0, 2) + header := r.NewStyle().Bold(true) + // Wrap before styling so every physical line receives full-width shading. + content := []string{header.Render("[" + string(status) + "] " + title), ""} + content = append(content, lines...) + result := []string{row.Render("")} + for _, block := range content { + for _, line := range strings.Split(ansi.Wrap(block, inner, ""), "\n") { + result = append(result, row.Render(line)) + } + } + result = append(result, row.Render("")) + return strings.Join(result, "\n") +} diff --git a/outputbox/box_test.go b/outputbox/box_test.go new file mode 100644 index 0000000..c20d08f --- /dev/null +++ b/outputbox/box_test.go @@ -0,0 +1,34 @@ +package outputbox + +import ( + "strings" + "testing" + + "github.com/charmbracelet/x/ansi" +) + +func TestBoxStatusColorAndWidth(t *testing.T) { + backgrounds := map[Status]string{Pending: "236", Success: "22", Warning: "58", Failure: "52"} + for _, status := range []Status{Pending, Success, Warning, Failure} { + for _, color := range []bool{false, true} { + text := Render("gcx login ops", []string{strings.Repeat("界", 40), "complete"}, 40, color, status) + if !strings.Contains(ansi.Strip(text), "["+string(status)+"]") || strings.Count(ansi.Strip(text), "界") != 40 { + t.Fatal("missing status or truncated output", text) + } + if strings.ContainsAny(ansi.Strip(text), "╭╰│") || strings.Contains(text, "\x1b[J") { + t.Fatal("unexpected chrome/cursor movement") + } + if strings.Contains(text, "\x1b[") != color { + t.Fatal("wrong color mode") + } + for _, line := range strings.Split(text, "\n") { + if ansi.StringWidth(line) != 40 { + t.Fatalf("expected full-width shading: %q", line) + } + } + if color && !strings.Contains(text, "48;5;"+backgrounds[status]+"m") { + t.Fatalf("missing %s status background: %q", status, text) + } + } + } +}