2024-03-23 00:43:15 +00:00
|
|
|
package asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-03-23 05:54:32 +00:00
|
|
|
"log/slog"
|
2024-03-23 00:43:15 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.humancabbage.net/sam/moonmath/coindesk"
|
|
|
|
"code.humancabbage.net/sam/moonmath/config"
|
|
|
|
"code.humancabbage.net/sam/moonmath/moon"
|
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
|
|
"github.com/charmbracelet/bubbles/table"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
math moon.Math
|
|
|
|
|
|
|
|
refreshing bool
|
|
|
|
indicator spinner.Model
|
|
|
|
|
|
|
|
properties table.Model
|
|
|
|
projections table.Model
|
|
|
|
}
|
|
|
|
|
|
|
|
type Msg struct {
|
|
|
|
Asset coindesk.Asset
|
|
|
|
inner tea.Msg
|
|
|
|
}
|
|
|
|
|
2024-03-29 08:15:59 +00:00
|
|
|
func New(cfg config.Asset) (m Model) {
|
2024-03-23 00:43:15 +00:00
|
|
|
m.math = moon.NewMath(
|
|
|
|
cfg.Asset,
|
|
|
|
cfg.Goals,
|
|
|
|
config.GetBases(&cfg),
|
|
|
|
)
|
|
|
|
|
|
|
|
tableStyle := table.DefaultStyles()
|
|
|
|
tableStyle.Selected = tableStyle.Cell.Copy().
|
|
|
|
Padding(0)
|
|
|
|
tableStyle.Header = tableStyle.Header.
|
|
|
|
Bold(true).
|
|
|
|
Foreground(lipgloss.Color("214")).
|
|
|
|
Border(baseStyle.GetBorderStyle(), false, false, true, false)
|
|
|
|
|
|
|
|
// properties table
|
|
|
|
|
|
|
|
m.properties = table.New(
|
|
|
|
table.WithColumns([]table.Column{
|
|
|
|
{Title: "Property", Width: 9},
|
|
|
|
{Title: "Value", Width: 9},
|
|
|
|
}),
|
|
|
|
table.WithHeight(2),
|
|
|
|
table.WithStyles(tableStyle),
|
|
|
|
)
|
|
|
|
|
|
|
|
// projections table
|
|
|
|
|
|
|
|
labelsWidth := 0
|
|
|
|
for _, l := range m.math.Labels {
|
|
|
|
if len(l) > labelsWidth {
|
|
|
|
labelsWidth = len(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
projectionCols := []table.Column{
|
|
|
|
{Title: "Labels", Width: labelsWidth},
|
|
|
|
}
|
|
|
|
for _, c := range m.math.Columns {
|
|
|
|
projectionCols = append(projectionCols,
|
|
|
|
table.Column{
|
|
|
|
Title: c.Base.Label(),
|
|
|
|
Width: 10,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
m.projections = table.New(
|
|
|
|
table.WithColumns(projectionCols),
|
|
|
|
table.WithHeight(len(m.math.Labels)),
|
|
|
|
table.WithStyles(tableStyle),
|
|
|
|
)
|
|
|
|
|
|
|
|
// indicator spinner
|
|
|
|
|
|
|
|
m.indicator = spinner.New()
|
|
|
|
m.indicator.Spinner = spinner.Points
|
2024-03-24 09:29:45 +00:00
|
|
|
m.indicator.Style = indicatorNormalStyle
|
2024-03-23 00:43:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
|
|
return tea.Batch(
|
|
|
|
m.indicator.Tick,
|
|
|
|
func() tea.Msg {
|
|
|
|
return Msg{m.math.Asset, refresh{}}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-03-29 08:15:59 +00:00
|
|
|
func (m Model) Handles(a coindesk.Asset) bool {
|
|
|
|
return m.math.Asset == a
|
|
|
|
}
|
|
|
|
|
2024-03-23 00:43:15 +00:00
|
|
|
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case Msg:
|
|
|
|
switch msg := msg.inner.(type) {
|
|
|
|
case refresh:
|
|
|
|
m.refreshing = true
|
2024-03-24 09:29:45 +00:00
|
|
|
m.indicator.Style = indicatorNormalStyle
|
2024-03-23 04:12:14 +00:00
|
|
|
return m, tea.Batch(
|
2024-03-23 05:54:32 +00:00
|
|
|
m.resumeIndicator,
|
|
|
|
m.refresh,
|
2024-03-25 06:01:51 +00:00
|
|
|
m.scheduleRefresh(),
|
2024-03-23 04:12:14 +00:00
|
|
|
)
|
2024-03-24 09:29:45 +00:00
|
|
|
case update:
|
2024-03-25 06:01:51 +00:00
|
|
|
var cmd tea.Cmd
|
2024-03-24 09:29:45 +00:00
|
|
|
if msg.err == nil {
|
|
|
|
m.math = msg.math
|
|
|
|
refillProperties(&m)
|
|
|
|
refillProjections(&m)
|
2024-03-25 06:01:51 +00:00
|
|
|
cmd = m.stopIndicator()
|
2024-03-24 09:29:45 +00:00
|
|
|
} else {
|
|
|
|
m.indicator.Style = indicatorErrorStyle
|
|
|
|
}
|
2024-03-25 06:01:51 +00:00
|
|
|
return m, cmd
|
2024-03-23 00:43:15 +00:00
|
|
|
case stopIndicator:
|
|
|
|
m.refreshing = false
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
case spinner.TickMsg:
|
2024-03-23 04:12:14 +00:00
|
|
|
if !m.refreshing {
|
|
|
|
return m, nil
|
|
|
|
}
|
2024-03-23 00:43:15 +00:00
|
|
|
var cmd tea.Cmd
|
|
|
|
m.indicator, cmd = m.indicator.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type refresh struct{}
|
2024-03-24 09:29:45 +00:00
|
|
|
type update struct {
|
|
|
|
math moon.Math
|
|
|
|
err error
|
|
|
|
}
|
2024-03-23 00:43:15 +00:00
|
|
|
type stopIndicator struct{}
|
|
|
|
|
2024-03-23 05:54:32 +00:00
|
|
|
func (m Model) refresh() tea.Msg {
|
2024-03-24 09:29:45 +00:00
|
|
|
ctx, cancel := context.WithDeadline(
|
|
|
|
context.Background(),
|
2024-03-25 06:01:51 +00:00
|
|
|
time.Now().Add(refreshDeadline))
|
2024-03-24 09:29:45 +00:00
|
|
|
defer cancel()
|
|
|
|
err := m.math.Refresh(ctx)
|
2024-03-23 05:54:32 +00:00
|
|
|
if err != nil {
|
|
|
|
slog.Error("refresh",
|
|
|
|
"asset", m.math.Asset,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
}
|
2024-03-24 09:29:45 +00:00
|
|
|
return Msg{m.math.Asset, update{m.math, err}}
|
2024-03-23 05:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) resumeIndicator() tea.Msg {
|
|
|
|
return m.indicator.Tick()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) scheduleRefresh() tea.Cmd {
|
2024-03-25 06:01:51 +00:00
|
|
|
return tea.Tick(refreshInterval,
|
2024-03-23 05:54:32 +00:00
|
|
|
func(t time.Time) tea.Msg {
|
|
|
|
return Msg{m.math.Asset, refresh{}}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) stopIndicator() tea.Cmd {
|
|
|
|
// wait a bit to stop the indicator, so that it's more obvious
|
|
|
|
// even when the refresh completes quickly.
|
2024-03-25 06:01:51 +00:00
|
|
|
return tea.Tick(stopIndicatorDelay,
|
2024-03-23 05:54:32 +00:00
|
|
|
func(t time.Time) tea.Msg {
|
|
|
|
return Msg{m.math.Asset, stopIndicator{}}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-25 06:01:51 +00:00
|
|
|
var refreshInterval = time.Second * 30
|
|
|
|
var refreshDeadline = time.Second * 15
|
|
|
|
var stopIndicatorDelay = time.Millisecond * 500
|
|
|
|
|
2024-03-23 00:43:15 +00:00
|
|
|
func refillProperties(m *Model) {
|
|
|
|
rows := []table.Row{
|
|
|
|
{"Asset", string(m.math.Asset)},
|
|
|
|
{"Price", fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
|
|
|
|
}
|
|
|
|
m.properties.SetRows(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func refillProjections(m *Model) {
|
2024-03-29 08:15:59 +00:00
|
|
|
cols := []table.Row{m.math.Labels}
|
2024-03-23 00:43:15 +00:00
|
|
|
for i := range m.math.Columns {
|
2024-03-29 08:15:59 +00:00
|
|
|
entries := renderEntries(m.math.Columns[i])
|
|
|
|
cols = append(cols, entries)
|
2024-03-23 00:43:15 +00:00
|
|
|
}
|
2024-03-29 08:15:59 +00:00
|
|
|
rows := transpose(cols)
|
2024-03-23 00:43:15 +00:00
|
|
|
m.projections.SetRows(rows)
|
|
|
|
}
|
|
|
|
|
2024-03-29 08:15:59 +00:00
|
|
|
func renderEntries(c moon.Column) (entries []string) {
|
|
|
|
entries = append(entries, fmt.Sprintf("$%.2f", c.StartingPrice))
|
|
|
|
entries = append(entries, fmt.Sprintf("%.2f%%", (c.CDPR-1)*100))
|
|
|
|
never := c.CDPR <= 1
|
|
|
|
for i := range c.Projections.Dates {
|
|
|
|
var cell string
|
|
|
|
if never {
|
|
|
|
cell = "NEVER!!!!!"
|
|
|
|
} else {
|
|
|
|
cell = c.
|
|
|
|
Projections.
|
|
|
|
Dates[i].
|
|
|
|
Format("2006-01-02")
|
|
|
|
}
|
|
|
|
entries = append(entries, cell)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-23 00:43:15 +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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) View() string {
|
|
|
|
var s string
|
|
|
|
indicator := ""
|
|
|
|
if m.refreshing {
|
|
|
|
indicator = m.indicator.View()
|
|
|
|
}
|
|
|
|
right := lipgloss.JoinVertical(
|
|
|
|
lipgloss.Center,
|
|
|
|
baseStyle.Render(m.properties.View()),
|
|
|
|
indicator,
|
|
|
|
)
|
|
|
|
s += lipgloss.JoinHorizontal(
|
|
|
|
lipgloss.Center,
|
|
|
|
right,
|
|
|
|
baseStyle.Render(m.projections.View()),
|
|
|
|
)
|
|
|
|
return s + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseStyle = lipgloss.NewStyle().
|
|
|
|
BorderStyle(lipgloss.RoundedBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("240"))
|
2024-03-24 09:29:45 +00:00
|
|
|
|
|
|
|
var indicatorNormalStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("69"))
|
|
|
|
|
|
|
|
var indicatorErrorStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("160"))
|