components

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package components is the set of shapes a command prints in.

There is no template engine between a command and its terminal: the markup each component prints is written directly into the component itself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureDynamicContentIsHighlighted

func EnsureDynamicContentIsHighlighted(s string) string

EnsureDynamicContentIsHighlighted emboldens every [bracketed] run.

The escape sequence is written here rather than a markup tag, because there is no formatter between this and the terminal.

func EnsureNoPunctuation

func EnsureNoPunctuation(s string) string

EnsureNoPunctuation drops a trailing . ? ! or : .

It is what keeps a task description from reading "migrating the database. ....... DONE".

func EnsurePunctuation

func EnsurePunctuation(s string) string

EnsurePunctuation adds a full stop when there is none.

A line component is a sentence, and a sentence ends. The empty string is the exception: it returns empty rather than a bare full stop.

Types

type Alert

type Alert struct{ Component }

Alert renders a message in a box, in capitals.

func NewAlert

func NewAlert(output Output, base string) Alert

NewAlert returns the component.

func (Alert) Render

func (a Alert) Render(message string, verbosity ...Verbosity)

Render writes the alert.

The band is as wide as the terminal and three lines tall: a blank padding line, the message centered and inverted, and another blank padding line. The message is uppercased through [upperVisible] rather than strings.ToUpper, because the message has already been through EnsureDynamicContentIsHighlighted and a naive uppercase would mangle the escape sequences that mutator adds.

There is a blank line above the band and none below: whatever renders next supplies its own leading margin, the way Line.Render does with its own trailing NewLine.

type Ask

type Ask struct{ Component }

Ask puts a question and returns the answer.

func NewAsk

func NewAsk(output Output, base string) Ask

NewAsk returns the component.

func (Ask) Render

func (a Ask) Render(question, def string) (string, error)

Render asks.

type AskWithCompletion

type AskWithCompletion struct{ Component }

AskWithCompletion is Ask with a list the terminal completes from.

func NewAskWithCompletion

func NewAskWithCompletion(output Output, base string) AskWithCompletion

NewAskWithCompletion returns the component.

func (AskWithCompletion) Render

func (a AskWithCompletion) Render(question string, choices []string, def string) (string, error)

Render asks.

type BulletList

type BulletList struct{ Component }

BulletList renders one line per element, each with a leading mark.

func NewBulletList

func NewBulletList(output Output, base string) BulletList

NewBulletList returns the component.

func (BulletList) Render

func (b BulletList) Render(elements []string, verbosity ...Verbosity)

Render writes the list.

The dim colour covers the whole line, mark and element together, not just the mark.

type Choice

type Choice struct{ Component }

Choice offers a numbered list and returns the option that was picked.

The prompt keeps asking until it gets an answer it understands.

func NewChoice

func NewChoice(output Output, base string) Choice

NewChoice returns the component.

func (Choice) Render

func (c Choice) Render(question string, choices []string, def string) (string, error)

Render asks.

type Component

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

Component is what every console component holds: the output it renders into and the root it shortens paths against.

The markup is written straight to the stream, with no template or output buffer in between.

func NewComponent

func NewComponent(output Output, base string) Component

NewComponent returns a component over an output.

base is the application root, and it is what EnsureRelativePaths strips. Passing it in is what keeps a component testable.

func (Component) Output

func (c Component) Output() Output

Output returns the stream the component renders into, so a component built from another one shares it.

type Confirm

type Confirm struct{ Component }

Confirm asks a yes or no question.

func NewConfirm

func NewConfirm(output Output, base string) Confirm

NewConfirm returns the component.

func (Confirm) Render

func (c Confirm) Render(question string, def bool) (bool, error)

Render asks.

type Error

type Error struct{ Component }

Error renders a line about what went wrong.

func NewError

func NewError(output Output, base string) Error

NewError returns the component.

func (Error) Render

func (e Error) Render(message string, verbosity ...Verbosity)

Render writes the line.

type Factory

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

Factory is where a command reaches for a component.

Each method is declared rather than resolved from a name at run time, which is what makes a typo a compile error rather than a failure once the command runs.

func NewFactory

func NewFactory(output Output, base string) *Factory

NewFactory returns the factory for one output.

base is the application root that EnsureRelativePaths strips from a path, and empty means paths are printed as they came.

func (*Factory) Alert

func (f *Factory) Alert(message string, verbosity ...Verbosity)

Alert renders a message in a box, in capitals.

func (*Factory) Ask

func (f *Factory) Ask(question, def string) (string, error)

Ask puts a question and returns the answer.

func (*Factory) AskWithCompletion

func (f *Factory) AskWithCompletion(question string, choices []string, def string) (string, error)

AskWithCompletion is Ask with a list the terminal completes from.

func (*Factory) BulletList

func (f *Factory) BulletList(elements []string, verbosity ...Verbosity)

BulletList renders one line per element.

func (*Factory) Choice

func (f *Factory) Choice(question string, choices []string, def string) (string, error)

Choice offers a numbered list and returns the option that was picked.

func (*Factory) Confirm

func (f *Factory) Confirm(question string, def bool) (bool, error)

Confirm asks a yes or no question.

func (*Factory) Error

func (f *Factory) Error(message string, verbosity ...Verbosity)

Error renders a line about what went wrong.

func (*Factory) Info

func (f *Factory) Info(message string, verbosity ...Verbosity)

Info renders an informational line.

func (*Factory) Line

func (f *Factory) Line(style, message string, verbosity ...Verbosity)

Line renders a labelled sentence in one of the four styles: info, success, warn, error.

func (*Factory) Output

func (f *Factory) Output() Output

Output returns the stream the components render into.

func (*Factory) Secret

func (f *Factory) Secret(question string) (string, error)

Secret asks for a value the terminal must not show.

func (*Factory) Success

func (f *Factory) Success(message string, verbosity ...Verbosity)

Success renders a line that reports something worked.

func (*Factory) Task

func (f *Factory) Task(description string, task TaskFunc, verbosity ...Verbosity) error

Task runs the work and reports how it went, on one line.

func (*Factory) TwoColumnDetail

func (f *Factory) TwoColumnDetail(first, second string, verbosity ...Verbosity)

TwoColumnDetail renders a label and its value, joined by dots.

func (*Factory) Warn

func (f *Factory) Warn(message string, verbosity ...Verbosity)

Warn renders a line about something that is off but did not stop the command.

type Info

type Info struct{ Component }

Info renders an informational line.

func NewInfo

func NewInfo(output Output, base string) Info

NewInfo returns the component.

func (Info) Render

func (i Info) Render(message string, verbosity ...Verbosity)

Render writes the line.

type Line

type Line struct{ Component }

Line renders a labelled sentence: the chip, then the message.

It is what Info, Success, Warn and Error are each one call to.

func NewLineComponent

func NewLineComponent(output Output, base string) Line

NewLineComponent returns the component.

The constructor is not called NewLine because the package already has that name on Output, and a package with two NewLine of different shapes is a package where the completion list picks the wrong one.

func (Line) Render

func (l Line) Render(style, message string, verbosity ...Verbosity)

Render writes the labelled line.

The top margin is 2 minus the line endings already written, floored at zero: two labelled lines in a row get one blank line between them and never two.

An unknown style panics. The four names are a closed set the caller writes as a literal, so an unknown one is a mistake in the command rather than anything a person typed, and a panic is what Go spends on that.

type Mutator

type Mutator func(string) string

Mutator rewrites the text of a component before it is rendered.

func EnsureRelativePaths

func EnsureRelativePaths(base string) Mutator

EnsureRelativePaths strips the application root from every path in the text, curried over the root.

The root is given here rather than read from a global, because a component that reaches for a global to shorten a path is a component no test can pin. Base is set by the Factory.

type Output

type Output interface {
	// Write puts text out without a line ending.
	Write(message string, verbosity ...Verbosity)

	// Writeln puts one line out.
	Writeln(message string, verbosity ...Verbosity)

	// NewLine writes count blank lines, one when count is omitted.
	NewLine(count ...int)

	// NewLinesWritten is how many line endings the last write left behind. The
	// Line component reads it to decide its top margin.
	NewLinesWritten() int

	// GetTerminalWidth is how many columns there are to fill.
	GetTerminalWidth() int

	// Ask puts a question and returns the answer, or def when it is empty.
	Ask(question, def string) (string, error)

	// AskWithCompletion is Ask with a list the terminal completes from.
	AskWithCompletion(question string, choices []string, def string) (string, error)

	// Secret asks for a value the terminal must not show.
	Secret(question string) (string, error)

	// Confirm asks a yes or no question.
	Confirm(question string, def bool) (bool, error)

	// Choice offers a numbered list and returns the option that was picked.
	Choice(question string, choices []string, def string) (string, error)
}

Output is the terminal a component renders into.

It is the minimal set of methods a component actually calls, kept as an interface so this package does not import console, which imports this one: a component is the leaf, and the IO is what satisfies it.

type Secret

type Secret struct{ Component }

Secret asks for a value the terminal must not show.

A terminal that will not stop echoing is an error here rather than a choice between showing the value or not.

func NewSecret

func NewSecret(output Output, base string) Secret

NewSecret returns the component.

func (Secret) Render

func (s Secret) Render(question string) (string, error)

Render asks.

type Success

type Success struct{ Component }

Success renders a line that reports something worked.

func NewSuccess

func NewSuccess(output Output, base string) Success

NewSuccess returns the component.

func (Success) Render

func (s Success) Render(message string, verbosity ...Verbosity)

Render writes the line.

type Task

type Task struct{ Component }

Task renders a description, runs the work and reports how it went, on one line.

func NewTask

func NewTask(output Output, base string) Task

NewTask returns the component.

func (Task) Render

func (t Task) Render(description string, task TaskFunc, verbosity ...Verbosity) error

Render runs task and writes the line.

The line is written after task returns, whatever it returned.

type TaskFunc

type TaskFunc func() (view.TaskResult, error)

TaskFunc is the work a Task component runs.

A non-nil error makes the line read FAIL whatever the result says.

type TwoColumnDetail

type TwoColumnDetail struct{ Component }

TwoColumnDetail renders a label on the left and its value on the right, joined by dots.

func NewTwoColumnDetail

func NewTwoColumnDetail(output Output, base string) TwoColumnDetail

NewTwoColumnDetail returns the component.

func (TwoColumnDetail) Render

func (t TwoColumnDetail) Render(first, second string, verbosity ...Verbosity)

Render writes the line.

type Verbosity

type Verbosity int

Verbosity is how much a command is allowed to say.

It is declared here, in the leaf, rather than in console: a component may not import the package that builds it, and one set of constants in the package that cannot import is better than two sets that agree by hand. console aliases this type, so console.Verbosity and components.Verbosity are one type and not two.

const (
	// VerbosityQuiet writes nothing at all: -q.
	VerbosityQuiet Verbosity = 16
	// VerbosityNormal is the default.
	VerbosityNormal Verbosity = 32
	// VerbosityVerbose is -v.
	VerbosityVerbose Verbosity = 64
	// VerbosityVeryVerbose is -vv.
	VerbosityVeryVerbose Verbosity = 128
	// VerbosityDebug is -vvv.
	VerbosityDebug Verbosity = 256
)

type Warn

type Warn struct{ Component }

Warn renders a line about something that is off but did not stop the command.

func NewWarn

func NewWarn(output Output, base string) Warn

NewWarn returns the component.

func (Warn) Render

func (w Warn) Render(message string, verbosity ...Verbosity)

Render writes the line.

Jump to

Keyboard shortcuts

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