mirror of
https://github.com/nabbar/golib.git
synced 2025-10-13 03:23:47 +08:00
Add Set context/cancel for progress packages
This commit is contained in:
@@ -38,6 +38,7 @@ import (
|
|||||||
type bar struct {
|
type bar struct {
|
||||||
u bool
|
u bool
|
||||||
t int64
|
t int64
|
||||||
|
|
||||||
b *mpb.Bar
|
b *mpb.Bar
|
||||||
s sem.Sem
|
s sem.Sem
|
||||||
}
|
}
|
||||||
|
@@ -29,11 +29,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/vbauerster/mpb/v5/decor"
|
"github.com/nabbar/golib/semaphore"
|
||||||
|
|
||||||
njs_semaphore "github.com/nabbar/golib/semaphore"
|
|
||||||
|
|
||||||
"github.com/vbauerster/mpb/v5"
|
"github.com/vbauerster/mpb/v5"
|
||||||
|
"github.com/vbauerster/mpb/v5/decor"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -72,6 +70,12 @@ type progressBar struct {
|
|||||||
type ProgressBar interface {
|
type ProgressBar interface {
|
||||||
GetMPB() *mpb.Progress
|
GetMPB() *mpb.Progress
|
||||||
|
|
||||||
|
GetContext() context.Context
|
||||||
|
SetContext(ctx context.Context)
|
||||||
|
|
||||||
|
GetCancel() context.CancelFunc
|
||||||
|
SetCancel(cancel context.CancelFunc)
|
||||||
|
|
||||||
SetSemaphoreOption(maxSimultaneous int, timeout time.Duration)
|
SetSemaphoreOption(maxSimultaneous int, timeout time.Duration)
|
||||||
|
|
||||||
NewBar(parent context.Context, total int64, options ...mpb.BarOption) Bar
|
NewBar(parent context.Context, total int64, options ...mpb.BarOption) Bar
|
||||||
@@ -80,14 +84,14 @@ type ProgressBar interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewProgressBar(timeout time.Duration, deadline time.Time, parent context.Context, options ...mpb.ContainerOption) ProgressBar {
|
func NewProgressBar(timeout time.Duration, deadline time.Time, parent context.Context, options ...mpb.ContainerOption) ProgressBar {
|
||||||
x, c := njs_semaphore.GetContext(timeout, deadline, parent)
|
x, c := semaphore.GetContext(timeout, deadline, parent)
|
||||||
|
|
||||||
return &progressBar{
|
return &progressBar{
|
||||||
mpb: mpb.New(options...),
|
mpb: mpb.New(options...),
|
||||||
ctx: x,
|
ctx: x,
|
||||||
cnl: c,
|
cnl: c,
|
||||||
sTimeOut: timeout,
|
sTimeOut: timeout,
|
||||||
sMaxSimul: njs_semaphore.GetMaxSimultaneous(),
|
sMaxSimul: semaphore.GetMaxSimultaneous(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,19 +104,19 @@ func (p *progressBar) SetSemaphoreOption(maxSimultaneous int, timeout time.Durat
|
|||||||
p.sTimeOut = timeout
|
p.sTimeOut = timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p progressBar) NewBar(parent context.Context, total int64, options ...mpb.BarOption) Bar {
|
func (p *progressBar) NewBar(parent context.Context, total int64, options ...mpb.BarOption) Bar {
|
||||||
if parent == nil {
|
if parent == nil {
|
||||||
parent = p.ctx
|
parent = p.ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
return newBar(
|
return newBar(
|
||||||
p.mpb.AddBar(0, options...),
|
p.mpb.AddBar(0, options...),
|
||||||
njs_semaphore.NewSemaphore(p.sMaxSimul, p.sTimeOut, njs_semaphore.GetEmptyTime(), parent),
|
semaphore.NewSemaphore(p.sMaxSimul, p.sTimeOut, semaphore.GetEmptyTime(), parent),
|
||||||
total,
|
total,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p progressBar) NewBarSimpleETA(name string) Bar {
|
func (p *progressBar) NewBarSimpleETA(name string) Bar {
|
||||||
return newBar(
|
return newBar(
|
||||||
p.mpb.AddBar(0,
|
p.mpb.AddBar(0,
|
||||||
mpb.BarStyle(defaultStyle),
|
mpb.BarStyle(defaultStyle),
|
||||||
@@ -127,12 +131,12 @@ func (p progressBar) NewBarSimpleETA(name string) Bar {
|
|||||||
),
|
),
|
||||||
mpb.AppendDecorators(decor.Percentage()),
|
mpb.AppendDecorators(decor.Percentage()),
|
||||||
),
|
),
|
||||||
njs_semaphore.NewSemaphore(p.sMaxSimul, p.sTimeOut, njs_semaphore.GetEmptyTime(), p.ctx),
|
semaphore.NewSemaphore(p.sMaxSimul, p.sTimeOut, semaphore.GetEmptyTime(), p.ctx),
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p progressBar) NewBarSimpleCounter(name string, total int64) Bar {
|
func (p *progressBar) NewBarSimpleCounter(name string, total int64) Bar {
|
||||||
return newBar(
|
return newBar(
|
||||||
p.mpb.AddBar(total,
|
p.mpb.AddBar(total,
|
||||||
mpb.BarStyle(defaultStyle),
|
mpb.BarStyle(defaultStyle),
|
||||||
@@ -149,7 +153,23 @@ func (p progressBar) NewBarSimpleCounter(name string, total int64) Bar {
|
|||||||
),
|
),
|
||||||
mpb.AppendDecorators(decor.Percentage()),
|
mpb.AppendDecorators(decor.Percentage()),
|
||||||
),
|
),
|
||||||
njs_semaphore.NewSemaphore(p.sMaxSimul, p.sTimeOut, njs_semaphore.GetEmptyTime(), p.ctx),
|
semaphore.NewSemaphore(p.sMaxSimul, p.sTimeOut, semaphore.GetEmptyTime(), p.ctx),
|
||||||
total,
|
total,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *progressBar) GetContext() context.Context {
|
||||||
|
return p.ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *progressBar) SetContext(ctx context.Context) {
|
||||||
|
p.ctx = ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *progressBar) GetCancel() context.CancelFunc {
|
||||||
|
return p.cnl
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *progressBar) SetCancel(cancel context.CancelFunc) {
|
||||||
|
p.cnl = cancel
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user