From 7b445a02a2090802c76155d1da7fa2ade5bbc9b0 Mon Sep 17 00:00:00 2001 From: Sam Fredrickson Date: Sun, 24 Mar 2024 02:29:45 -0700 Subject: [PATCH] Refresh indicator goes red on error. 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. --- tui/asset/asset.go | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/tui/asset/asset.go b/tui/asset/asset.go index a9608d7..a4169b0 100644 --- a/tui/asset/asset.go +++ b/tui/asset/asset.go @@ -84,8 +84,7 @@ func New(cfg config.Data) (m Model) { m.indicator = spinner.New() m.indicator.Spinner = spinner.Points - m.indicator.Style = lipgloss.NewStyle(). - Foreground(lipgloss.Color("69")) + m.indicator.Style = indicatorNormalStyle return } @@ -109,18 +108,22 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { switch msg := msg.inner.(type) { case refresh: m.refreshing = true + m.indicator.Style = indicatorNormalStyle return m, tea.Batch( m.resumeIndicator, m.refresh, ) - case moon.Math: - m.math = msg - refillProperties(&m) - refillProjections(&m) - return m, tea.Batch( - m.stopIndicator(), - m.scheduleRefresh(), - ) + case update: + commands := []tea.Cmd{m.scheduleRefresh()} + if msg.err == nil { + m.math = msg.math + refillProperties(&m) + refillProjections(&m) + commands = append(commands, m.stopIndicator()) + } else { + m.indicator.Style = indicatorErrorStyle + } + return m, tea.Batch(commands...) case stopIndicator: m.refreshing = false return m, nil @@ -137,17 +140,25 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { } type refresh struct{} +type update struct { + math moon.Math + err error +} type stopIndicator struct{} 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 { slog.Error("refresh", "asset", m.math.Asset, "err", err, ) } - return Msg{m.math.Asset, m.math} + return Msg{m.math.Asset, update{m.math, err}} } func (m Model) resumeIndicator() tea.Msg { @@ -224,3 +235,9 @@ func (m Model) View() string { var baseStyle = lipgloss.NewStyle(). BorderStyle(lipgloss.RoundedBorder()). BorderForeground(lipgloss.Color("240")) + +var indicatorNormalStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("69")) + +var indicatorErrorStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("160"))