Simplify the API around fine-grained spinner display rules

This commit is contained in:
Christian Rocha 2020-08-24 23:14:17 -04:00
parent 75df6b96a1
commit 5f7c8b5375
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -29,7 +29,7 @@ type Model struct {
// Type is the set of frames to use. See Spinner. // Type is the set of frames to use. See Spinner.
Frames Spinner Frames Spinner
// FPS is the speed at which the ticker should tick // FPS is the speed at which the ticker should tick.
FPS time.Duration FPS time.Duration
// ForegroundColor sets the background color of the spinner. It can be a // ForegroundColor sets the background color of the spinner. It can be a
@ -51,33 +51,46 @@ type Model struct {
// 200ms then MinimumLifetime will expire after 300ms. // 200ms then MinimumLifetime will expire after 300ms.
// //
// MinimumLifetime is optional. // MinimumLifetime is optional.
//
// This is considered experimental and may not appear in future versions of
// this library.
MinimumLifetime time.Duration MinimumLifetime time.Duration
// HideFor can be used to wait to show the spinner until a certain amount // HideFor can be used to wait to show the spinner until a certain amount
// of time has passed. This can be useful for preventing flicking when load // of time has passed. This can be useful for preventing flicking when load
// times are very fast. The hidden state can be set with HiddenState. // times are very fast. The hidden state can be set with HiddenState.
// Optional. // Optional.
//
// This is considered experimental and may not appear in future versions of
// this library.
HideFor time.Duration HideFor time.Duration
// HiddenState is the state to render the spinner when HideFor is in effect.
// For more control you can also use Model.Hidden() in the parent view.
HiddenState string
frame int frame int
startTime time.Time startTime time.Time
} }
// Start resets resets the spinner start time. For use with MinimumLifetime and // Start resets resets the spinner start time. For use with MinimumLifetime and
// MinimumStartTime. Optional. // MinimumStartTime. Optional.
//
// This is considered experimental and may not appear in future versions of
// this library.
func (m *Model) Start() { func (m *Model) Start() {
m.frame = 0
m.startTime = time.Now() m.startTime = time.Now()
} }
// MinimumLifetimeReached returns whether or not the spinner has run for the // hidden returns whether or not Model.HideFor is in effect.
// minimum specified duration, if any. If no minimum lifetime has been set, or func (m Model) hidden() bool {
// if Model.Start() hasn't been called this function returns true. if m.startTime.IsZero() {
func (m Model) MinimumLifetimeReached() bool { return false
}
if m.HideFor == 0 {
return false
}
return m.startTime.Add(m.HideFor).After(time.Now())
}
// finished returns whether Model.MinimumLifetimeReached has been met.
func (m Model) finished() bool {
if m.startTime.IsZero() { if m.startTime.IsZero() {
return true return true
} }
@ -87,17 +100,20 @@ func (m Model) MinimumLifetimeReached() bool {
return m.startTime.Add(m.HideFor).Add(m.MinimumLifetime).Before(time.Now()) return m.startTime.Add(m.HideFor).Add(m.MinimumLifetime).Before(time.Now())
} }
// Hidden returns whether or not the view should be rendered. Works in // Visible returns whether or not the view should be rendered. Works in
// conjunction with Model.HideFor. You can perform this message directly to // conjunction with Model.HideFor and Model.MinimumLifetimeReached. You should
// Do additional logic on your views. // use this message directly to determine whether or not to render this view in
func (m Model) Hidden() bool { // the parent view and whether to continue sending spin messaging in the
if m.startTime.IsZero() { // parent update function.
return false //
} // Also note that using this function is optional and generally considered for
if m.HideFor == 0 { // advanced use only. Most of the time your application logic will determine
return false // whether or not this view should be used.
} //
return m.startTime.Add(m.HideFor).After(time.Now()) // This is considered experimental and may not appear in future versions of
// this library.
func (m Model) Visible() bool {
return !m.hidden() && !m.finished()
} }
// NewModel returns a model with default values. // NewModel returns a model with default values.
@ -133,12 +149,6 @@ func View(model Model) string {
return "error" return "error"
} }
if model.Hidden() {
return termenv.String(model.HiddenState).
Background(color(model.BackgroundColor)).
String()
}
frame := model.Frames[model.frame] frame := model.Frames[model.frame]
if model.ForegroundColor != "" || model.BackgroundColor != "" { if model.ForegroundColor != "" || model.BackgroundColor != "" {