presenter

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MPL-2.0 Imports: 17 Imported by: 0 modules, 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Background

func Background() context.Context

Background returns a background context that carries the default presenter. It is for code that has no context of its own yet; code with a context uses that context instead.

func Begin

func Begin(ctx context.Context, subject, message string) (context.Context, func(error))

Begin emits KindApplying for a new operation under the operation in ctx. It returns a context that carries the new operation and an end function that emits KindApplied for a nil error or KindFailed otherwise, with the elapsed time. Calls to end after the first do nothing.

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration returns d in whole seconds below one minute ("14s"), in minutes and seconds below one hour ("2m05s"), and in hours and minutes from one hour ("1h02m").

func GuardInterrupts

func GuardInterrupts() func()

GuardInterrupts marks that a caller handles interrupt signals itself until the returned release function runs. Release runs once.

func HandOff

func HandOff() func()

HandOff pauses the default presenter so another program can use the terminal, and returns a function that resumes it. It does nothing for a presenter that cannot pause. Call the returned function once the other program is done.

func InterruptsGuarded

func InterruptsGuarded() bool

InterruptsGuarded reports whether an interrupt guard is open.

func Message

func Message(ctx context.Context, text string)

Message emits an info-level narrative line under the operation in ctx.

func NewContext

func NewContext(ctx context.Context, p Presenter) context.Context

NewContext returns a copy of ctx that carries p.

func Progress

func Progress(ctx context.Context, text string)

Progress replaces the status text of the operation in ctx. It does nothing outside an operation.

func SetDefault

func SetDefault(p Presenter)

SetDefault makes p the presenter that owns the terminal for this process. Nil clears it.

func Track

func Track(ctx context.Context, subject, message string, fn func(context.Context) error) error

Track runs fn as an operation that Begin starts and ends. It passes fn the context of the new operation and returns the error from fn unchanged.

func Warn

func Warn(ctx context.Context, text string)

Warn emits a warn-level narrative line under the operation in ctx.

Types

type Console

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

Console renders events as an indented tree of lines, or as logfmt lines in plain mode.

func NewConsole

func NewConsole(w io.Writer, opts ConsoleOptions) *Console

NewConsole returns a Console that writes to w.

func (*Console) Close

func (c *Console) Close()

Close stops the animation and leaves the live display mode for the rest of the run. The console keeps rendering events afterward, with the cursor and input echo left on.

func (*Console) Emit

func (c *Console) Emit(ctx context.Context, event Event)

Emit renders event. In plain mode it writes one logfmt line, as emitPlain describes. In interactive mode an operation header prints when its first child appears, and end lines show the elapsed time. Events that refer to an unknown operation render at the top level.

func (*Console) Pause

func (c *Console) Pause()

Pause erases the live region and stops the animation until a matching Resume. Calls nest.

func (*Console) Resume

func (c *Console) Resume()

Resume redraws the live region and restarts the animation when the last Pause ends.

func (*Console) WatchInterrupts

func (c *Console) WatchInterrupts(guarded func() bool) func()

WatchInterrupts restores the terminal when the process receives an interrupt or termination signal. After the restore it stops watching and re-raises the signal, unless guarded reports that another handler owns it. The returned function stops the watch.

type ConsoleOptions

type ConsoleOptions struct {
	// Level is the lowest log level that the console writes.
	Level slog.Level
	// Interactive enables the animated live region and colors.
	Interactive bool
	// Width returns the terminal width for line truncation, or 0 for no truncation.
	Width func() int
	// Height returns the terminal height that caps the live region, or 0 for the default cap.
	Height func() int
	// Terminal enters the live display mode while the live region is on screen. Nil skips it.
	Terminal Terminal
	// Now returns the current time for elapsed counters. Nil uses time.Now.
	Now func() time.Time
}

ConsoleOptions configures a Console.

type Event

type Event struct {
	Kind     Kind
	ID       string
	ParentID string
	Subject  string
	Message  string
	Level    slog.Level
	Duration time.Duration
	Err      error
	Record   *slog.Record
}

Event is one entry in the output stream. Kind selects which optional fields are set.

type Group

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

Group tracks one child operation per name under the operation in a context.

func NewGroup

func NewGroup(ctx context.Context, prefix string) *Group

NewGroup returns an empty group whose children start under the operation in ctx. Each child's subject is prefix followed by its name, and its message is the name.

func (*Group) Close

func (g *Group) Close(err error)

Close ends every started child still open with err, in start order, so a nil err ends them as applied. Children that never started stay hidden.

func (*Group) Done

func (g *Group) Done(name string)

Done ends the named child as applied, starting it first when it has not started.

func (*Group) Fail

func (g *Group) Fail(name string, err error)

Fail ends the named child as failed with err, starting it first when it has not started.

func (*Group) Progress

func (g *Group) Progress(name, text string)

Progress replaces the status text of the named child. For a child that has not started, it holds the text until Start. It does nothing for a child that has ended.

func (*Group) Start

func (g *Group) Start(name string)

Start begins the named child and shows any progress held for it. It does nothing for a name that has already started or ended.

type JSON

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

JSON renders events as newline-delimited JSON objects.

func NewJSON

func NewJSON(w io.Writer) *JSON

NewJSON returns a JSON presenter that writes to w.

func (*JSON) Emit

func (j *JSON) Emit(_ context.Context, event Event)

Emit writes event as one JSON line. Messages add their level. A log record supplies the time, level, message, and attrs.

type Kind

type Kind string

Kind names the type of an Event.

const (
	// KindApplying marks the start of an operation.
	KindApplying Kind = "applying"
	// KindApplied marks the successful end of an operation.
	KindApplied Kind = "applied"
	// KindFailed marks the failed end of an operation.
	KindFailed Kind = "failed"
	// KindProgress replaces the status text of a running operation.
	KindProgress Kind = "progress"
	// KindMessage is a narrative line for the user, at info or warn level.
	KindMessage Kind = "message"
	// KindLog carries a structured log record.
	KindLog Kind = "log"
	// KindInputNeeded is reserved for interactive input requests.
	KindInputNeeded Kind = "input_needed"
)

type LogHandler

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

LogHandler emits slog records as KindLog events.

func NewLogHandler

func NewLogHandler(p Presenter, level slog.Leveler) *LogHandler

NewLogHandler returns a handler that emits records at level or above to p.

func (*LogHandler) Enabled

func (h *LogHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether level is at or above the handler level.

func (*LogHandler) Handle

func (h *LogHandler) Handle(ctx context.Context, r slog.Record) error

Handle emits a copy of r that holds the handler attrs and the record attrs under open groups. The event parent is the operation in ctx.

func (*LogHandler) WithAttrs

func (h *LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a handler that adds attrs, nested under the open groups, to every record.

func (*LogHandler) WithGroup

func (h *LogHandler) WithGroup(name string) slog.Handler

WithGroup returns a handler that nests later attrs under name. An empty name is ignored.

type Presenter

type Presenter interface {
	Emit(ctx context.Context, event Event)
}

Presenter receives every event that business code emits.

var Discard Presenter = discard{}

Discard is a Presenter that drops every event. FromContext returns it when ctx has none.

func Default

func Default() Presenter

Default returns the presenter that owns the terminal, or Discard when none is set.

func FromContext

func FromContext(ctx context.Context) Presenter

FromContext returns the presenter in ctx, or Discard when ctx has none.

func New

func New(format logging.Format, w io.Writer, opts ConsoleOptions) Presenter

New returns the Presenter for format that writes to w. For text output, opts configures the console. Interactive stays on only when w is a terminal that accepts ANSI codes, and then the console hides the cursor and stdin echo while it animates. JSON output ignores opts.

type Terminal

type Terminal interface {
	Enter()
	Leave()
}

Terminal enters and leaves the live display mode.

Jump to

Keyboard shortcuts

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