github.com/vmware/govmomi@v0.51.0/vim25/progress/prefix.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package progress
     6  
     7  import "fmt"
     8  
     9  type prefixedReport struct {
    10  	Report
    11  	prefix string
    12  }
    13  
    14  func (r prefixedReport) Detail() string {
    15  	if d := r.Report.Detail(); d != "" {
    16  		return fmt.Sprintf("%s: %s", r.prefix, d)
    17  	}
    18  
    19  	return r.prefix
    20  }
    21  
    22  func prefixLoop(upstream <-chan Report, downstream chan<- Report, prefix string) {
    23  	defer close(downstream)
    24  
    25  	for r := range upstream {
    26  		downstream <- prefixedReport{
    27  			Report: r,
    28  			prefix: prefix,
    29  		}
    30  	}
    31  }
    32  
    33  func Prefix(s Sinker, prefix string) Sinker {
    34  	fn := func() chan<- Report {
    35  		upstream := make(chan Report)
    36  		downstream := s.Sink()
    37  		go prefixLoop(upstream, downstream, prefix)
    38  		return upstream
    39  	}
    40  
    41  	return SinkFunc(fn)
    42  }