Remove regular status printing for non terminals

This commit is contained in:
Alexander Neumann 2017-05-25 17:03:48 +02:00
parent 37b107b90b
commit 5247de552a

View file

@ -14,6 +14,7 @@ const minTickerTime = time.Second / 60
var isTerminal = terminal.IsTerminal(int(os.Stdout.Fd())) var isTerminal = terminal.IsTerminal(int(os.Stdout.Fd()))
var forceUpdateProgress = make(chan bool) var forceUpdateProgress = make(chan bool)
// Progress reports progress on an operation.
type Progress struct { type Progress struct {
OnStart func() OnStart func()
OnUpdate ProgressFunc OnUpdate ProgressFunc
@ -32,6 +33,7 @@ type Progress struct {
running bool running bool
} }
// Stat captures newly done parts of the operation.
type Stat struct { type Stat struct {
Files uint64 Files uint64
Dirs uint64 Dirs uint64
@ -41,6 +43,7 @@ type Stat struct {
Errors uint64 Errors uint64
} }
// ProgressFunc is used to report progress back to the user.
type ProgressFunc func(s Stat, runtime time.Duration, ticker bool) type ProgressFunc func(s Stat, runtime time.Duration, ticker bool)
// NewProgress returns a new progress reporter. When Start() is called, the // NewProgress returns a new progress reporter. When Start() is called, the
@ -50,10 +53,7 @@ type ProgressFunc func(s Stat, runtime time.Duration, ticker bool)
// synchronously and can use shared state. // synchronously and can use shared state.
func NewProgress() *Progress { func NewProgress() *Progress {
var d time.Duration var d time.Duration
if !isTerminal { if isTerminal {
// TODO: make the duration for non-terminal progress (user) configurable
d = time.Duration(10) * time.Second
} else {
d = time.Second d = time.Second
} }
return &Progress{d: d} return &Progress{d: d}
@ -70,7 +70,10 @@ func (p *Progress) Start() {
p.running = true p.running = true
p.Reset() p.Reset()
p.start = time.Now() p.start = time.Now()
p.c = time.NewTicker(p.d) p.c = nil
if p.d != 0 {
p.c = time.NewTicker(p.d)
}
if p.OnStart != nil { if p.OnStart != nil {
p.OnStart() p.OnStart()
@ -143,14 +146,21 @@ func (p *Progress) reporter() {
p.updateProgress(cur, true) p.updateProgress(cur, true)
} }
var ticker <-chan time.Time
if p.c != nil {
ticker = p.c.C
}
for { for {
select { select {
case <-p.c.C: case <-ticker:
updateProgress() updateProgress()
case <-forceUpdateProgress: case <-forceUpdateProgress:
updateProgress() updateProgress()
case <-p.cancel: case <-p.cancel:
p.c.Stop() if p.c != nil {
p.c.Stop()
}
return return
} }
} }