ui

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ui renders treewright's interactive terminal pieces.

Index

Constants

This section is empty.

Variables

View Source
var ErrCancelled = errors.New("cancelled")

ErrCancelled reports that the user dismissed the picker, or that there was no terminal to prompt on. Callers treat it as "do nothing", not as a failure.

Functions

func ColorEnabled

func ColorEnabled(w io.Writer) bool

ColorEnabled reports whether ANSI color is appropriate for w.

Three conditions must hold, and each rules out a way color goes wrong: NO_COLOR must be unset or empty (the no-color.org convention, which any value honors), TERM must not be "dumb", and w must be a terminal — so redirected or piped output never carries escape sequences into a file or a parser.

func Pick

func Pick(w io.Writer, header string, items []string) (int, error)

Pick shows a numbered menu on w and returns the zero-based index the user chose, or ErrCancelled.

Keys are read from /dev/tty rather than stdin so the picker still works when stdin has been redirected. With no controlling terminal there is nobody to prompt, so it cancels instead of hanging.

Types

type Cell

type Cell struct {
	Text  string
	Color Color
}

Cell is one table entry: its text, and how to display it.

func Colored

func Colored(s string, c Color) Cell

Colored makes a styled cell.

func Text

func Text(s string) Cell

Text makes an unstyled cell.

type Color

type Color uint8

Color is a display attribute for one piece of text.

const (
	Plain Color = iota
	Green
	Yellow
	Red
	Cyan
	Dim
)

The attributes treewright uses. Plain is the zero value, so a Cell that names no color renders as the terminal's own foreground rather than as a chosen one.

func (Color) Apply

func (c Color) Apply(s string, enabled bool) string

Apply wraps s in this color's escape codes, or returns it unchanged when color is off. Callers decide "on" once, per output stream, via ColorEnabled.

type Table

type Table struct {
	Headers []string
	Rows    [][]Cell
}

Table lays out rows in columns sized to their contents.

Sizing from content is the point: a fixed column width silently misaligns every subsequent column as soon as one value is longer than the guess, and worktree slugs have no bounded length.

func (*Table) Add

func (t *Table) Add(cells ...Cell)

Add appends a row.

func (*Table) Lines

func (t *Table) Lines(color bool) (header string, rows []string)

Lines renders the table as a header line plus one line per row.

Returning lines rather than writing them lets the same table feed both plain output and the interactive picker, which needs each row as a separate label.

Padding is computed from cell text only, never from the styled string: escape sequences occupy no columns, so measuring them would break alignment exactly when color is on.

func (*Table) Render

func (t *Table) Render(w io.Writer, color bool)

Render writes the table, header first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL