Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions outputbox/box.go
Original file line number Diff line number Diff line change
@@ -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")
}
34 changes: 34 additions & 0 deletions outputbox/box_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
Loading