| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package tui
- import (
- "fmt"
- 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) Model {
- var model Model
- for _, address := range addresses {
- var addr Address
- addr.Address = address
- 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
- }
- // 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
- }
- func (m Model) View() string {
- output := "Results:\n"
- for _, address := range m.Addresses {
- last := address.Last()
- if last == -1 {
- output = output + fmt.Sprintf("%s: loading...\n", address.Address)
- } else {
- output = output + fmt.Sprintf("%s: %f\n", address.Address, last)
- }
- }
- return output
- }
|