Skip to content

Commit

Permalink
Add min time to show progress (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed Nov 13, 2022
1 parent 8e31b4a commit 571eeec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions console.go
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"
"sync"
"time"

"github.com/briandowns/spinner"
"github.com/heaths/go-console/pkg/colorscheme"
Expand Down Expand Up @@ -54,6 +55,7 @@ type con struct {
progress *spinner.Spinner
progressEnabled bool
progressLock sync.Mutex
progressMin <-chan time.Time
}

func System() Console {
Expand Down
19 changes: 17 additions & 2 deletions progress.go
Expand Up @@ -14,7 +14,7 @@ const (
ProgressStyleDots ProgressStyle = 11
)

type ProgressOption func(*spinner.Spinner)
type ProgressOption func(*con, *spinner.Spinner)

func (c *con) StartProgress(label string, opts ...ProgressOption) {
if !c.progressEnabled || !c.IsStderrTTY() {
Expand All @@ -38,6 +38,10 @@ func (c *con) StartProgress(label string, opts ...ProgressOption) {
sp.Suffix = " " + label
}

for _, opt := range opts {
opt(c, sp)
}

sp.Start()
c.progress = sp
}
Expand All @@ -50,12 +54,23 @@ func (c *con) StopProgress() {
return
}

if c.progressMin != nil {
<-c.progressMin
c.progressMin = nil
}

c.progress.Stop()
c.progress = nil
}

func WithMinimum(d time.Duration) ProgressOption {
return func(c *con, _ *spinner.Spinner) {
c.progressMin = time.After(d)
}
}

func WithProgressStyle(style ProgressStyle) ProgressOption {
return func(sp *spinner.Spinner) {
return func(_ *con, sp *spinner.Spinner) {
cs := spinner.CharSets[int(style)]
sp.UpdateCharSet(cs)
}
Expand Down

0 comments on commit 571eeec

Please sign in to comment.