|
@@ -1,6 +1,8 @@
|
|
|
package tui
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -8,7 +10,7 @@ import (
|
|
|
//
|
|
//
|
|
|
// BUG(state): how do declare pointer here?
|
|
// BUG(state): how do declare pointer here?
|
|
|
type Model struct {
|
|
type Model struct {
|
|
|
- Addresses []Address // as defined in [internal/tui/types.go]
|
|
|
|
|
|
|
+ Addresses []Address // as defined in internal/tui/types.go
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func InitialModel(addresses []string) Model {
|
|
func InitialModel(addresses []string) Model {
|
|
@@ -21,14 +23,47 @@ func InitialModel(addresses []string) Model {
|
|
|
return model
|
|
return model
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (m *Model) Init() tea.Cmd {
|
|
|
|
|
- return nil
|
|
|
|
|
|
|
+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 {
|
|
|
|
|
- return ""
|
|
|
|
|
|
|
+// 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) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
|
- return m, nil
|
|
|
|
|
|
|
+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
|
|
|
}
|
|
}
|