Skip to content

Commit

Permalink
don't auto refresh if output is not terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Feb 11, 2023
1 parent 4ed783c commit 93889df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
3 changes: 0 additions & 3 deletions container_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ func WithShutdownNotifier(ch chan struct{}) ContainerOption {
// will effectively disable auto refresh rate and discard any output,
// useful if you want to disable progress bars with little overhead.
func WithOutput(w io.Writer) ContainerOption {
var discarded bool
if w == nil {
w = io.Discard
discarded = true
}
return func(s *pState) {
s.output = w
s.outputDiscarded = discarded
}
}

Expand Down
5 changes: 5 additions & 0 deletions cwriter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func New(out io.Writer) *Writer {
return w
}

// IsTerminal tells whether underlying io.Writer is terminal.
func (w *Writer) IsTerminal() bool {
return w.terminal
}

// GetTermSize returns WxH of underlying terminal.
func (w *Writer) GetTermSize() (width, height int, err error) {
return w.termSize(w.fd)
Expand Down
43 changes: 23 additions & 20 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ type pState struct {
reqWidth int
popPriority int
popCompleted bool
outputDiscarded bool
disableAutoRefresh bool
ignoreNotTTY bool
manualRefresh chan interface{}
renderDelay <-chan struct{}
shutdownNotifier chan struct{}
shutdownNotifier chan<- interface{}
queueBars map[*Bar]*Bar
output io.Writer
debugOut io.Writer
Expand Down Expand Up @@ -90,16 +88,10 @@ func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress {
operateState: make(chan func(*pState)),
interceptIo: make(chan func(io.Writer)),
done: make(chan struct{}),
shutdown: make(chan struct{}),
cancel: cancel,
}

if s.shutdownNotifier != nil {
p.shutdown = s.shutdownNotifier
s.shutdownNotifier = nil
} else {
p.shutdown = make(chan struct{})
}

go p.serve(s, cwriter.New(s.output))
return p
}
Expand Down Expand Up @@ -233,11 +225,11 @@ func (p *Progress) Shutdown() {
<-p.shutdown
}

func (p *Progress) newTicker(s *pState) chan time.Time {
func (p *Progress) newTicker(s *pState, isTerminal bool) chan time.Time {
ch := make(chan time.Time)
go func() {
var autoRefresh <-chan time.Time
if !s.disableAutoRefresh && !s.outputDiscarded {
if isTerminal && !s.disableAutoRefresh {
if s.renderDelay != nil {
<-s.renderDelay
}
Expand Down Expand Up @@ -265,10 +257,13 @@ func (p *Progress) newTicker(s *pState) chan time.Time {
}

func (p *Progress) serve(s *pState, cw *cwriter.Writer) {
render := func() error {
return s.render(cw)
}

go s.hm.run()

refreshCh := p.newTicker(s)
refreshCh := p.newTicker(s, cw.IsTerminal())

for {
select {
Expand All @@ -277,26 +272,34 @@ func (p *Progress) serve(s *pState, cw *cwriter.Writer) {
case fn := <-p.interceptIo:
fn(cw)
case <-refreshCh:
err := s.render(cw)
err := render()
if err != nil {
refreshCh = nil
_, _ = fmt.Fprintln(s.debugOut, err.Error())
render = func() error { return nil }
p.cancel() // cancel all bars
}
case <-p.done:
close(s.hm)
if s.shutdownNotifier != nil {
go func() {
s.shutdownNotifier <- s.hm.end()
}()
} else {
close(s.hm)
}
close(p.shutdown)
return
}
}
}

func (s *pState) render(cw *cwriter.Writer) error {
width, height, err := cw.GetTermSize()
if err != nil {
if !s.ignoreNotTTY {
func (s *pState) render(cw *cwriter.Writer) (err error) {
var width, height int
if cw.IsTerminal() {
width, height, err = cw.GetTermSize()
if err != nil {
return err
}
} else {
width = s.reqWidth
height = 100
}
Expand Down

0 comments on commit 93889df

Please sign in to comment.