1
0

tui.go 3.8 KB

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