Skip to content

Commit

Permalink
multiple bars
Browse files Browse the repository at this point in the history
  • Loading branch information
cheggaaa committed Dec 16, 2014
1 parent e8c7cc5 commit 9ba607e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type ProgressBar struct {
currentValue int64

prefix, postfix string

lastPrint string

BarStart string
BarEnd string
Expand Down Expand Up @@ -287,8 +289,9 @@ func (pb *ProgressBar) write(current int64) {
if len(out) < width {
end = strings.Repeat(" ", width-len(out))
}

// and print!
pb.lastPrint = out+end
switch {
case pb.Output != nil:
fmt.Fprint(pb.Output, "\r"+out+end)
Expand Down Expand Up @@ -322,6 +325,10 @@ func (pb *ProgressBar) Update() {
}
}

func (pb *ProgressBar) String() string {
return pb.lastPrint
}

// Internal loop for writing progressbar
func (pb *ProgressBar) writer() {
for {
Expand Down
51 changes: 51 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pb

import (
"fmt"
"time"
)

type Pool struct {
RefreshRate time.Duration
bars []*ProgressBar
}

func (p *Pool) Add(pbs ...*ProgressBar) {
for _, bar := range pbs {
bar.ManualUpdate = true
bar.NotPrint = true
bar.Start()
p.bars = append(p.bars, bar)
}
}

func (p *Pool) Start() {
p.RefreshRate = DefaultRefreshRate
go p.writer()
}

func (p *Pool) writer() {
var first = true
var out string
for {
if first {
out = ""
first = false
} else {
out = fmt.Sprintf("\033[%dA", len(p.bars))
}
isFinished := true
for _, bar := range p.bars {
bar.Update()
out += fmt.Sprintf("\r%s\n", bar.String())
if !bar.isFinish {
isFinished = false
}
}
fmt.Print(out)
if isFinished {
return
}
time.Sleep(p.RefreshRate)
}
}

0 comments on commit 9ba607e

Please sign in to comment.