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

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
1 changed files with 29 additions and 12 deletions

View File

@ -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"))