|
|
@@ -5,21 +5,23 @@ import (
|
|
|
|
|
|
"github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
+ "github.com/charmbracelet/lipgloss"
|
|
|
)
|
|
|
|
|
|
// Bubbletea model
|
|
|
//
|
|
|
// BUG(state): how do declare pointer here?
|
|
|
type Model struct {
|
|
|
+ width int
|
|
|
Addresses []Address // as defined in internal/tui/types.go
|
|
|
}
|
|
|
|
|
|
-func InitialModel(addresses []string, max_results int) Model {
|
|
|
+func InitialModel(addresses []string) Model {
|
|
|
var model Model
|
|
|
for _, address := range addresses {
|
|
|
var addr Address
|
|
|
+ addr.max_results = 10
|
|
|
addr.Address = address
|
|
|
- addr.max_results = max_results
|
|
|
model.Addresses = append(model.Addresses, addr)
|
|
|
}
|
|
|
return model
|
|
|
@@ -30,15 +32,18 @@ func (m Model) Init() tea.Cmd {
|
|
|
}
|
|
|
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
- switch msg.(type) {
|
|
|
+ switch msg := msg.(type) {
|
|
|
// if case is KeyMsg (keypress)
|
|
|
+ case tea.WindowSizeMsg:
|
|
|
+ m.width = msg.Width
|
|
|
+ for i, address := range m.Addresses {
|
|
|
+ address.max_results = m.width
|
|
|
+ m.Addresses[i] = address
|
|
|
+ }
|
|
|
+
|
|
|
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()
|
|
|
}
|
|
|
@@ -46,22 +51,30 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
}
|
|
|
|
|
|
func (m Model) View() string {
|
|
|
+ var headerStyle = lipgloss.NewStyle().
|
|
|
+ Bold(true).
|
|
|
+ Italic(true)
|
|
|
+
|
|
|
+ var blockStyle = lipgloss.NewStyle().
|
|
|
+ Width(m.width).
|
|
|
+ Align(lipgloss.Center)
|
|
|
+
|
|
|
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)
|
|
|
+
|
|
|
+ if len(address.results) == 0 {
|
|
|
+ output = output + fmt.Sprintf("%s\tloading...\n\n", headerStyle.Render(address.Address))
|
|
|
} else {
|
|
|
- output = output + fmt.Sprintf("- %s\n\n", address.Address)
|
|
|
- }
|
|
|
+ output = output + fmt.Sprintf("%s\n\n", blockStyle.Render(headerStyle.Render(address.Address)))
|
|
|
|
|
|
- // Linechart
|
|
|
- slc := streamlinechart.New(address.max_results, 10)
|
|
|
- for _, v := range address.results {
|
|
|
- slc.Push(v)
|
|
|
+ // Linechart
|
|
|
+ slc := streamlinechart.New(m.width, 10)
|
|
|
+ for _, v := range address.results {
|
|
|
+ slc.Push(v)
|
|
|
+ }
|
|
|
+ slc.Draw()
|
|
|
+ output = output + fmt.Sprintf("%s\n\n", slc.View())
|
|
|
}
|
|
|
- slc.Draw()
|
|
|
- output = output + fmt.Sprintf("%s\n\n", slc.View())
|
|
|
}
|
|
|
|
|
|
return output
|