Compare commits

..

No commits in common. "bf50ba45398e310f2389482bf5cc9b0c2f515014" and "83fd89446d9369c5dca82ae724c65c6f028be9c2" have entirely different histories.

6 changed files with 161 additions and 173 deletions

View File

@ -1 +0,0 @@
{"statusCode":200,"message":"OK","data":{"BTC":{"iso":"BTC","name":"Bitcoin","slug":"bitcoin","change":{"percent":-6.3,"value":-4187.634882541},"ohlc":{"o":66436.26558132548,"h":66436.26558132548,"l":61526.78744328402,"c":62248.630698784444},"circulatingSupply":19658987,"marketCap":1223745021675.2043,"ts":1710898259000,"src":"tb"}}}

29
coindesk/lib_test.go Normal file
View File

@ -0,0 +1,29 @@
package coindesk_test
import (
"context"
"fmt"
"testing"
"time"
"code.humancabbage.net/sam/moonmath/coindesk"
)
func TestXxx(t *testing.T) {
now := time.Now()
then := now.Add(time.Duration(-24) * time.Hour)
values, err := coindesk.GetPriceValues(context.Background(), coindesk.BTC, then, now)
if err != nil {
t.Errorf("test failure: %v", err)
}
_ = values
fmt.Println()
tickers, err := coindesk.GetAssetTickers(context.Background(), coindesk.BTC, coindesk.ETH)
if err != nil {
t.Errorf("test failure: %v", err)
}
_ = tickers
fmt.Println()
}

View File

@ -1,31 +0,0 @@
package coindesk_test
import (
_ "embed"
"encoding/json"
"testing"
"code.humancabbage.net/sam/moonmath/coindesk"
)
func TestUnmarshalPriceValues(t *testing.T) {
testUnmarshalResponse[coindesk.PriceValues](t, priceValuesJson)
}
func TestUnmarshalAssetTickers(t *testing.T) {
testUnmarshalResponse[coindesk.AssetTickers](t, assetTickersJson)
}
func testUnmarshalResponse[T any](t *testing.T, jsonBytes []byte) {
var resp coindesk.Response[T]
err := json.Unmarshal(jsonBytes, &resp)
if err != nil {
t.Errorf("failed to unmarshal JSON: %v", err)
}
}
//go:embed price_values.json
var priceValuesJson []byte
//go:embed asset_tickers.json
var assetTickersJson []byte

View File

@ -1 +0,0 @@
{"statusCode":200,"message":"OK","data":{"iso":"BTC","name":"Bitcoin","slug":"bitcoin","ingestionStart":"2014-11-03","interval":"15m","src":"tb","entries":[[1710548099000,69784.6599201257],[1710548999000,69907.6435505413],[1710549899000,69735.0244223474],[1710550799000,69608.3777481736],[1710551699000,69643.5872644923],[1710552599000,69389.4883714119],[1710553499000,69350.2996335944],[1710554399000,69241.2719800575],[1710555299000,69110.5930246779],[1710556199000,69046.5706710773],[1710557099000,69209.8770692447],[1710557999000,69069.0204183649],[1710558899000,69317.6327892521],[1710559799000,69175.2057532632],[1710560699000,69237.706076427],[1710561599000,69206.0448006493],[1710562499000,69077.8264056849],[1710563399000,69161.0784263752],[1710564299000,68903.1424009851],[1710565199000,68994.5234182921],[1710566099000,68754.9875193438],[1710566999000,68784.6200740683],[1710567899000,69002.1375650817],[1710568799000,68975.0878307215],[1710569699000,69045.2310776643],[1710570599000,69025.7239355934],[1710571499000,69068.3988090268],[1710572399000,69185.2444741912],[1710573299000,69246.5439171529],[1710574199000,69278.2574941136],[1710575099000,69407.4780576693],[1710575999000,69406.4527711537],[1710576899000,69296.4923727422],[1710577799000,69165.948416751],[1710578699000,69258.7912250956],[1710579599000,69017.1102984366],[1710580499000,69014.4480481396],[1710581399000,69099.3096696255],[1710582299000,69011.246072562],[1710583199000,68942.5343294262],[1710584099000,68697.8235476743],[1710584999000,68597.1119491188],[1710585899000,68532.2931864276],[1710586799000,68420.1712986066],[1710587699000,68354.8847177761],[1710588599000,67949.5219527423],[1710589499000,68327.2101380853],[1710590399000,68260.6576285653],[1710591299000,68192.7968537126],[1710592199000,68302.103987459],[1710593099000,68096.4362930929],[1710593999000,67906.8863092216],[1710594899000,67982.183818933],[1710595799000,68073.3862259585],[1710596699000,67891.8144823227],[1710597599000,67706.923652975],[1710598499000,67942.4174766702],[1710599399000,67848.3672161555],[1710600299000,67975.9404080798],[1710601199000,68177.0804610881],[1710602099000,67994.5903423281],[1710602999000,68024.8507494152],[1710603899000,68201.8594048759],[1710604799000,68358.4188016011],[1710605699000,68239.5960888028],[1710606599000,68224.3438496054],[1710607499000,68293.736596961],[1710608399000,68158.4057208716],[1710609299000,68114.4234280406],[1710610199000,67829.2127954109],[1710611099000,67630.8190396486],[1710611999000,67220.3085172518],[1710612899000,67043.4786652409],[1710613799000,66906.1713676385],[1710614699000,66871.2079622553],[1710615599000,67015.8311895788],[1710616499000,67458.766887969],[1710617399000,67493.9852796663],[1710618299000,67354.2332686561],[1710619199000,66916.4704143146],[1710620099000,66995.7196236024],[1710620999000,67046.4965828003],[1710621899000,67402.0503271179],[1710622799000,67087.3064846683],[1710623699000,66943.270112864],[1710624599000,66268.8656111216],[1710625499000,66290.9133774055],[1710626399000,66189.3483322972],[1710627299000,66073.8513767399],[1710628199000,66433.8329094007],[1710629099000,66549.3543050487],[1710629999000,66331.1108451565],[1710630899000,65376.9600942977],[1710631799000,65280.3933464042],[1710632699000,65598.1187015513],[1710633599000,65261.5180497119],[1710634499000,65780.8283123471]]}}

View File

@ -1,15 +1,145 @@
package main
import (
"context"
"fmt"
"os"
"time"
"code.humancabbage.net/sam/moonmath/tui"
"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
}
func (m model) Init() tea.Cmd {
return func() tea.Msg {
_ = m.math.Refresh(context.TODO())
return m.math
}
}
func initialModel() model {
math := moon.NewMath(nil, nil)
tableStyle := table.DefaultStyles()
tableStyle.Selected = lipgloss.NewStyle()
prices := table.New(
table.WithColumns([]table.Column{
{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{
Title: math.Columns[i].Base.Name(),
Width: 10,
})
}
projectionRows := make([]table.Row, len(math.Goals)+2)
for i := range projectionRows {
projectionRows[i] = make(table.Row, len(projectionCols))
}
projectionRows[0][0] = "Starting"
projectionRows[1][0] = "CDPR"
for i := range math.Goals {
projectionRows[i+2][0] = fmt.Sprintf("$%.0f", math.Goals[i])
}
projections := table.New(
table.WithColumns(projectionCols),
table.WithRows(projectionRows),
table.WithHeight(len(math.Goals)+2),
table.WithStyles(tableStyle),
)
return model{
math: math,
prices: prices,
projections: projections,
}
}
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{
[]string{fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
}
m.prices.SetRows(rows)
}
func refillProjections(m *model) {
rows := m.projections.Rows()
for col := range m.math.Columns {
_ = col
never := false
if m.math.Columns[col].CDPR <= 1 {
never = true
}
rows[0][col+1] = fmt.Sprintf("$%.2f", m.math.Columns[col].StartingPrice)
rows[1][col+1] = fmt.Sprintf("%.2f%%", (m.math.Columns[col].CDPR-1)*100)
for row := 0; row < len(m.math.Goals); row++ {
var cell string
if never {
cell = "NEVER!!!!!"
} else {
cell = m.math.Columns[col].
Projections.Dates[row].
Format("2006-01-02")
}
rows[row+2][col+1] = cell
}
}
m.projections.SetRows(rows)
}
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"
}
func main() {
p := tea.NewProgram(tui.New())
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Printf("program error: %v\n", err)
os.Exit(1)

View File

@ -1,138 +0,0 @@
package tui
import (
"context"
"fmt"
"time"
"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
}
func New() Model {
math := moon.NewMath(nil, nil)
tableStyle := table.DefaultStyles()
tableStyle.Selected = lipgloss.NewStyle()
prices := table.New(
table.WithColumns([]table.Column{
{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{
Title: math.Columns[i].Base.Name(),
Width: 10,
})
}
projectionRows := make([]table.Row, len(math.Goals)+2)
for i := range projectionRows {
projectionRows[i] = make(table.Row, len(projectionCols))
}
projectionRows[0][0] = "Starting"
projectionRows[1][0] = "CDPR"
for i := range math.Goals {
projectionRows[i+2][0] = fmt.Sprintf("$%.0f", math.Goals[i])
}
projections := table.New(
table.WithColumns(projectionCols),
table.WithRows(projectionRows),
table.WithHeight(len(math.Goals)+2),
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{
[]string{fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
}
m.prices.SetRows(rows)
}
func refillProjections(m *Model) {
rows := m.projections.Rows()
for col := range m.math.Columns {
_ = col
never := false
if m.math.Columns[col].CDPR <= 1 {
never = true
}
rows[0][col+1] = fmt.Sprintf("$%.2f", m.math.Columns[col].StartingPrice)
rows[1][col+1] = fmt.Sprintf("%.2f%%", (m.math.Columns[col].CDPR-1)*100)
for row := 0; row < len(m.math.Goals); row++ {
var cell string
if never {
cell = "NEVER!!!!!"
} else {
cell = m.math.Columns[col].
Projections.Dates[row].
Format("2006-01-02")
}
rows[row+2][col+1] = cell
}
}
m.projections.SetRows(rows)
}
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"
}