tui.go 3.6 KB

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