code-intelligence.com/cifuzz@v0.40.0/internal/cmd/remoterun/progress/progress.go (about)

     1  package progress
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/mitchellh/ioprogress"
    11  )
    12  
    13  func NewReader(reader io.Reader, size int64, successMessage string) *ioprogress.Reader {
    14  	return &ioprogress.Reader{
    15  		Reader:       reader,
    16  		Size:         size,
    17  		DrawFunc:     DrawProgressBar(os.Stdout, ioprogress.DrawTextFormatBar(60), successMessage),
    18  		DrawInterval: 100 * time.Millisecond,
    19  	}
    20  }
    21  
    22  func DrawProgressBar(w io.Writer, drawFormatBar ioprogress.DrawTextFormatFunc, successMessage string) ioprogress.DrawFunc {
    23  	var maxLength int
    24  
    25  	return func(progress, total int64) error {
    26  		if progress == -1 && total == -1 {
    27  			// Progress completed, so we clear the utils.progress bar and print the success message
    28  			_, err := fmt.Fprintln(w, strings.Repeat(" ", maxLength)+"\r"+successMessage)
    29  			return err
    30  		}
    31  
    32  		line := drawFormatBar(progress, total)
    33  
    34  		// Make sure we pad the line to the max length we've ever drawn so that
    35  		// we don't have trailing characters.
    36  		if len(line) < maxLength {
    37  			line += strings.Repeat(" ", maxLength-len(line))
    38  		}
    39  
    40  		maxLength = len(line)
    41  		_, err := fmt.Fprint(w, line+"\r")
    42  		return err
    43  	}
    44  }