Naming things is hard.
All checks were successful
Build & Test / Main (push) Successful in 2m26s

This commit is contained in:
Sam Fredrickson 2024-03-29 01:15:59 -07:00
parent 2d991880ce
commit 9e6abb1112
4 changed files with 48 additions and 52 deletions

View File

@ -16,62 +16,59 @@ import (
var k = koanf.New(".") var k = koanf.New(".")
func Load(path string) (all All, err error) { func Load(filePath string) (r Root, err error) {
err = k.Load(structs.Provider(Default, "koanf"), nil) err = k.Load(structs.Provider(Default, "koanf"), nil)
if err != nil { if err != nil {
return return
} }
if path != "" { if filePath != "" {
err = k.Load(file.Provider(path), yaml.Parser()) err = k.Load(file.Provider(filePath), yaml.Parser())
if err != nil { if err != nil {
return return
} }
} }
err = k.Unmarshal("", &all) err = k.Unmarshal("", &r)
if err != nil {
return
}
return return
} }
var Default All var Default Root
type All struct { type Root struct {
Defaults Data `koanf:"defaults"` Defaults Asset `koanf:"defaults"`
Assets map[coindesk.Asset]Data `koanf:"assets"` Assets map[coindesk.Asset]Asset `koanf:"assets"`
} }
type Data struct { type Asset struct {
Asset coindesk.Asset `koanf:"asset"` Asset coindesk.Asset `koanf:"asset"`
Goals []moon.Goal `koanf:"goals"` Goals []moon.Goal `koanf:"goals"`
ConstantBases []moon.ConstantBase `koanf:"constantBases"` ConstantBases []moon.ConstantBase `koanf:"constantBases"`
RelativeBases []moon.RelativeBase `koanf:"relativeBases"` RelativeBases []moon.RelativeBase `koanf:"relativeBases"`
} }
func (all All) GetData(asset coindesk.Asset) (data Data) { func (r Root) ForAsset(asset coindesk.Asset) (data Asset) {
data, ok := all.Assets[asset] data, ok := r.Assets[asset]
if !ok { if !ok {
data = all.Defaults data = r.Defaults
} }
if data.Asset == "" { if data.Asset == "" {
data.Asset = asset data.Asset = asset
} }
if data.Goals == nil || len(data.Goals) == 0 { if data.Goals == nil || len(data.Goals) == 0 {
data.Goals = all.Defaults.Goals data.Goals = r.Defaults.Goals
} }
if data.ConstantBases == nil || len(data.ConstantBases) == 0 { if data.ConstantBases == nil || len(data.ConstantBases) == 0 {
data.ConstantBases = all.Defaults.ConstantBases data.ConstantBases = r.Defaults.ConstantBases
} }
if data.RelativeBases == nil || len(data.RelativeBases) == 0 { if data.RelativeBases == nil || len(data.RelativeBases) == 0 {
data.RelativeBases = all.Defaults.RelativeBases data.RelativeBases = r.Defaults.RelativeBases
} }
return return
} }
// GetBases returns the concatenation of the constant and relative bases, sorted // GetBases returns the concatenation of the constant and relative bases, sorted
// from most recent to least recent in time. // from most recent to least recent in time.
func GetBases(d *Data) (bases []moon.Base) { func GetBases(d *Asset) (bases []moon.Base) {
for _, b := range d.ConstantBases { for _, b := range d.ConstantBases {
bases = append(bases, b) bases = append(bases, b)
} }

View File

@ -2,7 +2,6 @@ package moon
import ( import (
"context" "context"
"fmt"
"math" "math"
"time" "time"
@ -115,25 +114,6 @@ type Column struct {
Projections Projection Projections Projection
} }
func (c *Column) 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
}
var DefaultGoals = []Goal{ var DefaultGoals = []Goal{
{"$100k", 100000}, {"$100k", 100000},
{"$150k", 150000}, {"$150k", 150000},

View File

@ -30,7 +30,7 @@ type Msg struct {
inner tea.Msg inner tea.Msg
} }
func New(cfg config.Data) (m Model) { func New(cfg config.Asset) (m Model) {
m.math = moon.NewMath( m.math = moon.NewMath(
cfg.Asset, cfg.Asset,
cfg.Goals, cfg.Goals,
@ -89,10 +89,6 @@ func New(cfg config.Data) (m Model) {
return return
} }
func (m Model) Handles(a coindesk.Asset) bool {
return m.math.Asset == a
}
func (m Model) Init() tea.Cmd { func (m Model) Init() tea.Cmd {
return tea.Batch( return tea.Batch(
m.indicator.Tick, m.indicator.Tick,
@ -102,6 +98,10 @@ func (m Model) Init() tea.Cmd {
) )
} }
func (m Model) Handles(a coindesk.Asset) bool {
return m.math.Asset == a
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case Msg: case Msg:
@ -195,14 +195,34 @@ func refillProperties(m *Model) {
} }
func refillProjections(m *Model) { func refillProjections(m *Model) {
rows := []table.Row{m.math.Labels} cols := []table.Row{m.math.Labels}
for i := range m.math.Columns { for i := range m.math.Columns {
rows = append(rows, m.math.Columns[i].Column()) entries := renderEntries(m.math.Columns[i])
cols = append(cols, entries)
} }
rows = transpose(rows) rows := transpose(cols)
m.projections.SetRows(rows) m.projections.SetRows(rows)
} }
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
}
func transpose(slice []table.Row) []table.Row { func transpose(slice []table.Row) []table.Row {
xl := len(slice[0]) xl := len(slice[0])
yl := len(slice) yl := len(slice)

View File

@ -17,17 +17,16 @@ type Model struct {
displayStats bool displayStats bool
} }
func New(assets []coindesk.Asset, cfg config.All, displayStats bool) (m Model) { func New(assets []coindesk.Asset, cfg config.Root, displayStats bool) (m Model) {
m.stats = perf.New() m.stats = perf.New()
m.displayStats = displayStats m.displayStats = displayStats
// construct models for each asset, but don't filter out dupes // construct models for each asset, but remove dupes
seen := map[coindesk.Asset]struct{}{} seen := map[coindesk.Asset]struct{}{}
for _, a := range assets { for _, a := range assets {
_, ok := seen[a] if _, ok := seen[a]; ok {
if ok {
continue continue
} }
assetCfg := cfg.GetData(a) assetCfg := cfg.ForAsset(a)
assetModel := asset.New(assetCfg) assetModel := asset.New(assetCfg)
m.assets = append(m.assets, assetModel) m.assets = append(m.assets, assetModel)
seen[a] = struct{}{} seen[a] = struct{}{}