moonmath/tui/tui.go

131 lines
2.6 KiB
Go
Raw Normal View History

2024-03-20 01:38:46 +00:00
package tui
import (
"context"
"fmt"
"time"
2024-03-20 04:44:11 +00:00
"code.humancabbage.net/sam/moonmath/config"
2024-03-20 01:38:46 +00:00
"code.humancabbage.net/sam/moonmath/moon"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
type Model struct {
math moon.Math
prices table.Model
projections table.Model
}
2024-03-20 04:44:11 +00:00
func New(cfg config.Data) Model {
math := moon.NewMath(
cfg.Asset,
cfg.Goals,
config.GetBases(&cfg))
2024-03-20 01:38:46 +00:00
tableStyle := table.DefaultStyles()
tableStyle.Selected = lipgloss.NewStyle()
prices := table.New(
table.WithColumns([]table.Column{
2024-03-20 04:44:11 +00:00
{Title: "Asset", Width: 6},
2024-03-20 01:38:46 +00:00
{Title: "Price", Width: 9},
}),
table.WithHeight(1),
table.WithStyles(tableStyle),
)
projectionCols := []table.Column{
{Title: "Labels", Width: 8},
}
for i := range math.Columns {
projectionCols = append(projectionCols, table.Column{
2024-03-20 04:44:11 +00:00
Title: math.Columns[i].Base.Label(),
2024-03-20 01:38:46 +00:00
Width: 10,
})
}
projections := table.New(
table.WithColumns(projectionCols),
table.WithHeight(len(math.Labels)),
2024-03-20 01:38:46 +00:00
table.WithStyles(tableStyle),
)
return Model{
math: math,
prices: prices,
projections: projections,
}
}
func (m Model) Init() tea.Cmd {
return func() tea.Msg {
_ = m.math.Refresh(context.TODO())
return m.math
}
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case moon.Math:
m.math = msg
refillPrice(&m)
refillProjections(&m)
return m, tea.Tick(time.Second*30, func(t time.Time) tea.Msg {
_ = m.math.Refresh(context.TODO())
return m.math
})
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
}
}
return m, nil
}
func refillPrice(m *Model) {
rows := []table.Row{
2024-03-20 04:44:11 +00:00
[]string{string(m.math.Asset), fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
2024-03-20 01:38:46 +00:00
}
m.prices.SetRows(rows)
}
func refillProjections(m *Model) {
rows := []table.Row{m.math.Labels}
for i := range m.math.Columns {
rows = append(rows, m.math.Columns[i].Column())
}
rows = transpose(rows)
m.projections.SetRows(rows)
}
2024-03-20 01:38:46 +00:00
func transpose(slice []table.Row) []table.Row {
xl := len(slice[0])
yl := len(slice)
result := make([]table.Row, xl)
for i := range result {
result[i] = make(table.Row, yl)
}
for i := 0; i < xl; i++ {
for j := 0; j < yl; j++ {
result[i][j] = slice[j][i]
2024-03-20 01:38:46 +00:00
}
}
return result
2024-03-20 01:38:46 +00:00
}
func (m Model) View() string {
var s string
s += lipgloss.JoinHorizontal(
lipgloss.Top,
baseStyle.Render(m.prices.View()),
baseStyle.Render(m.projections.View()),
)
return s + "\n"
}