Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
7222c4e26b | |||
ba4e803eea | |||
ac1a4bc16c | |||
52508b7d4e | |||
4a40899653 |
11
README.md
11
README.md
@@ -1,6 +1,6 @@
|
||||
# moonmath
|
||||
|
||||
## Bullshit BTC Price Projections, Now in Your CLI!
|
||||
## Bullshit Crypto Price Projections, Now in Your CLI!
|
||||
|
||||

|
||||
|
||||
@@ -15,10 +15,11 @@ program. It's written in Go using the [Bubble Tea][tea] library, and uses
|
||||
### Configuration
|
||||
|
||||
By default, the program will use Bitcoin along with various goals and bases
|
||||
of comparison. With the `--config-file` flag, however, one can specify a TOML
|
||||
file that overrides these defaults.
|
||||
|
||||
See [this one for Ethereum](./config/eth.toml) for an example.
|
||||
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.
|
||||
|
||||
### "Theory"
|
||||
|
||||
|
@@ -21,3 +21,24 @@ func TestUnmarshalGetTickerResponse(t *testing.T) {
|
||||
|
||||
//go:embed get_ticker.json
|
||||
var getTickerJson []byte
|
||||
|
||||
func TestUnmarshalWebhookMessages(t *testing.T) {
|
||||
var msgs []bitcoinity.Message
|
||||
err := json.Unmarshal(capturedJson, &msgs)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal webhook messages JSON: %v", err)
|
||||
}
|
||||
for _, msg := range msgs {
|
||||
if msg.Event != "new_msg" {
|
||||
continue
|
||||
}
|
||||
var payload bitcoinity.MarketPayload
|
||||
err := json.Unmarshal(msg.Payload, &payload)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal market payload JSON: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed captured.json
|
||||
var capturedJson []byte
|
||||
|
@@ -1,31 +1,33 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||
"code.humancabbage.net/sam/moonmath/moon"
|
||||
"github.com/knadh/koanf/parsers/toml"
|
||||
"github.com/knadh/koanf/parsers/yaml"
|
||||
"github.com/knadh/koanf/providers/file"
|
||||
"github.com/knadh/koanf/providers/rawbytes"
|
||||
"github.com/knadh/koanf/providers/structs"
|
||||
"github.com/knadh/koanf/v2"
|
||||
)
|
||||
|
||||
var k = koanf.New(".")
|
||||
|
||||
func Load(path string) (data Data, err error) {
|
||||
func Load(path string) (all All, err error) {
|
||||
err = k.Load(structs.Provider(Default, "koanf"), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if path != "" {
|
||||
err = k.Load(file.Provider(path), toml.Parser())
|
||||
err = k.Load(file.Provider(path), yaml.Parser())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
err = k.Unmarshal("", &data)
|
||||
err = k.Unmarshal("", &all)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -33,11 +35,11 @@ func Load(path string) (data Data, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
var Default = Data{
|
||||
Asset: coindesk.BTC,
|
||||
Goals: moon.DefaultGoals,
|
||||
ConstantBases: moon.DefaultConstantBases,
|
||||
RelativeBases: moon.DefaultRelativeBases,
|
||||
var Default All
|
||||
|
||||
type All struct {
|
||||
Defaults Data `koanf:"defaults"`
|
||||
Assets map[coindesk.Asset]Data `koanf:"assets"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
@@ -47,9 +49,24 @@ type Data struct {
|
||||
RelativeBases []moon.RelativeBase `koanf:"relativeBases"`
|
||||
}
|
||||
|
||||
type Goal struct {
|
||||
Name string `koanf:"name"`
|
||||
Value float64 `koanf:"value"`
|
||||
func (all All) GetData(asset coindesk.Asset) (data Data, err error) {
|
||||
data, ok := all.Assets[asset]
|
||||
if !ok {
|
||||
data = all.Defaults
|
||||
}
|
||||
if data.Asset == "" {
|
||||
data.Asset = asset
|
||||
}
|
||||
if data.Goals == nil || len(data.Goals) == 0 {
|
||||
data.Goals = all.Defaults.Goals
|
||||
}
|
||||
if data.ConstantBases == nil || len(data.ConstantBases) == 0 {
|
||||
data.ConstantBases = all.Defaults.ConstantBases
|
||||
}
|
||||
if data.RelativeBases == nil || len(data.RelativeBases) == 0 {
|
||||
data.RelativeBases = all.Defaults.RelativeBases
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetBases returns the concatenation of the constant and relative bases, sorted
|
||||
@@ -77,3 +94,18 @@ func GetBases(d *Data) (bases []moon.Base) {
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
//go:embed default.yaml
|
||||
var defaultYamlBytes []byte
|
||||
|
||||
func init() {
|
||||
var k = koanf.New(".")
|
||||
err := k.Load(rawbytes.Provider(defaultYamlBytes), yaml.Parser())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = k.Unmarshal("", &Default)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
10
config/config_test.go
Normal file
10
config/config_test.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
// config.Default is parsed in config.init(), which panics on error.
|
||||
// so just getting this far means that at least it parsed successfully.
|
||||
}
|
46
config/default.yaml
Normal file
46
config/default.yaml
Normal file
@@ -0,0 +1,46 @@
|
||||
defaults:
|
||||
goals:
|
||||
- name: $100k
|
||||
value: 100000
|
||||
- name: $150k
|
||||
value: 150000
|
||||
- name: $200k
|
||||
value: 200000
|
||||
- name: $250k
|
||||
value: 250000
|
||||
- name: $300k
|
||||
value: 300000
|
||||
- name: $500k
|
||||
value: 500000
|
||||
- name: $1m
|
||||
value: 1000000
|
||||
relativeBases:
|
||||
- name: Month
|
||||
offset: -720h0m0s
|
||||
- name: Quarter
|
||||
offset: -2160h0m0s
|
||||
- name: Half-Year
|
||||
offset: -4368h0m0s
|
||||
- name: Year
|
||||
offset: -8760h0m0s
|
||||
constantBases:
|
||||
- name: 2020-
|
||||
time: 2019-12-31T16:00:00-08:00
|
||||
- name: 2017-
|
||||
time: 2016-12-31T16:00:00-08:00
|
||||
|
||||
assets:
|
||||
ETH:
|
||||
goals:
|
||||
- name: $5k
|
||||
value: 5000
|
||||
- name: $7.5k
|
||||
value: 7500
|
||||
- name: $10k
|
||||
value: 10000
|
||||
- name: $15k
|
||||
value: 15000
|
||||
- name: $20k
|
||||
value: 20000
|
||||
- name: $25k
|
||||
value: 25000
|
@@ -1,25 +0,0 @@
|
||||
asset = "ETH"
|
||||
|
||||
[[goals]]
|
||||
name = "$5k"
|
||||
value = 5000
|
||||
|
||||
[[goals]]
|
||||
name = "$7.5k"
|
||||
value = 7500
|
||||
|
||||
[[goals]]
|
||||
name = "$10k"
|
||||
value = 10000
|
||||
|
||||
[[goals]]
|
||||
name = "$15k"
|
||||
value = 15000
|
||||
|
||||
[[goals]]
|
||||
name = "$20k"
|
||||
value = 15000
|
||||
|
||||
[[goals]]
|
||||
name = "$25k"
|
||||
value = 25000
|
14
go.mod
14
go.mod
@@ -3,26 +3,28 @@ module code.humancabbage.net/sam/moonmath
|
||||
go 1.22.1
|
||||
|
||||
require (
|
||||
github.com/alecthomas/kong v0.9.0
|
||||
github.com/carlmjohnson/requests v0.23.5
|
||||
github.com/charmbracelet/bubbles v0.18.0
|
||||
github.com/charmbracelet/bubbletea v0.25.0
|
||||
github.com/charmbracelet/lipgloss v0.10.0
|
||||
github.com/knadh/koanf/parsers/yaml v0.1.0
|
||||
github.com/knadh/koanf/providers/file v0.1.0
|
||||
github.com/knadh/koanf/providers/rawbytes v0.1.0
|
||||
github.com/knadh/koanf/providers/structs v0.1.0
|
||||
github.com/knadh/koanf/v2 v2.1.0
|
||||
github.com/sourcegraph/conc v0.3.0
|
||||
golang.org/x/net v0.22.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alecthomas/kong v0.9.0 // indirect
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/containerd/console v1.0.4 // indirect
|
||||
github.com/fatih/structs v1.1.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
|
||||
github.com/knadh/koanf/maps v0.1.1 // indirect
|
||||
github.com/knadh/koanf/parsers/toml v0.1.0 // indirect
|
||||
github.com/knadh/koanf/providers/file v0.1.0 // indirect
|
||||
github.com/knadh/koanf/providers/structs v0.1.0 // indirect
|
||||
github.com/knadh/koanf/v2 v2.1.0 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||
@@ -33,7 +35,6 @@ require (
|
||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||
github.com/muesli/reflow v0.3.0 // indirect
|
||||
github.com/muesli/termenv v0.15.2 // indirect
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
@@ -41,4 +42,5 @@ require (
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
golang.org/x/term v0.18.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
24
go.sum
24
go.sum
@@ -1,5 +1,9 @@
|
||||
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
|
||||
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
|
||||
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
|
||||
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||
github.com/carlmjohnson/requests v0.23.5 h1:NPANcAofwwSuC6SIMwlgmHry2V3pLrSqRiSBKYbNHHA=
|
||||
@@ -12,6 +16,7 @@ github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMt
|
||||
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
|
||||
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
|
||||
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -21,16 +26,24 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
|
||||
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
|
||||
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
|
||||
github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6ODgOub/6LCI=
|
||||
github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18=
|
||||
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
|
||||
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
|
||||
github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c=
|
||||
github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA=
|
||||
github.com/knadh/koanf/providers/rawbytes v0.1.0 h1:dpzgu2KO6uf6oCb4aP05KDmKmAmI51k5pe8RYKQ0qME=
|
||||
github.com/knadh/koanf/providers/rawbytes v0.1.0/go.mod h1:mMTB1/IcJ/yE++A2iEZbY1MLygX7vttU+C+S/YmPu9c=
|
||||
github.com/knadh/koanf/providers/structs v0.1.0 h1:wJRteCNn1qvLtE5h8KQBvLJovidSdntfdyIbbCzEyE0=
|
||||
github.com/knadh/koanf/providers/structs v0.1.0/go.mod h1:sw2YZ3txUcqA3Z27gPlmmBzWn1h8Nt9O6EP/91MkcWE=
|
||||
github.com/knadh/koanf/v2 v2.1.0 h1:eh4QmHHBuU8BybfIJ8mB8K8gsGCD/AUQTdwGq/GzId8=
|
||||
github.com/knadh/koanf/v2 v2.1.0/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
@@ -52,14 +65,14 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
||||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
||||
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
|
||||
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
|
||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
@@ -82,5 +95,8 @@ golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
|
||||
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@@ -146,8 +146,6 @@ var DefaultGoals = []Goal{
|
||||
|
||||
var DefaultConstantBases = []ConstantBase{
|
||||
{"2020-", time.Unix(1577836800, 0)},
|
||||
{"2019-", time.Unix(1546300800, 0)},
|
||||
{"2018-", time.Unix(1514764800, 0)},
|
||||
{"2017-", time.Unix(1483228800, 0)},
|
||||
}
|
||||
|
||||
|
15
moonmath.go
15
moonmath.go
@@ -3,15 +3,17 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"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 {
|
||||
ConfigFile string `help:"Path to TOML configuration file."`
|
||||
Asset string `short:"a" default:"BTC" help:"Asset to project."`
|
||||
ConfigFile string `short:"c" help:"Path to YAML configuration file."`
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -19,11 +21,16 @@ func main() {
|
||||
if ctx.Error != nil {
|
||||
fail(ctx.Error)
|
||||
}
|
||||
cfg, err := config.Load(CLI.ConfigFile)
|
||||
allCfg, err := config.Load(CLI.ConfigFile)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
p := tea.NewProgram(tui.New(cfg))
|
||||
CLI.Asset = strings.ToUpper(CLI.Asset)
|
||||
cfg, err := allCfg.GetData(coindesk.Asset(CLI.Asset))
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
p := tui.New(cfg)
|
||||
if _, err := p.Run(); err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
BIN
screenshot.png
BIN
screenshot.png
Binary file not shown.
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 38 KiB |
120
tui/tui.go
120
tui/tui.go
@@ -13,60 +13,83 @@ import (
|
||||
"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
|
||||
|
||||
reloading bool
|
||||
indicator spinner.Model
|
||||
refreshing bool
|
||||
indicator spinner.Model
|
||||
|
||||
prices table.Model
|
||||
properties table.Model
|
||||
projections table.Model
|
||||
}
|
||||
|
||||
func New(cfg config.Data) Model {
|
||||
math := moon.NewMath(
|
||||
func newModel(cfg config.Data) (m Model) {
|
||||
m.math = moon.NewMath(
|
||||
cfg.Asset,
|
||||
cfg.Goals,
|
||||
config.GetBases(&cfg))
|
||||
config.GetBases(&cfg),
|
||||
)
|
||||
|
||||
tableStyle := table.DefaultStyles()
|
||||
tableStyle.Selected = lipgloss.NewStyle()
|
||||
prices := table.New(
|
||||
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: "Asset", Width: 6},
|
||||
{Title: "Price", Width: 9},
|
||||
{Title: "Property", Width: 9},
|
||||
{Title: "Value", Width: 9},
|
||||
}),
|
||||
table.WithHeight(1),
|
||||
table.WithHeight(2),
|
||||
table.WithStyles(tableStyle),
|
||||
)
|
||||
|
||||
projectionCols := []table.Column{
|
||||
{Title: "Labels", Width: 8},
|
||||
// projections table
|
||||
|
||||
labelsWidth := 0
|
||||
for _, l := range m.math.Labels {
|
||||
if len(l) > labelsWidth {
|
||||
labelsWidth = len(l)
|
||||
}
|
||||
}
|
||||
for i := range math.Columns {
|
||||
projectionCols := []table.Column{
|
||||
{Title: "Labels", Width: labelsWidth},
|
||||
}
|
||||
for _, c := range m.math.Columns {
|
||||
projectionCols = append(projectionCols,
|
||||
table.Column{
|
||||
Title: math.Columns[i].Base.Label(),
|
||||
Title: c.Base.Label(),
|
||||
Width: 10,
|
||||
})
|
||||
}
|
||||
projections := table.New(
|
||||
m.projections = table.New(
|
||||
table.WithColumns(projectionCols),
|
||||
table.WithHeight(len(math.Labels)),
|
||||
table.WithHeight(len(m.math.Labels)),
|
||||
table.WithStyles(tableStyle),
|
||||
)
|
||||
|
||||
indicator := spinner.New()
|
||||
indicator.Spinner = spinner.Points
|
||||
indicator.Style = lipgloss.NewStyle().
|
||||
// indicator spinner
|
||||
|
||||
m.indicator = spinner.New()
|
||||
m.indicator.Spinner = spinner.Points
|
||||
m.indicator.Style = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("69"))
|
||||
|
||||
return Model{
|
||||
math: math,
|
||||
indicator: indicator,
|
||||
prices: prices,
|
||||
projections: projections,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
@@ -81,20 +104,32 @@ func (m Model) Init() tea.Cmd {
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case refresh:
|
||||
m.reloading = true
|
||||
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
|
||||
m.reloading = false
|
||||
refillPrice(&m)
|
||||
refillProperties(&m)
|
||||
refillProjections(&m)
|
||||
return m, tea.Tick(time.Second*30,
|
||||
func(t time.Time) tea.Msg {
|
||||
return refresh{}
|
||||
})
|
||||
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)
|
||||
@@ -109,15 +144,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
type refresh struct{}
|
||||
type stopIndicator struct{}
|
||||
|
||||
func refillPrice(m *Model) {
|
||||
func refillProperties(m *Model) {
|
||||
rows := []table.Row{
|
||||
[]string{
|
||||
string(m.math.Asset),
|
||||
fmt.Sprintf("$%0.2f", m.math.CurrentPrice),
|
||||
},
|
||||
{"Asset", string(m.math.Asset)},
|
||||
{"Price", fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
|
||||
}
|
||||
m.prices.SetRows(rows)
|
||||
m.properties.SetRows(rows)
|
||||
}
|
||||
|
||||
func refillProjections(m *Model) {
|
||||
@@ -147,16 +181,16 @@ func transpose(slice []table.Row) []table.Row {
|
||||
func (m Model) View() string {
|
||||
var s string
|
||||
indicator := ""
|
||||
if m.reloading {
|
||||
if m.refreshing {
|
||||
indicator = m.indicator.View()
|
||||
}
|
||||
right := lipgloss.JoinVertical(
|
||||
lipgloss.Center,
|
||||
baseStyle.Render(m.prices.View()),
|
||||
baseStyle.Render(m.properties.View()),
|
||||
indicator,
|
||||
)
|
||||
s += lipgloss.JoinHorizontal(
|
||||
lipgloss.Top,
|
||||
lipgloss.Center,
|
||||
right,
|
||||
baseStyle.Render(m.projections.View()),
|
||||
)
|
||||
@@ -164,5 +198,5 @@ func (m Model) View() string {
|
||||
}
|
||||
|
||||
var baseStyle = lipgloss.NewStyle().
|
||||
BorderStyle(lipgloss.NormalBorder()).
|
||||
BorderStyle(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color("240"))
|
||||
|
Reference in New Issue
Block a user