| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package tui
- import (
- "fmt"
- "github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
- tea "github.com/charmbracelet/bubbletea"
- )
- // Bubbletea model
- //
- // BUG(state): how do declare pointer here?
- type Model struct {
- Addresses []Address // as defined in internal/tui/types.go
- }
- func InitialModel(addresses []string, max_results int) Model {
- var model Model
- for _, address := range addresses {
- var addr Address
- addr.Address = address
- addr.max_results = max_results
- model.Addresses = append(model.Addresses, addr)
- }
- return model
- }
- func (m Model) Init() tea.Cmd {
- return m.Poll
- }
- func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
- switch msg.(type) {
- // if case is KeyMsg (keypress)
- case tea.KeyMsg:
- return m, tea.Quit
- // switch msg.String() {
- // case "ctrl+c", "q": // These keys should exit the program.
- // return m, tea.Quit
- // }
- case pollMsg:
- m.Poll()
- }
- return m, m.Poll
- }
- func (m Model) View() string {
- output := "Results:\n\n"
- for _, address := range m.Addresses {
- last := address.Last()
- if last == -1 {
- output = output + fmt.Sprintf("- %s\tloading...\n\n", address.Address)
- } else {
- output = output + fmt.Sprintf("- %s\n\n", address.Address)
- }
- // Linechart
- slc := streamlinechart.New(address.max_results, 10)
- for _, v := range address.results {
- slc.Push(v)
- }
- slc.Draw()
- output = output + fmt.Sprintf("%s\n\n", slc.View())
- }
- return output
- }
- // A wrapper for the underlying [tui.Address.Poll] function. For each address in
- // [tui.Model.Addresses], run its respective Poll function and update [tui.Model]
- //
- // NOTE(async): this function fully blocks execution of the current thread.
- func (m Model) Poll() tea.Msg {
- for i, element := range m.Addresses {
- element.Poll()
- m.Addresses[i] = element
- }
- return true
- }
|