Documentation
¶
Overview ¶
Package progress provides a generic progress tracking system for long-running operations. It receives events from a source, maps them to a common format, and displays them via a Visualizer.
Usage:
tracker := progress.NewTracker(
progress.WithEvents(eventsChan, mapFunc),
progress.WithVisualizer(bar.NewBarVisualizer()),
progress.WithTotal(10),
)
go tracker.Start(ctx)
// ... send events to eventsChan ...
tracker.Summary(nil)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsTerminal ¶
IsTerminal reports whether the writer is connected to a terminal.
Types ¶
type Event ¶
Event holds progress data for a single tracked item.
- ID: unique identifier for the item
- Data: custom payload (e.g., transformation details)
- State: current lifecycle state
- Err: error if the item failed
type LogBufferAware ¶
LogBufferAware is an optional interface for visualizers that can display buffered log output. If a Visualizer implements this, the Tracker will pass the slog buffer so the visualizer can render log lines inline (e.g., above the progress bar).
type Tracker ¶
type Tracker[T, E any] struct { // contains filtered or unexported fields }
Tracker connects an event source to a visualizer. It reads events from a channel, maps them to Events, and forwards to the visualizer.
Type parameters:
- T: the data type stored in Event.Data
- E: the raw event type from the source channel
func NewTracker ¶
func NewTracker[T, E any](opts ...TrackerOption[T, E]) *Tracker[T, E]
NewTracker creates a Tracker configured with the given options.
type TrackerOption ¶
TrackerOption configures a Tracker using the functional options pattern.
func WithEvents ¶
func WithEvents[T, E any](events <-chan E, f func(E) Event[T]) TrackerOption[T, E]
WithEvents sets the event source channel and mapper function. The mapper converts raw events (E) to progress Events (T).
func WithOutput ¶
func WithOutput[T, E any](w io.Writer) TrackerOption[T, E]
WithOutput sets where the visualizer writes output (e.g., os.Stdout).
func WithTotal ¶
func WithTotal[T, E any](total int) TrackerOption[T, E]
WithTotal sets the expected number of items for progress percentage calculation.
func WithVisualizer ¶
func WithVisualizer[T, E any](factory VisualizerFactory[T]) TrackerOption[T, E]
WithVisualizer sets the factory that creates the visualizer. The visualizer handles all rendering of progress updates.
type Visualizer ¶
type Visualizer[T any] interface { // HandleEvent is called for each progress update. HandleEvent(event Event[T]) // Summary is called once after all events are processed. Summary(err error) }
Visualizer displays progress events to the user. Implementations handle rendering (e.g., progress bar, simple logs).
type VisualizerFactory ¶
type VisualizerFactory[T any] func(out io.Writer, total int) Visualizer[T]
VisualizerFactory creates a Visualizer with the output writer and total item count.