Refresh indicator goes red on error.
All checks were successful
Build & Test / Main (push) Successful in 1m0s
Release / Release (push) Successful in 1m3s

It will stay flashing red until the next refresh, at which point it goes
back to its normal color. On a successful refresh, it still stops.

Also, add a deadline of 15 seconds to the refresh command.
This commit is contained in:
Sam Fredrickson 2024-03-24 02:29:45 -07:00
parent 97f4793ec3
commit 7b445a02a2

View File

@ -84,8 +84,7 @@ func New(cfg config.Data) (m Model) {
m.indicator = spinner.New() m.indicator = spinner.New()
m.indicator.Spinner = spinner.Points m.indicator.Spinner = spinner.Points
m.indicator.Style = lipgloss.NewStyle(). m.indicator.Style = indicatorNormalStyle
Foreground(lipgloss.Color("69"))
return return
} }
@ -109,18 +108,22 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.inner.(type) { switch msg := msg.inner.(type) {
case refresh: case refresh:
m.refreshing = true m.refreshing = true
m.indicator.Style = indicatorNormalStyle
return m, tea.Batch( return m, tea.Batch(
m.resumeIndicator, m.resumeIndicator,
m.refresh, m.refresh,
) )
case moon.Math: case update:
m.math = msg commands := []tea.Cmd{m.scheduleRefresh()}
refillProperties(&m) if msg.err == nil {
refillProjections(&m) m.math = msg.math
return m, tea.Batch( refillProperties(&m)
m.stopIndicator(), refillProjections(&m)
m.scheduleRefresh(), commands = append(commands, m.stopIndicator())
) } else {
m.indicator.Style = indicatorErrorStyle
}
return m, tea.Batch(commands...)
case stopIndicator: case stopIndicator:
m.refreshing = false m.refreshing = false
return m, nil return m, nil
@ -137,17 +140,25 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
} }
type refresh struct{} type refresh struct{}
type update struct {
math moon.Math
err error
}
type stopIndicator struct{} type stopIndicator struct{}
func (m Model) refresh() tea.Msg { func (m Model) refresh() tea.Msg {
err := m.math.Refresh(context.TODO()) ctx, cancel := context.WithDeadline(
context.Background(),
time.Now().Add(time.Second*15))
defer cancel()
err := m.math.Refresh(ctx)
if err != nil { if err != nil {
slog.Error("refresh", slog.Error("refresh",
"asset", m.math.Asset, "asset", m.math.Asset,
"err", err, "err", err,
) )
} }
return Msg{m.math.Asset, m.math} return Msg{m.math.Asset, update{m.math, err}}
} }
func (m Model) resumeIndicator() tea.Msg { func (m Model) resumeIndicator() tea.Msg {
@ -224,3 +235,9 @@ func (m Model) View() string {
var baseStyle = lipgloss.NewStyle(). var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()). BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("240")) BorderForeground(lipgloss.Color("240"))
var indicatorNormalStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("69"))
var indicatorErrorStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("160"))