Skip to content

Commit

Permalink
adjusted render first frame after end
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeice committed Nov 7, 2020
1 parent 5d29b69 commit 14a8fff
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion default_decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func timeLeft(frame *FrameContext, cols int) string {
var left time.Duration
var timeLeftBox string
select {
case <-frame.bar.finish:
case <-frame.bar.ctx.Done():
default:
if currentFromStart > 0 {
perEntry := fromChange / time.Duration(currentFromStart)
Expand Down
20 changes: 10 additions & 10 deletions progress.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gogress

import (
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -44,7 +45,8 @@ type Progress struct {
finishedTime time.Time

finishOnce sync.Once
finish chan struct{}
ctx context.Context
stopFn context.CancelFunc
isFinish bool
pooled bool
frameCount int64
Expand All @@ -62,8 +64,10 @@ func New(max int) *Progress {
}

func newBar() *Progress {
ctx, fn := context.WithCancel(context.Background())
bar := &Progress{
finish: make(chan struct{}),
ctx: ctx,
stopFn: fn,
RefreshRate: defaultRefreshRate,
Format: format.DefaultFormat,
Units: format.U_NO,
Expand Down Expand Up @@ -171,11 +175,7 @@ func (p *Progress) write(total, current int64) {
atomic.AddInt64(&p.frameCount, 1)
frame := NewFrame(p, current, total, p.Width, p.frameCount)

if !isFinish {
p.frameParser.UpdateFrame(frame)
} else {
p.frameParser.UpdateFrame(frame)
}
p.frameParser.UpdateFrame(frame)

toPrint := append([]byte(p.frameParser.Last()), '\n')
switch {
Expand All @@ -193,7 +193,7 @@ func (p *Progress) write(total, current int64) {

func (p *Progress) Finish() {
p.finishOnce.Do(func() {
close(p.finish)
p.stopFn()
if !p.pooled {
p.write(atomic.LoadInt64(&p.max), atomic.LoadInt64(&p.current))
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func (p *Progress) Update() {
p.startTime = time.Now()
p.startValue = 0
} else if current >= max && !p.isFinish {
p.Finish()
p.stopFn()
}
}

Expand All @@ -261,7 +261,7 @@ func (p *Progress) Reset64(max int64) *Progress {
func (p *Progress) refresher() {
for {
select {
case <-p.finish:
case <-p.ctx.Done():
p.Update()
return
case <-time.After(p.RefreshRate):
Expand Down
13 changes: 9 additions & 4 deletions progress_pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gogress

import (
"context"
"os"
"sync"
"time"
Expand All @@ -9,9 +10,11 @@ import (
)

func NewPool() *Pool {
ctx, fn := context.WithCancel(context.Background())
return &Pool{
bars: []*Progress{},
finish: make(chan struct{}),
ctx: ctx,
stopFn: fn,
isRunning: false,
RefreshRate: time.Second / 45,
writer: writer.New(os.Stdout),
Expand All @@ -20,7 +23,8 @@ func NewPool() *Pool {

type Pool struct {
bars []*Progress
finish chan struct{}
ctx context.Context
stopFn context.CancelFunc
RefreshRate time.Duration
writer *writer.Writer
isRunning bool
Expand Down Expand Up @@ -77,7 +81,7 @@ func (p *Pool) NewBar64(max int64) *Progress {
func (p *Pool) refresher() {
for {
select {
case <-p.finish:
case <-p.ctx.Done():
p.isRunning = false
return
case <-time.After(p.RefreshRate):
Expand Down Expand Up @@ -110,11 +114,12 @@ func (p *Pool) Update() {

func (p *Pool) FinishAll() {
p.finishOnce.Do(func() {
close(p.finish)
p.stopFn()

for _, bar := range p.bars {
bar.Finish()
}

p.mu.Lock()
defer p.mu.Unlock()
p.Update()
Expand Down
31 changes: 31 additions & 0 deletions sample/multi_err/progress_sample_multi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"time"

"github.com/snakeice/gogress"
)

const (
TOTAL = 100
BARS = 6
)

func main() {
pool := gogress.NewPool()
pool.RefreshRate = time.Second / 30

newBar := func() *gogress.Progress {
bar := pool.NewBar(TOTAL)
bar.Set(56)
return bar
}

pool.Start()
defer pool.FinishAll()

for i := 0; i < BARS; i++ {
_ = newBar()
}

}
10 changes: 4 additions & 6 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ func (tp *TemplateParser) Last() string {
}

func (tp *TemplateParser) UpdateFrame(frame *FrameContext) {
if frame.IsFinish {
if frame.Width != tp.lastContext.Width {
atomic.AddInt64(&tp.frameNo, 1)
tp.lastContext.Width = frame.Width
tp.parseContext(frame)
}
if tp.lastContext != nil && frame.Width != tp.lastContext.Width {
atomic.AddInt64(&tp.frameNo, 1)
tp.lastContext.Width = frame.Width
tp.parseContext(frame)
} else {
atomic.AddInt64(&tp.frameNo, 1)
tp.parseContext(frame)
Expand Down

0 comments on commit 14a8fff

Please sign in to comment.