Documentation
¶
Index ¶
- func Background() context.Context
- func Begin(ctx context.Context, subject, message string) (context.Context, func(error))
- func FormatDuration(d time.Duration) string
- func GuardInterrupts() func()
- func HandOff() func()
- func InterruptsGuarded() bool
- func Message(ctx context.Context, text string)
- func NewContext(ctx context.Context, p Presenter) context.Context
- func Progress(ctx context.Context, text string)
- func SetDefault(p Presenter)
- func Track(ctx context.Context, subject, message string, fn func(context.Context) error) error
- func Warn(ctx context.Context, text string)
- type Console
- type ConsoleOptions
- type Event
- type Group
- type JSON
- type Kind
- type LogHandler
- type Presenter
- type Terminal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Background ¶
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 ¶
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 ¶
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 NewContext ¶
NewContext returns a copy of ctx that carries p.
func Progress ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Done ends the named child as applied, starting it first when it has not started.
func (*Group) Fail ¶
Fail ends the named child as failed with err, starting it first when it has not started.
type JSON ¶
type JSON struct {
// contains filtered or unexported fields
}
JSON renders events as newline-delimited JSON objects.
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) Handle ¶
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.
type Presenter ¶
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 ¶
FromContext returns the presenter in ctx, or Discard when ctx has none.