-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement multi-spinner. Non-interactive has unpolished UI/UX at this…
… stage.
- Loading branch information
Showing
4 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package spinner | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/ActiveState/cli/internal/constants" | ||
) | ||
|
||
type nonInteractive struct { | ||
isGroup bool | ||
supportsColors bool | ||
stop chan struct{} | ||
} | ||
|
||
func newNonInteractive(isGroup, supportColors bool) *nonInteractive { | ||
n := &nonInteractive{isGroup: isGroup, supportsColors: supportColors, stop: make(chan struct{}, 1)} | ||
go n.ticker() | ||
return n | ||
} | ||
|
||
func (n *nonInteractive) ticker() { | ||
ticker := time.NewTicker(constants.TerminalAnimationInterval) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
os.Stderr.WriteString(".") | ||
case <-n.stop: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (n *nonInteractive) Add(prefix string) Spinnable { | ||
os.Stderr.WriteString("\n" + color(prefix, !n.supportsColors) + " ") | ||
return n | ||
} | ||
|
||
func (n *nonInteractive) Wait() { | ||
n.stop <- struct{}{} | ||
} | ||
|
||
func (n *nonInteractive) Stop(msg string) { | ||
os.Stderr.WriteString(color(msg, !n.supportsColors) + "\n") | ||
if !n.isGroup { | ||
// If this isn't a group Wait will never be called | ||
n.stop <- struct{}{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package spinner | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"strings" | ||
|
||
"github.com/ActiveState/cli/internal/colorize" | ||
"github.com/ActiveState/cli/internal/errs" | ||
"github.com/ActiveState/cli/internal/logging" | ||
"github.com/vbauerster/mpb/v7" | ||
"github.com/vbauerster/mpb/v7/decor" | ||
) | ||
|
||
type Groupable interface { | ||
Add(prefix string) Spinnable | ||
Wait() | ||
} | ||
|
||
type Spinnable interface { | ||
Stop(msg string) | ||
} | ||
|
||
// Group collects multiple spinners | ||
type Group struct { | ||
mpbGroup *mpb.Progress | ||
supportColors bool | ||
interactive bool | ||
} | ||
|
||
// Spinner represents a single spinner | ||
type Spinner struct { | ||
mpbBar *mpb.Bar | ||
completionMsg string | ||
} | ||
|
||
// StandaloneSpinner represents a single spinner that is used in standalone, meaning it doesn't have a group. | ||
type StandaloneSpinner struct { | ||
*Spinner | ||
mpbGroup *mpb.Progress | ||
} | ||
|
||
func NewGroup(supportColors, interactive bool) Groupable { | ||
if !interactive { | ||
return newNonInteractive(true, supportColors) | ||
} | ||
return &Group{mpb.New( | ||
mpb.WithWidth(1)), | ||
supportColors, | ||
interactive, | ||
} | ||
} | ||
|
||
func NewSpinner(prefix string, supportColors, interactive bool) Spinnable { | ||
if !interactive { | ||
n := newNonInteractive(false, supportColors) | ||
n.Add(prefix) | ||
return n | ||
} | ||
mpbGroup := mpb.New(mpb.WithWidth(1)) | ||
return &StandaloneSpinner{newSpinner(mpbGroup, prefix, supportColors, interactive), mpbGroup} | ||
} | ||
|
||
func newSpinner(mpbGroup *mpb.Progress, prefix string, supportColors, interactive bool) *Spinner { | ||
s := &Spinner{} | ||
p := mpbGroup.Add( | ||
1, | ||
mpb.NewBarFiller(mpb.SpinnerStyle([]string{`|`, `/`, `-`, `\`}...)), | ||
mpb.PrependDecorators(decor.Any(func(s decor.Statistics) string { | ||
return color(prefix, !supportColors) | ||
})), | ||
barFillerOnComplete(func() string { return color(strings.TrimPrefix(s.completionMsg, " "), !supportColors) }), | ||
) | ||
s.mpbBar = p | ||
return s | ||
} | ||
|
||
func (g *Group) Add(prefix string) Spinnable { | ||
s := newSpinner(g.mpbGroup, prefix, g.supportColors, g.interactive) | ||
return s | ||
} | ||
|
||
func (s *Spinner) Stop(msg string) { | ||
s.completionMsg = msg | ||
s.mpbBar.Increment() // Our "bar" has a total of 1, so a single increment will complete it | ||
} | ||
|
||
func (s *StandaloneSpinner) Stop(msg string) { | ||
s.Spinner.Stop(msg) | ||
s.mpbGroup.Wait() | ||
} | ||
|
||
func (g *Group) Wait() { | ||
g.mpbGroup.Wait() | ||
} | ||
|
||
func color(v string, strip bool) string { | ||
if strip { | ||
return colorize.StripColorCodes(v) | ||
} | ||
|
||
b := &bytes.Buffer{} | ||
_, err := colorize.Colorize(v, b, false) | ||
if err != nil { | ||
logging.Warning("colorize failed, stripping colors - error: %s", errs.JoinMessage(err)) | ||
v = colorize.StripColorCodes(v) | ||
} else { | ||
v = b.String() | ||
} | ||
|
||
return v | ||
} | ||
|
||
func barFillerOnComplete(value func() string) mpb.BarOption { | ||
return mpb.BarFillerMiddleware(func(base mpb.BarFiller) mpb.BarFiller { | ||
return mpb.BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) { | ||
if st.Completed { | ||
io.WriteString(w, value()) | ||
} else { | ||
base.Fill(w, reqWidth, st) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters