tui.go 4.1 KB

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