Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
7b445a02a2 | |||
97f4793ec3 | |||
270534c0d5 |
44
moonmath.go
44
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(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)
|
||||
@@ -30,12 +39,45 @@ func main() {
|
||||
asset := coindesk.Asset(strings.ToUpper(CLI.Asset[i]))
|
||||
assets = append(assets, asset)
|
||||
}
|
||||
p := tui.New(assets, allCfg)
|
||||
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)
|
||||
|
@@ -3,6 +3,7 @@ package asset
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||
@@ -83,8 +84,7 @@ func New(cfg config.Data) (m Model) {
|
||||
|
||||
m.indicator = spinner.New()
|
||||
m.indicator.Spinner = spinner.Points
|
||||
m.indicator.Style = lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("69"))
|
||||
m.indicator.Style = indicatorNormalStyle
|
||||
|
||||
return
|
||||
}
|
||||
@@ -108,33 +108,30 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
switch msg := msg.inner.(type) {
|
||||
case refresh:
|
||||
m.refreshing = true
|
||||
return m, func() tea.Msg {
|
||||
// TODO: log errors
|
||||
_ = m.math.Refresh(context.TODO())
|
||||
return Msg{m.math.Asset, m.math}
|
||||
}
|
||||
case moon.Math:
|
||||
m.math = msg
|
||||
refillProperties(&m)
|
||||
refillProjections(&m)
|
||||
m.indicator.Style = indicatorNormalStyle
|
||||
return m, tea.Batch(
|
||||
// schedule the next refresh
|
||||
tea.Tick(time.Second*30,
|
||||
func(t time.Time) tea.Msg {
|
||||
return Msg{m.math.Asset, 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 Msg{m.math.Asset, stopIndicator{}}
|
||||
}),
|
||||
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
|
||||
@@ -143,8 +140,47 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
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)},
|
||||
@@ -199,3 +235,9 @@ func (m Model) View() string {
|
||||
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)
|
||||
}
|
24
tui/tui.go
24
tui/tui.go
@@ -6,25 +6,20 @@ import (
|
||||
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||
"code.humancabbage.net/sam/moonmath/config"
|
||||
"code.humancabbage.net/sam/moonmath/tui/asset"
|
||||
"code.humancabbage.net/sam/moonmath/tui/perf"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
assets []asset.Model
|
||||
assets []asset.Model
|
||||
stats perf.Model
|
||||
displayStats bool
|
||||
}
|
||||
|
||||
func New(assets []coindesk.Asset, cfg config.All) (p *tea.Program) {
|
||||
model := newModel(assets, cfg)
|
||||
p = tea.NewProgram(
|
||||
model,
|
||||
tea.WithAltScreen(),
|
||||
tea.WithFPS(30),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
func newModel(assets []coindesk.Asset, cfg config.All) (m Model) {
|
||||
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 {
|
||||
@@ -52,6 +47,7 @@ func (m Model) Init() tea.Cmd {
|
||||
}
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.stats.AddUpdate()
|
||||
switch msg := msg.(type) {
|
||||
// handle keys for quitting
|
||||
case tea.KeyMsg:
|
||||
@@ -78,11 +74,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
func (m Model) View() string {
|
||||
m.stats.AddView()
|
||||
var ss []string
|
||||
for i := range m.assets {
|
||||
s := m.assets[i].View()
|
||||
ss = append(ss, s)
|
||||
}
|
||||
if m.displayStats {
|
||||
ss = append(ss, m.stats.View())
|
||||
}
|
||||
r := lipgloss.JoinVertical(lipgloss.Center, ss...)
|
||||
return r
|
||||
}
|
||||
|
Reference in New Issue
Block a user