tui.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // TODO(doc): document TUI lifecycle
  2. // TODO(test): write unittests for types.go
  3. package pingo
  4. import (
  5. "fmt"
  6. "slices"
  7. "time"
  8. "github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
  9. "github.com/charmbracelet/bubbles/viewport"
  10. tea "github.com/charmbracelet/bubbletea"
  11. "github.com/charmbracelet/lipgloss"
  12. )
  13. // Style Defintions
  14. var (
  15. // A style for chart headers
  16. headerStyle = lipgloss.NewStyle().
  17. Bold(true).
  18. Italic(true)
  19. // A style for info text
  20. infoStyle = lipgloss.NewStyle().
  21. Italic(true).
  22. Faint(true)
  23. // A style for the secondary colour
  24. secondaryColor = lipgloss.NewStyle().
  25. Foreground(lipgloss.Color("#7b2d26"))
  26. // A style for the primary colour
  27. // primaryColor = lipgloss.NewStyle().
  28. // Foreground(lipgloss.Color("#f0f3f5"))
  29. )
  30. type ( // tea.Msg signatures
  31. tickMsg time.Time
  32. pollResultMsg struct {
  33. results []float64
  34. index int
  35. err error
  36. }
  37. )
  38. // Bubbletea model
  39. type Model struct {
  40. width int
  41. Addresses []Address // as defined in internal/tui/types.go
  42. viewport viewport.Model
  43. UpdateSpeed time.Duration
  44. ChartHeight int
  45. }
  46. func InitialModel(addresses []string, speed time.Duration, chartHeight int) Model {
  47. var model Model
  48. model.viewport = viewport.New(0, 0)
  49. model.viewport.MouseWheelEnabled = true
  50. model.UpdateSpeed = speed
  51. model.ChartHeight = chartHeight
  52. for _, address := range addresses {
  53. var addr Address
  54. addr.max_results = 80
  55. addr.Address = address
  56. model.Addresses = append(model.Addresses, addr)
  57. }
  58. return model
  59. }
  60. func (m Model) Init() tea.Cmd {
  61. return m.Tick()
  62. }
  63. func (m Model) Tick() tea.Cmd {
  64. return tea.Tick(time.Millisecond*m.UpdateSpeed, func(t time.Time) tea.Msg {
  65. return tickMsg(t)
  66. })
  67. }
  68. func (m Model) content() string {
  69. var blockStyle = lipgloss.NewStyle().
  70. Width(m.width).
  71. Align(lipgloss.Center)
  72. output := "\n"
  73. for _, address := range m.Addresses {
  74. if len(address.results) == 0 {
  75. output = output + fmt.Sprintf("\n%s\tloading...", headerStyle.Render(address.Address))
  76. } else if m.viewport.Width != 0 && m.viewport.Height != 0 {
  77. if slices.Contains(address.results, -1) {
  78. output = output + fmt.Sprintf("\n%s",
  79. blockStyle.Render(headerStyle.Render(
  80. fmt.Sprintf("%s\t%s",
  81. secondaryColor.Render(address.Address),
  82. infoStyle.Render("(connection unstable)"),
  83. ),
  84. )))
  85. } else {
  86. output = output + fmt.Sprintf("\n%s",
  87. blockStyle.Render(headerStyle.Render(address.Address)))
  88. }
  89. // Linechart
  90. viewportHeight := m.viewport.Height - 4
  91. var slc streamlinechart.Model
  92. if m.ChartHeight == 0 && len(m.Addresses) == 1 {
  93. slc = streamlinechart.New(m.width, viewportHeight)
  94. } else if m.ChartHeight == 0 && len(m.Addresses) > 1 {
  95. slc = streamlinechart.New(m.width, viewportHeight-5)
  96. } else {
  97. slc = streamlinechart.New(m.width, m.ChartHeight)
  98. }
  99. for _, v := range address.results {
  100. slc.Push(v)
  101. }
  102. slc.Draw()
  103. output = output + blockStyle.Render(fmt.Sprintf("\n%s\n", slc.View()))
  104. }
  105. }
  106. return output
  107. }
  108. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  109. var cmd tea.Cmd
  110. var cmds []tea.Cmd
  111. switch msg := msg.(type) {
  112. // if case is KeyMsg (keypress)
  113. case tea.WindowSizeMsg:
  114. m.width = msg.Width
  115. for i, address := range m.Addresses {
  116. address.max_results = m.width
  117. m.Addresses[i] = address
  118. }
  119. m.viewport.Height = msg.Height - 4
  120. m.viewport.Width = msg.Width
  121. m.viewport.YPosition = 1
  122. case tea.KeyMsg:
  123. if k := msg.String(); k == "j" { // scroll down
  124. m.viewport.LineDown(8)
  125. } else if k == "k" { // scroll up
  126. m.viewport.LineUp(8)
  127. } else {
  128. return m, tea.Quit
  129. }
  130. case tickMsg:
  131. cmds = append(cmds, m.Tick())
  132. cmds = append(cmds, m.Poll)
  133. }
  134. m.viewport, cmd = m.viewport.Update(msg)
  135. m.viewport.SetContent(m.content())
  136. cmds = append(cmds, cmd)
  137. // cmds = append(cmds, m.Poll)
  138. return m, tea.Batch(cmds...)
  139. }
  140. func (m Model) View() string {
  141. var headerStyle = lipgloss.NewStyle().
  142. Width(m.width).
  143. Align(lipgloss.Center).
  144. Italic(true).
  145. Faint(true)
  146. var footerStyle = lipgloss.NewStyle().
  147. Width(m.width).
  148. Align(lipgloss.Center).
  149. Italic(true).
  150. Faint(true)
  151. header := headerStyle.Render("pingo v0")
  152. footer := footerStyle.Render("\nj/k: down/up\t|\tq/ctrl-c/esc: quit\n")
  153. return fmt.Sprintf("\n%s\n%s\n%s", header, m.viewport.View(), footer)
  154. }
  155. // A wrapper for the underlying [tui.Address.Poll] function. For each address in
  156. // [tui.Model.Addresses], run its respective Poll function and update [tui.Model]
  157. //
  158. // NOTE(async): this function fully blocks execution of the current thread.
  159. func (m Model) Poll() tea.Msg {
  160. for i, element := range m.Addresses {
  161. element.Poll()
  162. // element.results = append(element.results, -1)
  163. m.Addresses[i] = element
  164. }
  165. return nil
  166. }