Add spinner that indicates an ongoing refresh.
All checks were successful
Build & Test / Main (push) Successful in 57s
All checks were successful
Build & Test / Main (push) Successful in 57s
This commit is contained in:
parent
1c6d5e9917
commit
19de412fc5
46
tui/tui.go
46
tui/tui.go
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"code.humancabbage.net/sam/moonmath/config"
|
"code.humancabbage.net/sam/moonmath/config"
|
||||||
"code.humancabbage.net/sam/moonmath/moon"
|
"code.humancabbage.net/sam/moonmath/moon"
|
||||||
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
"github.com/charmbracelet/bubbles/table"
|
"github.com/charmbracelet/bubbles/table"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
@ -19,6 +20,9 @@ var baseStyle = lipgloss.NewStyle().
|
|||||||
type Model struct {
|
type Model struct {
|
||||||
math moon.Math
|
math moon.Math
|
||||||
|
|
||||||
|
reloading bool
|
||||||
|
indicator spinner.Model
|
||||||
|
|
||||||
prices table.Model
|
prices table.Model
|
||||||
projections table.Model
|
projections table.Model
|
||||||
}
|
}
|
||||||
@ -55,29 +59,46 @@ func New(cfg config.Data) Model {
|
|||||||
table.WithStyles(tableStyle),
|
table.WithStyles(tableStyle),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
indicator := spinner.New()
|
||||||
|
indicator.Spinner = spinner.Points
|
||||||
|
indicator.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))
|
||||||
|
|
||||||
return Model{
|
return Model{
|
||||||
math: math,
|
math: math,
|
||||||
|
indicator: indicator,
|
||||||
prices: prices,
|
prices: prices,
|
||||||
projections: projections,
|
projections: projections,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) Init() tea.Cmd {
|
func (m Model) Init() tea.Cmd {
|
||||||
return func() tea.Msg {
|
return tea.Batch(
|
||||||
_ = m.math.Refresh(context.TODO())
|
m.indicator.Tick,
|
||||||
return m.math
|
func() tea.Msg {
|
||||||
}
|
return refresh{}
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
case refresh:
|
||||||
|
m.reloading = true
|
||||||
|
return m, func() tea.Msg {
|
||||||
|
_ = m.math.Refresh(context.TODO())
|
||||||
|
return m.math
|
||||||
|
}
|
||||||
|
case spinner.TickMsg:
|
||||||
|
var cmd tea.Cmd
|
||||||
|
m.indicator, cmd = m.indicator.Update(msg)
|
||||||
|
return m, cmd
|
||||||
case moon.Math:
|
case moon.Math:
|
||||||
m.math = msg
|
m.math = msg
|
||||||
|
m.reloading = false
|
||||||
refillPrice(&m)
|
refillPrice(&m)
|
||||||
refillProjections(&m)
|
refillProjections(&m)
|
||||||
return m, tea.Tick(time.Second*30, func(t time.Time) tea.Msg {
|
return m, tea.Tick(time.Second*30, func(t time.Time) tea.Msg {
|
||||||
_ = m.math.Refresh(context.TODO())
|
return refresh{}
|
||||||
return m.math
|
|
||||||
})
|
})
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
@ -88,6 +109,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type refresh struct{}
|
||||||
|
|
||||||
func refillPrice(m *Model) {
|
func refillPrice(m *Model) {
|
||||||
rows := []table.Row{
|
rows := []table.Row{
|
||||||
[]string{string(m.math.Asset), fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
|
[]string{string(m.math.Asset), fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
|
||||||
@ -121,9 +144,18 @@ func transpose(slice []table.Row) []table.Row {
|
|||||||
|
|
||||||
func (m Model) View() string {
|
func (m Model) View() string {
|
||||||
var s string
|
var s string
|
||||||
|
indicator := ""
|
||||||
|
if m.reloading {
|
||||||
|
indicator = m.indicator.View()
|
||||||
|
}
|
||||||
|
right := lipgloss.JoinVertical(
|
||||||
|
lipgloss.Center,
|
||||||
|
baseStyle.Render(m.prices.View()),
|
||||||
|
indicator,
|
||||||
|
)
|
||||||
s += lipgloss.JoinHorizontal(
|
s += lipgloss.JoinHorizontal(
|
||||||
lipgloss.Top,
|
lipgloss.Top,
|
||||||
baseStyle.Render(m.prices.View()),
|
right,
|
||||||
baseStyle.Render(m.projections.View()),
|
baseStyle.Render(m.projections.View()),
|
||||||
)
|
)
|
||||||
return s + "\n"
|
return s + "\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user