Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
7b445a02a2 | |||
97f4793ec3 | |||
270534c0d5 | |||
c4dde38d23 | |||
e14f0488c5 |
25
README.md
25
README.md
@@ -12,14 +12,27 @@ program. It's written in Go using the [Bubble Tea][tea] library, and uses
|
||||
[tea]: https://github.com/charmbracelet/bubbletea
|
||||
[coin]: https://www.coindesk.com
|
||||
|
||||
### Installation
|
||||
|
||||
Go to the [Releases page](https://code.humancabbage.net/sam/moonmath/releases)
|
||||
and download the archive for your operating system and architecture. (For the
|
||||
uninitiated, "Darwin" means macOS.)
|
||||
|
||||
### Configuration
|
||||
|
||||
By default, the program will use Bitcoin along with various goals and bases
|
||||
of comparison. With the `--asset` flag, another asset supported by Coindesk can
|
||||
be chosen. The [builtin default config](./config/default.yaml) only has special
|
||||
goals for Ethereum ("ETH"). With the `--config-file` flag, however, one can
|
||||
specify a YAML file that overrides these defaults and adds goals for other
|
||||
assets.
|
||||
By default, the program will use Bitcoin along with various goals and bases of
|
||||
comparison. With the `--asset` flag, another asset supported by Coindesk can be
|
||||
chosen. These can even be chained, e.g. `--asset BTC --asset ETH`, to show
|
||||
projections for multiple assets simultaneously.
|
||||
|
||||
The [builtin default config](./config/default.yaml) only has special
|
||||
goals a handful of the most popular assets. With the `--config-file` flag,
|
||||
however, one can specify a YAML file that overrides these defaults and adds
|
||||
goals for other assets.
|
||||
|
||||
Check out [coindesk/assets.go](./coindesk/assets.go) for a full list of
|
||||
supported assets. Keep in mind these have not been exhaustively tested, and
|
||||
it's likely that many will fail with the default configuration settings.
|
||||
|
||||
### "Theory"
|
||||
|
||||
|
@@ -49,7 +49,7 @@ type Data struct {
|
||||
RelativeBases []moon.RelativeBase `koanf:"relativeBases"`
|
||||
}
|
||||
|
||||
func (all All) GetData(asset coindesk.Asset) (data Data, err error) {
|
||||
func (all All) GetData(asset coindesk.Asset) (data Data) {
|
||||
data, ok := all.Assets[asset]
|
||||
if !ok {
|
||||
data = all.Defaults
|
||||
|
@@ -44,3 +44,88 @@ assets:
|
||||
value: 20000
|
||||
- name: $25k
|
||||
value: 25000
|
||||
LTC:
|
||||
goals:
|
||||
- name: $100
|
||||
value: 100
|
||||
- name: $150
|
||||
value: 150
|
||||
- name: $200
|
||||
value: 200
|
||||
- name: $300
|
||||
value: 300
|
||||
- name: $500
|
||||
value: 500
|
||||
- name: $1k
|
||||
value: 1000
|
||||
constantBases:
|
||||
- name: 2021-
|
||||
time: 2020-12-31T16:00:00-08:00
|
||||
SOL:
|
||||
goals:
|
||||
- name: $250
|
||||
value: 250
|
||||
- name: $375
|
||||
value: 375
|
||||
- name: $500
|
||||
value: 500
|
||||
- name: $750
|
||||
value: 750
|
||||
- name: $1k
|
||||
value: 1000
|
||||
- name: $2k
|
||||
value: 2000
|
||||
constantBases:
|
||||
- name: 2022-
|
||||
time: 2021-12-31T16:00:00-08:00
|
||||
XRP:
|
||||
goals:
|
||||
- name: $1
|
||||
value: 1
|
||||
- name: $1.5
|
||||
value: 1.5
|
||||
- name: $2
|
||||
value: 2
|
||||
- name: $3
|
||||
value: 3
|
||||
- name: $5
|
||||
value: 5
|
||||
- name: $10
|
||||
value: 10
|
||||
constantBases:
|
||||
- name: 2022-
|
||||
time: 2021-12-31T16:00:00-08:00
|
||||
DOGE:
|
||||
goals:
|
||||
- name: $1
|
||||
value: 1
|
||||
- name: $1.5
|
||||
value: 1.5
|
||||
- name: $2
|
||||
value: 2
|
||||
- name: $3
|
||||
value: 3
|
||||
- name: $5
|
||||
value: 5
|
||||
- name: $10
|
||||
value: 10
|
||||
constantBases:
|
||||
- name: 2022-
|
||||
time: 2021-12-31T16:00:00-08:00
|
||||
ADA:
|
||||
goals:
|
||||
- name: $1
|
||||
value: 1
|
||||
- name: $1.5
|
||||
value: 1.5
|
||||
- name: $2
|
||||
value: 2
|
||||
- name: $3
|
||||
value: 3
|
||||
- name: $5
|
||||
value: 5
|
||||
- name: $10
|
||||
value: 10
|
||||
constantBases:
|
||||
- name: 2022-
|
||||
time: 2021-12-31T16:00:00-08:00
|
||||
|
16
moon/moon.go
16
moon/moon.go
@@ -86,18 +86,18 @@ func (m *Math) Refresh(ctx context.Context) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(resp.Data.Entries) == 0 {
|
||||
c.Projections.Dates = nil
|
||||
return nil
|
||||
}
|
||||
c.StartingPrice = resp.Data.Entries[0].Price
|
||||
c.Gain = float64(m.CurrentPrice) / float64(c.StartingPrice)
|
||||
days := now.Sub(c.StartingDate).Hours() / 24
|
||||
c.CDPR = CDPR(days, c.Gain)
|
||||
if c.CDPR > 1 {
|
||||
c.Projections = ProjectDates(
|
||||
now, float64(m.CurrentPrice),
|
||||
c.CDPR, m.Goals,
|
||||
)
|
||||
} else {
|
||||
c.Projections.Dates = nil
|
||||
}
|
||||
c.Projections = ProjectDates(
|
||||
now, float64(m.CurrentPrice),
|
||||
c.CDPR, m.Goals,
|
||||
)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
56
moonmath.go
56
moonmath.go
@@ -2,21 +2,30 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||
"code.humancabbage.net/sam/moonmath/config"
|
||||
"code.humancabbage.net/sam/moonmath/tui"
|
||||
"github.com/alecthomas/kong"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
var CLI struct {
|
||||
Asset string `short:"a" default:"BTC" help:"Asset to project."`
|
||||
ConfigFile string `short:"c" help:"Path to YAML configuration file."`
|
||||
Asset []string `short:"a" default:"BTC" help:"Asset(s) to project."`
|
||||
ConfigFile string `short:"c" help:"Path to YAML configuration file."`
|
||||
Perf bool `help:"Display internal performance stats."`
|
||||
}
|
||||
|
||||
func main() {
|
||||
logFile := setupLogging()
|
||||
defer func() {
|
||||
_ = logFile.Close()
|
||||
}()
|
||||
ctx := kong.Parse(&CLI)
|
||||
if ctx.Error != nil {
|
||||
fail(ctx.Error)
|
||||
@@ -25,17 +34,50 @@ func main() {
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
CLI.Asset = strings.ToUpper(CLI.Asset)
|
||||
cfg, err := allCfg.GetData(coindesk.Asset(CLI.Asset))
|
||||
if err != nil {
|
||||
fail(err)
|
||||
var assets []coindesk.Asset
|
||||
for i := range CLI.Asset {
|
||||
asset := coindesk.Asset(strings.ToUpper(CLI.Asset[i]))
|
||||
assets = append(assets, asset)
|
||||
}
|
||||
p := tui.New(cfg)
|
||||
m := tui.New(assets, allCfg, CLI.Perf)
|
||||
p := tea.NewProgram(m,
|
||||
tea.WithAltScreen(),
|
||||
tea.WithFPS(30),
|
||||
)
|
||||
if _, err := p.Run(); err != nil {
|
||||
fail(err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupLogging() io.Closer {
|
||||
homePath, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
programConfigPath := filepath.Join(homePath, ".moonmath")
|
||||
err = os.MkdirAll(programConfigPath, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
errLogPath := filepath.Join(programConfigPath, "errors.log")
|
||||
errLogFile, err := os.OpenFile(
|
||||
errLogPath,
|
||||
os.O_CREATE|os.O_APPEND|os.O_WRONLY,
|
||||
0600,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
slog.SetDefault(slog.New(
|
||||
slog.NewTextHandler(errLogFile, &slog.HandlerOptions{
|
||||
Level: slog.LevelError,
|
||||
})))
|
||||
return errLogFile
|
||||
}
|
||||
|
||||
func fail(err error) {
|
||||
fmt.Printf("program error: %v\n", err)
|
||||
os.Exit(1)
|
||||
|
243
tui/asset/asset.go
Normal file
243
tui/asset/asset.go
Normal file
@@ -0,0 +1,243 @@
|
||||
package asset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"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
|
||||
}
|
||||
|
||||
func New(cfg config.Data) (m Model) {
|
||||
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
|
||||
m.indicator.Style = indicatorNormalStyle
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (m Model) Handles(a coindesk.Asset) bool {
|
||||
return m.math.Asset == a
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
return tea.Batch(
|
||||
m.indicator.Tick,
|
||||
func() tea.Msg {
|
||||
return Msg{m.math.Asset, refresh{}}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
m.indicator.Style = indicatorNormalStyle
|
||||
return m, tea.Batch(
|
||||
m.resumeIndicator,
|
||||
m.refresh,
|
||||
)
|
||||
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
|
||||
}
|
||||
case spinner.TickMsg:
|
||||
if !m.refreshing {
|
||||
return m, nil
|
||||
}
|
||||
var cmd tea.Cmd
|
||||
m.indicator, cmd = m.indicator.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
type refresh struct{}
|
||||
type update struct {
|
||||
math moon.Math
|
||||
err error
|
||||
}
|
||||
type stopIndicator struct{}
|
||||
|
||||
func (m Model) refresh() tea.Msg {
|
||||
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, update{m.math, err}}
|
||||
}
|
||||
|
||||
func (m Model) resumeIndicator() tea.Msg {
|
||||
return m.indicator.Tick()
|
||||
}
|
||||
|
||||
func (m Model) scheduleRefresh() tea.Cmd {
|
||||
return tea.Tick(time.Second*30,
|
||||
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.
|
||||
return tea.Tick(time.Millisecond*500,
|
||||
func(t time.Time) tea.Msg {
|
||||
return Msg{m.math.Asset, stopIndicator{}}
|
||||
})
|
||||
}
|
||||
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
|
||||
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"))
|
||||
|
||||
var indicatorNormalStyle = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("69"))
|
||||
|
||||
var indicatorErrorStyle = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("160"))
|
42
tui/perf/perf.go
Normal file
42
tui/perf/perf.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package perf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
updates *atomic.Int64
|
||||
views *atomic.Int64
|
||||
}
|
||||
|
||||
func New() (m Model) {
|
||||
m.updates = new(atomic.Int64)
|
||||
m.views = new(atomic.Int64)
|
||||
return
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) View() string {
|
||||
updates := m.updates.Load()
|
||||
views := m.views.Load()
|
||||
s := fmt.Sprintf("updates: %d\tviews: %d", updates, views)
|
||||
return s
|
||||
}
|
||||
|
||||
func (m Model) AddUpdate() {
|
||||
m.updates.Add(1)
|
||||
}
|
||||
|
||||
func (m Model) AddView() {
|
||||
m.views.Add(1)
|
||||
}
|
230
tui/tui.go
230
tui/tui.go
@@ -1,202 +1,100 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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"
|
||||
"code.humancabbage.net/sam/moonmath/tui/asset"
|
||||
"code.humancabbage.net/sam/moonmath/tui/perf"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
func New(cfg config.Data) (p *tea.Program) {
|
||||
p = tea.NewProgram(
|
||||
newModel(cfg),
|
||||
tea.WithAltScreen(),
|
||||
tea.WithFPS(30),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
math moon.Math
|
||||
|
||||
refreshing bool
|
||||
indicator spinner.Model
|
||||
|
||||
properties table.Model
|
||||
projections table.Model
|
||||
assets []asset.Model
|
||||
stats perf.Model
|
||||
displayStats bool
|
||||
}
|
||||
|
||||
func newModel(cfg config.Data) (m Model) {
|
||||
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)
|
||||
func New(assets []coindesk.Asset, cfg config.All, displayStats bool) (m Model) {
|
||||
m.stats = perf.New()
|
||||
m.displayStats = displayStats
|
||||
// construct models for each asset, but don't filter out dupes
|
||||
seen := map[coindesk.Asset]struct{}{}
|
||||
for _, a := range assets {
|
||||
_, ok := seen[a]
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
assetCfg := cfg.GetData(a)
|
||||
assetModel := asset.New(assetCfg)
|
||||
m.assets = append(m.assets, assetModel)
|
||||
seen[a] = struct{}{}
|
||||
}
|
||||
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
|
||||
m.indicator.Style = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("69"))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
return tea.Batch(
|
||||
m.indicator.Tick,
|
||||
func() tea.Msg {
|
||||
return refresh{}
|
||||
},
|
||||
)
|
||||
// initialize child models, collecting their commands,
|
||||
// then return them all in a batch
|
||||
var inits []tea.Cmd
|
||||
for i := range m.assets {
|
||||
cmd := m.assets[i].Init()
|
||||
inits = append(inits, cmd)
|
||||
}
|
||||
return tea.Batch(inits...)
|
||||
}
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.stats.AddUpdate()
|
||||
switch msg := msg.(type) {
|
||||
case refresh:
|
||||
m.refreshing = true
|
||||
return m, func() tea.Msg {
|
||||
// TODO: log errors
|
||||
_ = m.math.Refresh(context.TODO())
|
||||
return m.math
|
||||
}
|
||||
case moon.Math:
|
||||
m.math = msg
|
||||
refillProperties(&m)
|
||||
refillProjections(&m)
|
||||
return m, tea.Batch(
|
||||
// schedule the next refresh
|
||||
tea.Tick(time.Second*30,
|
||||
func(t time.Time) tea.Msg {
|
||||
return refresh{}
|
||||
}),
|
||||
// wait a bit to stop the indicator, so that it's more obvious
|
||||
// even when the refresh completes quickly.
|
||||
tea.Tick(time.Millisecond*500,
|
||||
func(t time.Time) tea.Msg {
|
||||
return stopIndicator{}
|
||||
}),
|
||||
)
|
||||
case stopIndicator:
|
||||
m.refreshing = false
|
||||
return m, nil
|
||||
case spinner.TickMsg:
|
||||
var cmd tea.Cmd
|
||||
m.indicator, cmd = m.indicator.Update(msg)
|
||||
return m, cmd
|
||||
// handle keys for quitting
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q", "esc":
|
||||
return m, tea.Quit
|
||||
}
|
||||
// forward asset messages to the appropriate model
|
||||
case asset.Msg:
|
||||
cmd := m.forward(msg.Asset, msg)
|
||||
return m, cmd
|
||||
// forward any other message to each child model.
|
||||
// typically, this is for animation.
|
||||
default:
|
||||
var commands []tea.Cmd
|
||||
for i := range m.assets {
|
||||
var cmd tea.Cmd
|
||||
m.assets[i], cmd = m.assets[i].Update(msg)
|
||||
commands = append(commands, cmd)
|
||||
}
|
||||
return m, tea.Batch(commands...)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
type refresh struct{}
|
||||
type stopIndicator struct{}
|
||||
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
|
||||
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()
|
||||
m.stats.AddView()
|
||||
var ss []string
|
||||
for i := range m.assets {
|
||||
s := m.assets[i].View()
|
||||
ss = append(ss, s)
|
||||
}
|
||||
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"
|
||||
if m.displayStats {
|
||||
ss = append(ss, m.stats.View())
|
||||
}
|
||||
r := lipgloss.JoinVertical(lipgloss.Center, ss...)
|
||||
return r
|
||||
}
|
||||
|
||||
var baseStyle = lipgloss.NewStyle().
|
||||
BorderStyle(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color("240"))
|
||||
func (m Model) forward(a coindesk.Asset, msg tea.Msg) (cmd tea.Cmd) {
|
||||
// O(n) is fine when n is small
|
||||
for i := range m.assets {
|
||||
if !m.assets[i].Handles(a) {
|
||||
continue
|
||||
}
|
||||
m.assets[i], cmd = m.assets[i].Update(msg)
|
||||
return
|
||||
}
|
||||
panic(fmt.Errorf("rogue message: %v", msg))
|
||||
}
|
||||
|
Reference in New Issue
Block a user