Add color settings to spinner

This commit is contained in:
Christian Rocha
2020-03-27 14:10:09 -04:00
parent c9aefb2448
commit eea5c2a32a

View File

@@ -5,6 +5,7 @@ import (
"time"
"github.com/charmbracelet/tea"
"github.com/muesli/termenv"
)
// Spinner denotes a type of spinner
@@ -24,6 +25,8 @@ var (
}
assertionErr = errors.New("could not perform assertion on model to what the spinner expects. are you sure you passed the right value?")
color = termenv.ColorProfile().Color
)
// Model contains the state for the spinner. Use NewModel to create new models
@@ -31,6 +34,9 @@ var (
type Model struct {
Type Spinner
FPS int
ForegroundColor string
BackgroundColor string
frame int
}
@@ -66,7 +72,18 @@ func View(model Model) string {
if model.frame >= len(s) {
return "[error]"
}
return s[model.frame]
str := s[model.frame]
if model.ForegroundColor != "" || model.BackgroundColor != "" {
return termenv.
String(str).
Foreground(color(model.ForegroundColor)).
Background(color(model.BackgroundColor)).
String()
}
return str
}
// Sub is the subscription that allows the spinner to spin