"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/progress.go" (28 May 2021, 1938 Bytes) of package /linux/misc/old/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package drive
    2 
    3 import (
    4     "fmt"
    5     "io"
    6     "io/ioutil"
    7     "time"
    8 )
    9 
   10 const MaxDrawInterval = time.Second * 1
   11 const MaxRateInterval = time.Second * 3
   12 
   13 func getProgressReader(r io.Reader, w io.Writer, size int64) io.Reader {
   14     // Don't wrap reader if output is discarded or size is too small
   15     if w == ioutil.Discard || (size > 0 && size < 1024*1024) {
   16         return r
   17     }
   18 
   19     return &Progress{
   20         Reader: r,
   21         Writer: w,
   22         Size:   size,
   23     }
   24 }
   25 
   26 type Progress struct {
   27     Writer       io.Writer
   28     Reader       io.Reader
   29     Size         int64
   30     progress     int64
   31     rate         int64
   32     rateProgress int64
   33     rateUpdated  time.Time
   34     updated      time.Time
   35     done         bool
   36 }
   37 
   38 func (self *Progress) Read(p []byte) (int, error) {
   39     // Read
   40     n, err := self.Reader.Read(p)
   41 
   42     now := time.Now()
   43     isLast := err != nil
   44 
   45     // Increment progress
   46     newProgress := self.progress + int64(n)
   47     self.progress = newProgress
   48 
   49     // Initialize rate state
   50     if self.rateUpdated.IsZero() {
   51         self.rateUpdated = now
   52         self.rateProgress = newProgress
   53     }
   54 
   55     // Update rate every x seconds
   56     if self.rateUpdated.Add(MaxRateInterval).Before(now) {
   57         self.rate = calcRate(newProgress-self.rateProgress, self.rateUpdated, now)
   58         self.rateUpdated = now
   59         self.rateProgress = newProgress
   60     }
   61 
   62     // Draw progress every x seconds
   63     if self.updated.Add(MaxDrawInterval).Before(now) || isLast {
   64         self.draw(isLast)
   65         self.updated = now
   66     }
   67 
   68     // Mark as done if error occurs
   69     self.done = isLast
   70 
   71     return n, err
   72 }
   73 
   74 func (self *Progress) draw(isLast bool) {
   75     if self.done {
   76         return
   77     }
   78 
   79     self.clear()
   80 
   81     // Print progress
   82     fmt.Fprintf(self.Writer, "%s", formatSize(self.progress, false))
   83 
   84     // Print total size
   85     if self.Size > 0 {
   86         fmt.Fprintf(self.Writer, "/%s", formatSize(self.Size, false))
   87     }
   88 
   89     // Print rate
   90     if self.rate > 0 {
   91         fmt.Fprintf(self.Writer, ", Rate: %s/s", formatSize(self.rate, false))
   92     }
   93 
   94     if isLast {
   95         self.clear()
   96     }
   97 }
   98 
   99 func (self *Progress) clear() {
  100     fmt.Fprintf(self.Writer, "\r%50s\r", "")
  101 }