1
0

tui.go 3.4 KB

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