Documentation
¶
Overview ¶
Package fx provides terminal animation types (spinners, progress bars, shimmers, and pulses) with composable builders and group orchestration.
Index ¶
- Constants
- type Animation
- type AnimationConfig
- type Builder
- func (b *Builder) After(d time.Duration) *Builder
- func (b *Builder) BarPercent(key string) *Builder
- func (b *Builder) BarPercentValue() core.Percent
- func (b *Builder) Column(key, path string, line, column int) *Builder
- func (b *Builder) Dedent() *Builder
- func (b *Builder) Depth(n int) *Builder
- func (b *Builder) Elapsed(key string) *Builder
- func (b *Builder) Indent() *Builder
- func (b *Builder) IndentedLogger(def Logger) Logger
- func (b *Builder) Line(key, path string, line int) *Builder
- func (b *Builder) Link(key, url, text string) *Builder
- func (b *Builder) NonTTYSilent(silent bool) *Builder
- func (b *Builder) Parts(parts ...core.Part) *Builder
- func (b *Builder) Path(key, path string) *Builder
- func (b *Builder) Progress(ctx context.Context, task UpdateFunc) *WaitResult
- func (b *Builder) ResolveDynamicFields(fields []core.Field, dur time.Duration) []core.Field
- func (b *Builder) ResolveLogger(def Logger) Logger
- func (b *Builder) Spinner(opts ...spinner.Option) *Builder
- func (b *Builder) StripDynamicFields(fields []core.Field) []core.Field
- func (b *Builder) Symbol(symbol string) *Builder
- func (b *Builder) Tree(pos core.TreePos) *Builder
- func (b *Builder) URL(key, url string) *Builder
- func (b *Builder) Wait(ctx context.Context, task TaskFunc) *WaitResult
- type BuilderConfig
- type DoneEvent
- type FieldAlignment
- type Group
- type GroupEntry
- type GroupResult
- func (r *GroupResult) Err() error
- func (r *GroupResult) Msg(msg string) error
- func (b *GroupResult) OnErrorLevel(lvl core.Level) *T
- func (b *GroupResult) OnErrorMessage(msg string) *T
- func (b *GroupResult) OnSuccessLevel(lvl core.Level) *T
- func (b *GroupResult) OnSuccessMessage(msg string) *T
- func (b *GroupResult) Parts(parts ...core.Part) *T
- func (r *GroupResult) Send() error
- func (r *GroupResult) Silent() error
- func (b *GroupResult) Symbol(symbol string) *T
- type GroupStatus
- type GroupStatusFunc
- type GroupTask
- type Logger
- type Output
- type TaskFunc
- type TaskResult
- func (r *TaskResult) Err() error
- func (r *TaskResult) Msg(msg string) error
- func (b *TaskResult) OnErrorLevel(lvl core.Level) *T
- func (b *TaskResult) OnErrorMessage(msg string) *T
- func (b *TaskResult) OnSuccessLevel(lvl core.Level) *T
- func (b *TaskResult) OnSuccessMessage(msg string) *T
- func (b *TaskResult) Parts(parts ...core.Part) *T
- func (r *TaskResult) Send() error
- func (r *TaskResult) Silent() error
- func (b *TaskResult) Symbol(symbol string) *T
- type Update
- func (p *Update) AddTotal(delta int) *Update
- func (p *Update) Msg(msg string) *Update
- func (p *Update) Msgf(format string, args ...any) *Update
- func (p *Update) Send()
- func (p *Update) SetLevel(level core.Level) *Update
- func (p *Update) SetProgress(current int) *Update
- func (p *Update) SetSymbol(symbol string) *Update
- func (p *Update) SetTotal(total int) *Update
- type UpdateFunc
- type WaitResult
- func (w *WaitResult) Err() error
- func (w *WaitResult) Msg(msg string) error
- func (w *WaitResult) Msgf(format string, a ...any) error
- func (b *WaitResult) OnErrorLevel(lvl core.Level) *T
- func (b *WaitResult) OnErrorMessage(msg string) *T
- func (b *WaitResult) OnSuccessLevel(lvl core.Level) *T
- func (b *WaitResult) OnSuccessMessage(msg string) *T
- func (b *WaitResult) Parts(parts ...core.Part) *T
- func (w *WaitResult) Send() error
- func (w *WaitResult) Silent() error
- func (b *WaitResult) Symbol(symbol string) *T
Constants ¶
const DefaultSymbol = "⏳"
DefaultSymbol is the default icon shown during pulse, shimmer, and bar animations.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Animation ¶
type Animation int
Animation is the animation rendering mode for a Builder. It controls how the message text is animated. Symbol animation is controlled independently by Builder.AnimatedSymbol.
type AnimationConfig ¶
type AnimationConfig struct {
Builder *Builder
FieldsPtr *atomic.Pointer[[]core.Field]
LevelPtr *atomic.Int64
MsgPtr *atomic.Pointer[string]
StartTime time.Time
SymbolPtr *atomic.Pointer[string]
Task TaskFunc
}
AnimationConfig holds the parameters for running a single animation.
type Builder ¶
type Builder struct {
core.FieldBuilder[Builder]
AnimatedSymbol bool // when true, cycles SpinnerStyle.Frames as the symbol instead of a static icon
ClearOnCancel bool // when true, erase the animation line on context cancellation
BarPercentKey string // when set, a formatted percent field is injected each tick
BarProgressPtr *atomic.Int64 // bar mode: current progress; nil for non-bar modes
BarStyle bar.Style // bar mode: visual style
BarTotalPtr *atomic.Int64 // bar mode: total progress; nil for non-bar modes
DelayDur time.Duration // when set, suppresses animation until this duration elapses
ElapsedKey string // when set, a formatted elapsed-time field is injected each tick
IndentDepth int // additional indent depth applied to the animation
Log Logger // the logger interface; nil uses Default
Level core.Level // log level used during animation rendering (default: LevelInfo)
Message string
Mode Animation
PartOverrides *[]core.Part // nil = use logger's parts
PulseStops []gradient.ColorStop
ShimmerDir shimmer.Direction
ShimmerStops []gradient.ColorStop
Speed float64
SpinnerStyle spinner.Style
SuppressNonTTY bool // when true, no output is produced on non-TTY writers
SymbolIcon string // icon shown during animation; defaults to DefaultSymbol for pulse/shimmer/bar
TreePos []core.TreePos // additional tree levels applied to the animation
}
Builder configures an animation before execution. Create one with NewBuilder or the root clog convenience constructors (Spinner, Pulse, Shimmer, Bar).
func NewBuilder ¶
func NewBuilder(cfg BuilderConfig) *Builder
NewBuilder creates a new Builder from the given configuration.
func (*Builder) BarPercent ¶
BarPercent enables an auto-updating percentage field for AnimationBar.
func (*Builder) BarPercentValue ¶
BarPercentValue returns the current progress as a core.Percent.
func (*Builder) Column ¶
Column adds a file path field with a line and column number as a clickable terminal hyperlink.
func (*Builder) Dedent ¶
Dedent removes one indent level from the animation output, down to a minimum of zero.
func (*Builder) IndentedLogger ¶
IndentedLogger returns the builder's logger with any builder-level indent depth and tree levels applied. Used for completion logging so the result message has the same indentation as the animation.
func (*Builder) Line ¶
Line adds a file path field with a line number as a clickable terminal hyperlink.
func (*Builder) Link ¶
Link adds a field as a clickable terminal hyperlink with custom display text.
func (*Builder) NonTTYSilent ¶
NonTTYSilent controls whether animation output is suppressed entirely on non-TTY writers (CI, piped output). When silent is true, no output is produced. When false (default), a static line is printed.
func (*Builder) Parts ¶
Parts overrides the log-line part order for this animation and its completion message.
func (*Builder) Progress ¶
func (b *Builder) Progress(ctx context.Context, task UpdateFunc) *WaitResult
Progress executes the task with the animation whose message and fields can be updated via the Update builder.
func (*Builder) ResolveDynamicFields ¶
ResolveDynamicFields clones fields and injects elapsed/percent values.
func (*Builder) ResolveLogger ¶
ResolveLogger returns the builder's logger, falling back to the given default.
func (*Builder) Spinner ¶ added in v0.7.20
Spinner enables an animated spinning symbol. The symbol slot cycles through [SpinnerStyle] frames independently of the main animation mode. Options override the builder's existing [SpinnerStyle]. With no options the current style (set by the constructor or logger default) is used.
func (*Builder) StripDynamicFields ¶
StripDynamicFields returns fields with animation-only dynamic fields removed.
func (*Builder) Wait ¶
func (b *Builder) Wait(ctx context.Context, task TaskFunc) *WaitResult
Wait executes the task with the animation and returns a WaitResult for chaining.
type BuilderConfig ¶
type BuilderConfig struct {
AnimatedSymbol bool
BarProgress *atomic.Int64
BarStyle bar.Style
BarTotal *atomic.Int64
Level core.Level
Logger Logger
Message string
Mode Animation
PulseStops []gradient.ColorStop
ShimmerDir shimmer.Direction
ShimmerStops []gradient.ColorStop
Speed float64
SpinnerStyle spinner.Style
SymbolIcon string
}
BuilderConfig provides the initial configuration for a Builder.
type DoneEvent ¶
type DoneEvent struct {
Err error
Fields []core.Field
Level core.Level
Msg string
Parts *[]core.Part
Symbol *string
}
DoneEvent holds the parameters for logging a done event.
type FieldAlignment ¶ added in v0.7.0
type FieldAlignment int
FieldAlignment controls optional group-level alignment behavior.
const ( // FieldAlignmentNone disables group-level field alignment padding. FieldAlignmentNone FieldAlignment = iota // FieldAlignmentMessage aligns the first field column after the message. FieldAlignmentMessage )
type Group ¶
type Group struct {
Ctx context.Context //nolint:containedctx // Group shares a single ctx with all child goroutines
Mu sync.Mutex
ClearOnCancel bool
FieldAlignment FieldAlignment
Header *GroupStatus
HideDone bool
Log Logger
MaxLines int
MaxHeightPercent float64
Monotonic bool
Parallelism int
SyncAnimations bool
Tasks []*GroupTask
// contains filtered or unexported fields
}
Group manages a set of concurrent animations rendered as a multi-line block. Create one with root clog's Group function, add animations with Group.Add, then call Group.Wait to run the render loop.
func (*Group) Add ¶
func (g *Group) Add(b *Builder) *GroupEntry
Add registers an animation builder with the group and returns a GroupEntry for starting the task.
func (*Group) Wait ¶
func (g *Group) Wait() *GroupResult
Wait runs the render loop, blocking until all tasks complete or the context is cancelled. The returned GroupResult can be used to log a single summary line.
type GroupEntry ¶
type GroupEntry struct {
// contains filtered or unexported fields
}
GroupEntry is returned by Group.Add and provides [Run] and [Progress] methods to start a task within the group.
func (*GroupEntry) Progress ¶
func (ge *GroupEntry) Progress(task UpdateFunc) *TaskResult
Progress starts a task with progress update capability and returns a TaskResult.
func (*GroupEntry) Run ¶
func (ge *GroupEntry) Run(task TaskFunc) *TaskResult
Run starts a simple task (no progress updates) and returns a TaskResult.
type GroupResult ¶
type GroupResult struct {
// contains filtered or unexported fields
}
GroupResult holds the aggregate result of a Group.Wait.
func (*GroupResult) Err ¶
func (r *GroupResult) Err() error
Err returns the joined error, logging success or failure.
func (*GroupResult) Msg ¶
func (r *GroupResult) Msg(msg string) error
Msg logs at success level with the given message if all tasks succeeded.
func (*GroupResult) OnErrorLevel ¶
OnErrorLevel sets the log level for the error case.
func (*GroupResult) OnErrorMessage ¶
func (b *GroupResult) OnErrorMessage(msg string) *T
OnErrorMessage sets a custom message for the error case.
func (*GroupResult) OnSuccessLevel ¶
OnSuccessLevel sets the log level for the success case.
func (*GroupResult) OnSuccessMessage ¶
func (b *GroupResult) OnSuccessMessage(msg string) *T
OnSuccessMessage sets the message for the success case.
func (*GroupResult) Silent ¶
func (r *GroupResult) Silent() error
Silent returns the joined error without logging anything.
type GroupStatus ¶ added in v0.7.25
type GroupStatus struct {
Builder *Builder
Callback GroupStatusFunc
}
GroupStatus pairs a Builder (for initial config like level, symbol, parts) with a GroupStatusFunc callback that updates it each tick.
type GroupStatusFunc ¶ added in v0.7.25
GroupStatusFunc is called each render tick with the number of completed and total tasks. Use the Update to set the message and fields for that tick.
type GroupTask ¶
type GroupTask struct {
Builder *Builder
DoneErr chan error // buffered(1); goroutine sends result here
Err error // populated by Wait() after DoneErr is drained
FieldsPtr *atomic.Pointer[[]core.Field]
FinishedAt atomic.Int64
LevelPtr *atomic.Int64
MsgPtr *atomic.Pointer[string]
StartTime time.Time
StartedAt atomic.Int64
SymbolOverride atomic.Bool // true when SetSymbol has been called; disables animated spinner
SymbolPtr *atomic.Pointer[string]
}
GroupTask holds per-animation state for the group render loop. This is exported so the root clog rendering code can access it.
func (*GroupTask) Duration ¶ added in v0.7.0
Duration returns the elapsed execution time, or zero while the task is queued.
func (*GroupTask) FinishTime ¶ added in v0.7.2
FinishTime returns the actual finish time, or the zero value if unfinished.
func (*GroupTask) MarkFinished ¶ added in v0.7.2
MarkFinished records the actual task finish time.
func (*GroupTask) MarkStarted ¶ added in v0.7.0
MarkStarted records the actual task start time.
type Logger ¶
type Logger interface {
// Done logs a done event after an animation finishes.
Done(evt DoneEvent)
// WithIndent returns a Logger with additional indent depth and tree positions.
WithIndent(depth int, tree []core.TreePos) Logger
// Output returns the Output associated with this Logger.
Output() Output
// RunAnimation runs the animation loop for a single builder.
RunAnimation(ctx context.Context, cfg AnimationConfig) error
// RunGroup runs the group render loop.
RunGroup(ctx context.Context, g *Group) error
}
Logger is the interface that the root clog package implements. It decouples the fx animation types from the concrete *clog.Logger so the fx package can reference logging operations without importing root clog.
type Output ¶
type Output interface {
IsTTY() bool
Writer() io.Writer
Width() int
ColorsDisabled() bool
PathLink(path string, line, column int) string
Hyperlink(url, text string) string
}
Output provides terminal capabilities needed by animation types.
type TaskFunc ¶
TaskFunc is a function executed by Builder.Wait.
type TaskResult ¶
type TaskResult struct {
// contains filtered or unexported fields
}
TaskResult holds the result of a group animation task.
func (*TaskResult) Err ¶
func (r *TaskResult) Err() error
Err returns the error, logging success or failure using the original message.
func (*TaskResult) Msg ¶
func (r *TaskResult) Msg(msg string) error
Msg logs at success level with the given message on success, or at error level with the error string on failure. Returns the error.
func (*TaskResult) OnErrorLevel ¶
OnErrorLevel sets the log level for the error case.
func (*TaskResult) OnErrorMessage ¶
func (b *TaskResult) OnErrorMessage(msg string) *T
OnErrorMessage sets a custom message for the error case.
func (*TaskResult) OnSuccessLevel ¶
OnSuccessLevel sets the log level for the success case.
func (*TaskResult) OnSuccessMessage ¶
func (b *TaskResult) OnSuccessMessage(msg string) *T
OnSuccessMessage sets the message for the success case.
func (*TaskResult) Send ¶
func (r *TaskResult) Send() error
Send finalises the result, logging at the configured success or error level.
func (*TaskResult) Silent ¶
func (r *TaskResult) Silent() error
Silent returns just the error without logging anything.
type Update ¶
type Update struct {
core.FieldBuilder[Update]
Base []core.Field
FieldsPtr *atomic.Pointer[[]core.Field]
MsgPtr *atomic.Pointer[string]
MsgText string
ProgressPtr *atomic.Int64 // bar mode: current progress value; nil for non-bar modes
LevelPtr *atomic.Int64 // overridden level; nil when not updatable
SymbolOverridePtr *atomic.Bool // when set to true, disables animated spinner in favour of static symbol
SymbolPtr *atomic.Pointer[string] // symbol icon; nil when not updatable
TotalPtr *atomic.Int64 // bar mode: total progress value; nil for non-bar modes
}
Update is a fluent builder for updating an animation's message and fields during an UpdateFunc. Call Update.Msg and field methods to build the update, then Update.Send to apply it atomically.
func (*Update) AddTotal ¶
AddTotal atomically adds delta to the total progress value for a bar animation. The result is clamped to a minimum of 1. No-op if this is not a bar animation.
func (*Update) Send ¶
func (p *Update) Send()
Send applies the accumulated message and field changes to the animation atomically.
func (*Update) SetLevel ¶ added in v0.7.7
SetLevel overrides the log level used when rendering the final done line. No-op if the animation does not support level updates.
func (*Update) SetProgress ¶
SetProgress sets the current progress value for a bar animation. Values are clamped to [0, total]. No-op if this is not a bar animation.
type UpdateFunc ¶
UpdateFunc is a function executed by Builder.Progress. The Update allows updating the animation's message and fields.
type WaitResult ¶
type WaitResult struct {
TaskErr error
// contains filtered or unexported fields
}
WaitResult holds the result of a Builder.Wait or Builder.Progress operation and allows chaining additional fields before finalising the log output.
func NewWaitResult ¶
func NewWaitResult( err error, logger Logger, parts *[]core.Part, lvl core.Level, msg string, ) *WaitResult
NewWaitResult creates a WaitResult with default settings.
func (*WaitResult) Err ¶
func (w *WaitResult) Err() error
Err returns the error, logging success at info level or failure at error level using the original animation message.
func (*WaitResult) Msg ¶
func (w *WaitResult) Msg(msg string) error
Msg logs at info level with the given message on success, or at error level with the error string on failure. Returns the error.
func (*WaitResult) Msgf ¶ added in v0.7.12
func (w *WaitResult) Msgf(format string, a ...any) error
Msgf is like WaitResult.Msg but accepts a format string.
func (*WaitResult) OnErrorLevel ¶
OnErrorLevel sets the log level for the error case.
func (*WaitResult) OnErrorMessage ¶
func (b *WaitResult) OnErrorMessage(msg string) *T
OnErrorMessage sets a custom message for the error case.
func (*WaitResult) OnSuccessLevel ¶
OnSuccessLevel sets the log level for the success case.
func (*WaitResult) OnSuccessMessage ¶
func (b *WaitResult) OnSuccessMessage(msg string) *T
OnSuccessMessage sets the message for the success case.
func (*WaitResult) Send ¶
func (w *WaitResult) Send() error
Send finalises the result, logging at the configured success or error level. Context cancellation errors are silenced since they indicate an interrupt, not a task failure. Returns the error from the task.
func (*WaitResult) Silent ¶
func (w *WaitResult) Silent() error
Silent returns just the error without logging anything.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bar provides bar animation styles and presets for clog.
|
Package bar provides bar animation styles and presets for clog. |
|
widget
Package widget provides composable text widgets for progress bars.
|
Package widget provides composable text widgets for progress bars. |
|
Package pulse provides pulse animation rendering for clog.
|
Package pulse provides pulse animation rendering for clog. |
|
Package shimmer provides shimmer animation rendering for clog.
|
Package shimmer provides shimmer animation rendering for clog. |
|
Package spinner provides spinner animation styles and presets for clog.
|
Package spinner provides spinner animation styles and presets for clog. |