ui

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ui holds every terminal UI component: lipgloss styles, the reusable stack tree renderer and the bubbletea checkout picker. It is the only package allowed to import Charm libraries.

Index

Constants

This section is empty.

Variables

View Source
var ErrCancelled = fmt.Errorf("cancelled")

ErrCancelled is returned when the user dismisses a prompt.

Functions

func Busy

func Busy(w io.Writer, enabled bool) func()

Busy marks the terminal as busy with an indeterminate progress indicator (a pulsing tab or taskbar entry, depending on the terminal) and returns a function that clears it. With enabled false (not a terminal) it does nothing. Terminals without OSC 9;4 support ignore the sequence.

func FuzzyMatch

func FuzzyMatch(s, q string) (bool, []int)

FuzzyMatch reports whether q is a case-insensitive subsequence of s and returns the matched rune indices.

func Hyperlink(enabled bool, url, text string) string

Hyperlink wraps text in an OSC 8 hyperlink to url when enabled, so terminals such as Ghostty, iTerm2 and WezTerm make it clickable. With enabled false (not a terminal) or an empty url it returns text unchanged.

func RelativeTime

func RelativeTime(t, now time.Time) string

RelativeTime renders t relative to now ("3h ago").

func RenderLog

func RenderLog(v *app.View, o LogOptions) string

RenderLog renders every stack the way `gt log` does: trunk at the bottom, each stack a column rising from it, newest branch at the top, with the pull request and age under each branch name.

● feat-b
│  #13 draft · 2d ago
│
○ feat-a
│  #12 open · 3h ago
│
│ ○ hotfix
│ │  no PR · 1w ago
├─┘
■ main

A trunk with no stacks is still drawn, so an empty repository shows where a stack would start. Untracked branches are not shown. It returns "" when there is no trunk at all.

func RenderRow

func RenderRow(tr TreeRow, o RenderOptions) string

RenderRow renders one tree row: prefix, name, PR, restack marker, age and worktree.

func RenderTree

func RenderTree(rows []TreeRow, o RenderOptions) string

RenderTree renders every row, one per line, without selection.

func ShortPath added in v0.1.1

func ShortPath(p string) string

ShortPath shows a path under $HOME as ~/...; other paths are unchanged.

func WithSpinner

func WithSpinner(ctx context.Context, in io.Reader, out io.Writer, message string, fn func(ctx context.Context) error) error

WithSpinner runs fn while showing a spinner and message on the terminal. It returns fn's error; ctrl+c cancels with context.Canceled.

Types

type LogOptions

type LogOptions struct {
	Styles Styles
	Now    time.Time
	// Links makes PR references OSC 8 hyperlinks.
	Links bool
}

LogOptions controls RenderLog.

type Picker

type Picker struct {
	// contains filtered or unexported fields
}

Picker is the bubbletea model. It is exported for tests.

func NewPicker

func NewPicker(ctx context.Context, o PickerOptions) *Picker

NewPicker builds the model.

func (*Picker) Init

func (p *Picker) Init() tea.Cmd

Init starts the background refresh, if any.

func (*Picker) Result

func (p *Picker) Result() PickerResult

Result returns the choice once the program has finished.

func (*Picker) Update

func (p *Picker) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update handles keys, window size and refresh results.

func (*Picker) View

func (p *Picker) View() tea.View

View renders the picker inline.

type PickerOptions

type PickerOptions struct {
	Rows []app.Row
	// Refresh, when set, is run in the background; its result updates the
	// PR decorations while the picker is open.
	Refresh func(ctx context.Context) ([]forge.PullRequest, error)
	// Styles defaults to DefaultStyles when nil.
	Styles *Styles
	Now    time.Time
	// Height limits the number of visible rows (0: derive from the terminal).
	Height int
}

PickerOptions configures the checkout picker.

type PickerResult

type PickerResult struct {
	Branch    string
	Cancelled bool
}

PickerResult is the user's choice.

func RunPicker

func RunPicker(ctx context.Context, in io.Reader, out io.Writer, o PickerOptions) (PickerResult, error)

RunPicker runs the picker on the given terminal streams.

type Prompter

type Prompter struct {
	In  io.Reader
	Out io.Writer
	Ctx context.Context
}

Prompter asks yes/no and pick-one questions on the terminal. It satisfies app.Prompter.

func (Prompter) Confirm

func (p Prompter) Confirm(question string, defaultYes bool) (bool, error)

Confirm asks a yes/no question.

func (Prompter) Select

func (p Prompter) Select(question string, options []string) (int, error)

Select asks the user to pick one option and returns its index.

type RenderOptions

type RenderOptions struct {
	Styles Styles
	Now    time.Time
	// Selected highlights the row (picker cursor).
	Selected bool
}

RenderOptions controls RenderRow.

type Reporter

type Reporter struct {
	// contains filtered or unexported fields
}

Reporter prints every human facing line the CLI produces, so all commands share one look (see docs/style.md). On a terminal it uses marks, colour, emoji headlines and spinners; piped it falls back to plain ASCII prefixes (ok:, note:, error:) with the same words.

func NewReporter

func NewReporter(in io.Reader, out, err io.Writer, o ReporterOptions) *Reporter

NewReporter builds a Reporter over the given streams. Terminal writers are wrapped so colour is downsampled to what the terminal supports and dropped under NO_COLOR; hyperlinks and the busy state pass through.

func (*Reporter) Branch

func (r *Reporter) Branch(name string) string

Branch styles a branch name for use inside a message.

func (*Reporter) Error

func (r *Reporter) Error(err error)

Error prints an error on stderr: the message, any detail (faint) and the next steps. It renders stack.Error specially and anything else as one line.

func (*Reporter) Info

func (r *Reporter) Info(format string, args ...any)

Info prints a detail line on stdout, indented under the result line.

func (r *Reporter) Link(url string) string

Link makes url clickable on a terminal and returns it unchanged otherwise.

func (r *Reporter) Links() bool

Links reports whether stdout gets OSC 8 hyperlinks.

func (*Reporter) Print

func (r *Reporter) Print(block string)

Print writes an already rendered block (a tree) to stdout as is.

func (*Reporter) Quiet

func (r *Reporter) Quiet() bool

Quiet reports whether non-error output is suppressed.

func (*Reporter) Ref

func (r *Reporter) Ref(number int, url string) string

Ref styles a pull request reference (#123) and links it to url.

func (*Reporter) SHA

func (r *Reporter) SHA(sha string) string

SHA styles a commit id for use inside a message.

func (*Reporter) Step

func (r *Reporter) Step(ctx context.Context, phase app.Phase, message string, fn func(ctx context.Context) error) error

Step runs fn under a headline for the phase: an emoji, the message and a spinner on a terminal (the line is replaced by whatever is printed next when fn returns), or the message printed once when piped. The terminal's busy state is set for the duration. Output written to Stream while the step runs is shown above the spinner.

func (*Reporter) Stream

func (r *Reporter) Stream() io.Writer

Stream returns a writer for a subprocess's live output. Each line is shown in the "│" gutter on stderr (plain indent when piped), above the spinner while a Step is running.

func (*Reporter) Styles

func (r *Reporter) Styles() Styles

Styles returns the palette for stdout: the default styles on a terminal, the zero value (no escape codes) when piped.

func (*Reporter) Success

func (r *Reporter) Success(format string, args ...any)

Success prints a result line on stdout: "✔ Created feat-a on main".

func (*Reporter) Warn

func (r *Reporter) Warn(format string, args ...any)

Warn prints a notice on stderr: "⚠ gh stack submits the whole stack".

type ReporterOptions

type ReporterOptions struct {
	// OutTTY / ErrTTY: stdout / stderr are terminals (marks and colour).
	OutTTY, ErrTTY bool
	// Spinners enables animated headlines; needs a terminal on stdin and
	// stderr. Without it Step prints the headline once as a plain line.
	Spinners bool
	// Quiet drops everything but errors.
	Quiet bool
}

ReporterOptions describe the streams the Reporter writes to.

type Styles

type Styles struct {
	Trunk     lipgloss.Style
	Branch    lipgloss.Style
	Current   lipgloss.Style
	Untracked lipgloss.Style
	Prefix    lipgloss.Style
	Selected  lipgloss.Style
	Match     lipgloss.Style
	PROpen    lipgloss.Style
	PRDraft   lipgloss.Style
	PRMerged  lipgloss.Style
	PRClosed  lipgloss.Style
	Restack   lipgloss.Style
	Muted     lipgloss.Style
	Help      lipgloss.Style
	Title     lipgloss.Style
	// Reporter marks.
	Success lipgloss.Style
	Warning lipgloss.Style
	Error   lipgloss.Style
}

Styles groups the lipgloss styles used by the tree and picker.

func DefaultStyles

func DefaultStyles() Styles

DefaultStyles returns the standard palette (ANSI colours so NO_COLOR and 16-colour terminals degrade gracefully).

type TreeRow

type TreeRow struct {
	Row app.Row
	// Prefix holds the tree glyphs ("├─ ", "│  └─ ").
	Prefix string
	// Matched positions in Row.Name when a filter is active.
	Matched []int
	// IsMatch is true when the row itself matched the filter (as opposed to
	// being kept as an ancestor of a match).
	IsMatch bool
}

TreeRow is a display row: the app row plus its tree prefix and, after filtering, the matched rune positions.

func BuildTree

func BuildTree(rows []app.Row) []TreeRow

BuildTree computes tree prefixes for rows in display order. Stacks are linear, so only the branches directly above a trunk can have siblings.

func FilterTree

func FilterTree(rows []TreeRow, query string) []TreeRow

FilterTree keeps rows matching query plus their ancestors, so the tree context survives filtering. An empty query returns every row.

Jump to

Keyboard shortcuts

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