tui.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 // TODO(complexity): too many if branches
  65. var slc streamlinechart.Model
  66. if m.ChartHeight == 0 {
  67. if len(m.Addresses) > 1 {
  68. slc = streamlinechart.New(m.width, m.viewport.Height-9)
  69. } else {
  70. slc = streamlinechart.New(m.width, m.viewport.Height-4)
  71. }
  72. } else {
  73. slc = streamlinechart.New(m.width, m.ChartHeight)
  74. }
  75. for _, v := range address.results {
  76. slc.Push(v)
  77. }
  78. slc.Draw()
  79. output = output + blockStyle.Render(fmt.Sprintf("\n%s\n", slc.View()))
  80. }
  81. }
  82. return output
  83. }
  84. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  85. var cmd tea.Cmd
  86. var cmds []tea.Cmd
  87. switch msg := msg.(type) {
  88. // if case is KeyMsg (keypress)
  89. case tea.WindowSizeMsg:
  90. m.width = msg.Width
  91. for i, address := range m.Addresses {
  92. address.max_results = m.width
  93. m.Addresses[i] = address
  94. }
  95. m.viewport.Height = msg.Height - 4
  96. m.viewport.Width = msg.Width
  97. m.viewport.YPosition = 1
  98. case tea.KeyMsg:
  99. if k := msg.String(); k == "j" { // scroll down
  100. m.viewport.LineDown(8)
  101. } else if k == "k" { // scroll up
  102. m.viewport.LineUp(8)
  103. } else {
  104. return m, tea.Quit
  105. }
  106. case tickMsg:
  107. cmds = append(cmds, m.Tick())
  108. cmds = append(cmds, m.Poll)
  109. }
  110. m.viewport, cmd = m.viewport.Update(msg)
  111. m.viewport.SetContent(m.content())
  112. cmds = append(cmds, cmd)
  113. // cmds = append(cmds, m.Poll)
  114. return m, tea.Batch(cmds...)
  115. }
  116. func (m Model) View() string {
  117. var headerStyle = lipgloss.NewStyle().
  118. Width(m.width).
  119. Align(lipgloss.Center).
  120. Italic(true).
  121. Faint(true)
  122. var footerStyle = lipgloss.NewStyle().
  123. Width(m.width).
  124. Align(lipgloss.Center).
  125. Italic(true).
  126. Faint(true)
  127. header := headerStyle.Render("pingo v0")
  128. footer := footerStyle.Render("\nj/k: down/up\t|\tq/ctrl-c/esc: quit\n")
  129. return fmt.Sprintf("\n%s\n%s\n%s", header, m.viewport.View(), footer)
  130. }
  131. // A wrapper for the underlying [tui.Address.Poll] function. For each address in
  132. // [tui.Model.Addresses], run its respective Poll function and update [tui.Model]
  133. //
  134. // NOTE(async): this function fully blocks execution of the current thread.
  135. func (m Model) Poll() tea.Msg {
  136. for i, element := range m.Addresses {
  137. element.Poll()
  138. // element.results = append(element.results, -1)
  139. m.Addresses[i] = element
  140. }
  141. return nil
  142. }