tui.go 3.6 KB

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