1
0

tui.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // TODO document TUI lifecycle
  2. // TODO viewport separation
  3. //
  4. // BLOCKERS: header/footer height func
  5. //
  6. // TODO header/footer height func
  7. package pingo
  8. import (
  9. "fmt"
  10. "slices"
  11. "time"
  12. "charm.land/bubbles/v2/viewport"
  13. tea "charm.land/bubbletea/v2"
  14. "charm.land/lipgloss/v2"
  15. "github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
  16. )
  17. // Style Defintions
  18. var (
  19. // A style for chart headers
  20. headerStyle = lipgloss.NewStyle().
  21. Bold(true).
  22. Italic(true)
  23. // A style for info text
  24. infoStyle = lipgloss.NewStyle().
  25. Italic(true).
  26. Faint(true)
  27. // A style for the secondary colour
  28. secondaryColor = lipgloss.NewStyle().
  29. Foreground(lipgloss.Color("#7b2d26"))
  30. // A style for the primary colour
  31. // primaryColor = lipgloss.NewStyle().
  32. // Foreground(lipgloss.Color("#f0f3f5"))
  33. // A style for handling center-aligning
  34. blockStyle = lipgloss.NewStyle().
  35. Align(lipgloss.Center)
  36. // borderStyle = lipgloss.NewStyle().
  37. // BorderForeground(lipgloss.Color("8")).
  38. // // Padding(1, 2).
  39. // BorderStyle(lipgloss.NormalBorder())
  40. // footer styles
  41. titleStyle = lipgloss.NewStyle().
  42. Align(lipgloss.Center). // implies consumer functions will apply a width
  43. Italic(true).
  44. Faint(true)
  45. // footer style
  46. footerStyle = lipgloss.NewStyle().
  47. Align(lipgloss.Center). // implies consumer functions will apply a width
  48. Italic(true).
  49. Faint(true)
  50. )
  51. type ( // tea.Msg signatures
  52. tickMsg time.Time
  53. pollResultMsg struct {
  54. results []float64
  55. index int
  56. err error
  57. }
  58. )
  59. // Bubbletea model
  60. type Model struct {
  61. width int
  62. Addresses []Address // as defined in internal/tui/types.go
  63. viewport viewport.Model
  64. UpdateSpeed time.Duration
  65. ChartHeight int
  66. ModelHeight int
  67. ModelWidth int
  68. }
  69. func InitialModel(addresses []string, speed time.Duration, chartHeight int) Model {
  70. var model Model
  71. model.viewport.MouseWheelEnabled = true
  72. model.UpdateSpeed = speed
  73. model.ChartHeight = chartHeight
  74. for _, address := range addresses {
  75. var addr Address
  76. addr.MaxResults = 80
  77. addr.Address = address
  78. model.Addresses = append(model.Addresses, addr)
  79. }
  80. return model
  81. }
  82. func (m Model) Init() tea.Cmd {
  83. return m.Tick()
  84. }
  85. func (m Model) Tick() tea.Cmd {
  86. return tea.Tick(time.Millisecond*m.UpdateSpeed, func(t time.Time) tea.Msg {
  87. return tickMsg(t)
  88. })
  89. }
  90. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  91. var cmd tea.Cmd
  92. var cmds []tea.Cmd
  93. switch msg := msg.(type) {
  94. // if case is KeyMsg (keypress)
  95. case tea.WindowSizeMsg:
  96. if m.ModelWidth == 0 && m.ModelHeight == 0 {
  97. m.viewport = viewport.New(
  98. viewport.WithHeight(10),
  99. viewport.WithWidth(msg.Width),
  100. )
  101. }
  102. m.ModelWidth = msg.Width
  103. m.ModelHeight = msg.Height
  104. for i, address := range m.Addresses {
  105. address.MaxResults = m.ModelWidth
  106. m.Addresses[i] = address
  107. }
  108. m.viewport.SetHeight(m.ModelHeight - m.getVerticalMargin())
  109. m.viewport.SetWidth(m.ModelWidth)
  110. m.viewport.YPosition = 1
  111. case tea.KeyPressMsg:
  112. if k := msg.String(); k == "j" { // scroll down
  113. m.viewport.ScrollDown(1)
  114. } else if k == "k" { // scroll up
  115. m.viewport.ScrollUp(1)
  116. } else {
  117. if k == "ctrl+c" {
  118. cmds = append(cmds, tea.Quit)
  119. }
  120. }
  121. case tickMsg:
  122. cmds = append(cmds, m.Tick(), m.Poll())
  123. case pollResultMsg:
  124. m.Addresses[msg.index].Results = msg.results
  125. }
  126. m.viewport.SetContent(m.Render())
  127. m.viewport, cmd = m.viewport.Update(msg)
  128. cmds = append(cmds, cmd)
  129. // cmds = append(cmds, m.Poll)
  130. return m, tea.Batch(cmds...)
  131. }
  132. func (m Model) View() tea.View {
  133. content := fmt.Sprintf("%s%s\n%s", m.header(), m.viewport.View(), m.footer())
  134. var v tea.View
  135. v.SetContent(content)
  136. v.AltScreen = true
  137. return v
  138. }
  139. func (m Model) Render() string {
  140. var output string
  141. for _, address := range m.Addresses {
  142. if len(address.Results) == 0 {
  143. output = output + fmt.Sprintf("\n%s\tloading...", headerStyle.Render(address.Address))
  144. } else if m.ModelWidth != 0 && m.ModelHeight != 0 {
  145. if slices.Contains(address.Results, -1) {
  146. output = output + blockStyle.Width(m.ModelWidth).Render(headerStyle.Render(
  147. fmt.Sprintf("\n%s\t%s",
  148. secondaryColor.Render(address.Address),
  149. infoStyle.Render("(connection unstable)"),
  150. ),
  151. ))
  152. } else {
  153. output = output + fmt.Sprintf("\n%s",
  154. blockStyle.Width(m.ModelWidth).Render(headerStyle.Render(address.Address)))
  155. }
  156. // Linechart
  157. // set chartHeight - vertical margin
  158. chartHeight := m.ModelHeight - m.getVerticalMargin()
  159. var slc streamlinechart.Model
  160. if m.ChartHeight == 0 && len(m.Addresses) == 1 { // catch user specified fullscreen
  161. // render chart at fullscreen
  162. slc = streamlinechart.New(m.ModelWidth, chartHeight)
  163. } else if m.ChartHeight == 0 && len(m.Addresses) > 1 { // catch user specified fullscreen
  164. // render chart at fullscreen minus a few lines to hint at scrolling
  165. slc = streamlinechart.New(m.ModelWidth, chartHeight-5)
  166. } else {
  167. slc = streamlinechart.New(m.ModelWidth, m.ChartHeight)
  168. }
  169. for _, v := range address.Results {
  170. slc.Push(v)
  171. }
  172. slc.Draw()
  173. output = output + fmt.Sprintf("\n%s", slc.View())
  174. }
  175. }
  176. return output
  177. }
  178. func (m Model) header() string { return titleStyle.Width(m.ModelWidth).Render("pingo v0") }
  179. func (m Model) footer() string {
  180. return footerStyle.Width(m.ModelWidth).Render("j/k: down/up\t|\tq/ctrl-c/esc: quit")
  181. }
  182. func (m Model) getVerticalMargin() int { return lipgloss.Height(m.header() + m.footer()) }
  183. // Returns a batched set of tea.Cmd functions for each address.
  184. func (m Model) Poll() tea.Cmd {
  185. var cmds []tea.Cmd
  186. for i, element := range m.Addresses {
  187. cmds = append(cmds, func() tea.Msg {
  188. results, err := element.Poll()
  189. return pollResultMsg{results: results, err: err, index: i}
  190. })
  191. }
  192. return tea.Batch(cmds...)
  193. }