Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
116 lines
2.3 KiB
Go
116 lines
2.3 KiB
Go
// Package events provides functionalities for packages to log their states as events
|
|
// for others to consume and display to end users in meaningful ways.
|
|
package events
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/muesli/reflow/indent"
|
|
)
|
|
|
|
// ProgressIndication enumerates possible states of progress indication for an Event.
|
|
type ProgressIndication uint8
|
|
|
|
const (
|
|
GroupError = "error"
|
|
)
|
|
|
|
const (
|
|
IndicationNone ProgressIndication = iota
|
|
IndicationStart
|
|
IndicationUpdate
|
|
IndicationFinish
|
|
)
|
|
|
|
type (
|
|
// Event represents a state.
|
|
Event struct {
|
|
ProgressIndication ProgressIndication
|
|
Icon string
|
|
Indent uint
|
|
Message string
|
|
Verbose bool
|
|
Group string
|
|
}
|
|
|
|
// Option event options.
|
|
Option func(*Event)
|
|
)
|
|
|
|
// ProgressStart indicates that a status event starts the progress indicator.
|
|
func ProgressStart() Option {
|
|
return func(e *Event) {
|
|
e.ProgressIndication = IndicationStart
|
|
}
|
|
}
|
|
|
|
// ProgressUpdate indicates that a status event updated the current progress.
|
|
func ProgressUpdate() Option {
|
|
return func(e *Event) {
|
|
e.ProgressIndication = IndicationUpdate
|
|
}
|
|
}
|
|
|
|
// ProgressFinish indicates that a status event finished the ongoing task.
|
|
func ProgressFinish() Option {
|
|
return func(e *Event) {
|
|
e.ProgressIndication = IndicationFinish
|
|
}
|
|
}
|
|
|
|
// Verbose sets high verbosity for the Event.
|
|
func Verbose() Option {
|
|
return func(e *Event) {
|
|
e.Verbose = true
|
|
}
|
|
}
|
|
|
|
// Icon sets the text icon prefix.
|
|
func Icon(icon string) Option {
|
|
return func(e *Event) {
|
|
e.Icon = icon
|
|
}
|
|
}
|
|
|
|
// Indent sets the text indentation.
|
|
func Indent(indent uint) Option {
|
|
return func(e *Event) {
|
|
e.Indent = indent
|
|
}
|
|
}
|
|
|
|
// Group sets a group name for the event.
|
|
func Group(name string) Option {
|
|
return func(e *Event) {
|
|
e.Group = name
|
|
}
|
|
}
|
|
|
|
// New creates a new event with given config.
|
|
func New(message string, options ...Option) Event {
|
|
ev := Event{Message: message}
|
|
|
|
for _, applyOption := range options {
|
|
applyOption(&ev)
|
|
}
|
|
|
|
return ev
|
|
}
|
|
|
|
func (e Event) String() string {
|
|
s := e.Message
|
|
if e.Icon != "" {
|
|
s = fmt.Sprintf("%s %s", e.Icon, s)
|
|
}
|
|
|
|
if e.Indent > 0 {
|
|
s = indent.String(s, e.Indent)
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
// InProgress returns true when the event is in progress.
|
|
func (e Event) InProgress() bool {
|
|
return e.ProgressIndication == IndicationStart || e.ProgressIndication == IndicationUpdate
|
|
}
|