github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/core/progress.go (about)

     1  package core
     2  
     3  import (
     4  	"io"
     5  	"regexp"
     6  	"strconv"
     7  )
     8  
     9  // A progressWriter implements progress display for a target; when it's written to
    10  // it attempts to infer progress from the output.
    11  // Right now the heuristic to measure progress is pretty simple but we may expand it later.
    12  type progressWriter struct {
    13  	t  *BuildTarget
    14  	w  io.Writer
    15  	re *regexp.Regexp
    16  }
    17  
    18  func newProgressWriter(t *BuildTarget, w io.Writer) io.Writer {
    19  	return &progressWriter{t: t, w: w, re: regexp.MustCompile(`\[ *([0-9]+)%\]`)}
    20  }
    21  
    22  // Write implements the io.Writer interface
    23  func (w *progressWriter) Write(b []byte) (int, error) {
    24  	if matches := w.re.FindAllSubmatch(b, -1); matches != nil {
    25  		if f, err := strconv.ParseFloat(string(matches[len(matches)-1][1]), 32); err == nil {
    26  			w.t.Progress = float32(f)
    27  		}
    28  	}
    29  	return w.w.Write(b)
    30  }