diff --git a/config/config.go b/config/config.go index a597e64..373c5e6 100644 --- a/config/config.go +++ b/config/config.go @@ -46,26 +46,25 @@ type Asset struct { RelativeBases []moon.RelativeBase `koanf:"relativeBases"` } -func (r Root) ForAsset(asset coindesk.Asset) (data Asset) { - data, ok := r.Assets[asset] - if !ok { - data = r.Defaults - } - if data.Asset == "" { - data.Asset = asset - } - if data.Goals == nil || len(data.Goals) == 0 { - data.Goals = r.Defaults.Goals - } - if data.ConstantBases == nil || len(data.ConstantBases) == 0 { - data.ConstantBases = r.Defaults.ConstantBases - } - if data.RelativeBases == nil || len(data.RelativeBases) == 0 { - data.RelativeBases = r.Defaults.RelativeBases - } +func (r Root) ForAsset(a coindesk.Asset) (cfg Asset) { + cfg = merge(r.Assets[a], r.Defaults) + cfg.Asset = a return } +func merge(dst, src Asset) Asset { + if len(dst.Goals) == 0 { + dst.Goals = src.Goals + } + if len(dst.ConstantBases) == 0 { + dst.ConstantBases = src.ConstantBases + } + if len(dst.RelativeBases) == 0 { + dst.RelativeBases = src.RelativeBases + } + return dst +} + // GetBases returns the concatenation of the constant and relative bases, sorted // from most recent to least recent in time. func GetBases(d *Asset) (bases []moon.Base) { diff --git a/tui/asset/asset.go b/tui/asset/asset.go index 68b320e..0243312 100644 --- a/tui/asset/asset.go +++ b/tui/asset/asset.go @@ -148,9 +148,9 @@ type update struct { type stopIndicator struct{} func (m Model) refresh() tea.Msg { - ctx, cancel := context.WithDeadline( + ctx, cancel := context.WithTimeout( context.Background(), - time.Now().Add(refreshDeadline)) + refreshTimeout) defer cancel() err := m.math.Refresh(ctx) if err != nil { @@ -183,7 +183,7 @@ func (m Model) stopIndicator() tea.Cmd { } var refreshInterval = time.Second * 30 -var refreshDeadline = time.Second * 15 +var refreshTimeout = time.Second * 15 var stopIndicatorDelay = time.Millisecond * 500 func refillProperties(m *Model) {