github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/pkg/progress/progress.go (about)

     1  package progress
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Progress represents the progress of a transfer.
     8  type Progress struct {
     9  	ID string
    10  
    11  	// Progress contains a Message or...
    12  	Message string
    13  
    14  	// ...progress of an action
    15  	Action  string
    16  	Current int64
    17  	Total   int64
    18  
    19  	// If true, don't show xB/yB
    20  	HideCounts bool
    21  	// If not empty, use units instead of bytes for counts
    22  	Units string
    23  
    24  	// Aux contains extra information not presented to the user, such as
    25  	// digests for push signing.
    26  	Aux interface{}
    27  
    28  	LastUpdate bool
    29  }
    30  
    31  // Output is an interface for writing progress information. It's
    32  // like a writer for progress, but we don't call it Writer because
    33  // that would be confusing next to ProgressReader (also, because it
    34  // doesn't implement the io.Writer interface).
    35  type Output interface {
    36  	WriteProgress(Progress) error
    37  }
    38  
    39  type chanOutput chan<- Progress
    40  
    41  func (out chanOutput) WriteProgress(p Progress) error {
    42  	out <- p
    43  	return nil
    44  }
    45  
    46  // ChanOutput returns an Output that writes progress updates to the
    47  // supplied channel.
    48  func ChanOutput(progressChan chan<- Progress) Output {
    49  	return chanOutput(progressChan)
    50  }
    51  
    52  type discardOutput struct{}
    53  
    54  func (discardOutput) WriteProgress(Progress) error {
    55  	return nil
    56  }
    57  
    58  // DiscardOutput returns an Output that discards progress
    59  func DiscardOutput() Output {
    60  	return discardOutput{}
    61  }
    62  
    63  // Update is a convenience function to write a progress update to the channel.
    64  func Update(out Output, id, action string) {
    65  	out.WriteProgress(Progress{ID: id, Action: action})
    66  }
    67  
    68  // Updatef is a convenience function to write a printf-formatted progress update
    69  // to the channel.
    70  func Updatef(out Output, id, format string, a ...interface{}) {
    71  	Update(out, id, fmt.Sprintf(format, a...))
    72  }
    73  
    74  // Message is a convenience function to write a progress message to the channel.
    75  func Message(out Output, id, message string) {
    76  	out.WriteProgress(Progress{ID: id, Message: message})
    77  }
    78  
    79  // Messagef is a convenience function to write a printf-formatted progress
    80  // message to the channel.
    81  func Messagef(out Output, id, format string, a ...interface{}) {
    82  	Message(out, id, fmt.Sprintf(format, a...))
    83  }
    84  
    85  // Aux sends auxiliary information over a progress interface, which will not be
    86  // formatted for the UI. This is used for things such as push signing.
    87  func Aux(out Output, a interface{}) {
    88  	out.WriteProgress(Progress{Aux: a})
    89  }