github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/spinner/spinner.go (about)

     1  package spinner
     2  
     3  import "context"
     4  
     5  type Spinner interface {
     6  	Helper()
     7  
     8  	// Start spinning the spinner.
     9  	Start()
    10  
    11  	// Done stops the spinner and prints a success icon.
    12  	Done()
    13  
    14  	// DoneMsg stops the spinner and sets the success icon with a new message.
    15  	DoneMsg(msg string)
    16  
    17  	// Error prints the error to the spinner, stops the spinner, then returns the error
    18  	// so that you can continue passing it if needed.
    19  	Error(err error) error
    20  
    21  	// Message writes a message to the current spinner displayed alongside the initial job message.
    22  	Message(msg string)
    23  }
    24  
    25  type Provider interface {
    26  	New(string) Spinner
    27  }
    28  
    29  type key struct{}
    30  
    31  func WithProvider(ctx context.Context, sp Provider) context.Context {
    32  	return context.WithValue(ctx, key{}, sp)
    33  }
    34  
    35  // New configures a new spinner with the default values displaying the job message.
    36  func New(ctx context.Context, job string) Spinner {
    37  	sp, ok := ctx.Value(key{}).(Provider)
    38  	if !ok {
    39  		return noop{}
    40  	}
    41  	spin := sp.New(job)
    42  	spin.Helper()
    43  	spin.Start()
    44  	return spin
    45  }
    46  
    47  type noop struct{}
    48  
    49  func (n noop) Helper() {
    50  }
    51  
    52  func (n noop) Start() {
    53  }
    54  
    55  func (n noop) Done() {
    56  }
    57  
    58  func (n noop) DoneMsg(string) {
    59  }
    60  
    61  func (n noop) Error(error) error {
    62  	return nil
    63  }
    64  
    65  func (n noop) Message(string) {
    66  }