tui.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // TODO(chart): dynamically render charts to fit height on not set
  2. //
  3. // TODO(styling): allow end user to define their own styles when hacking.
  4. //
  5. // Pingo defines an extensible TUI based on the bubbletea framework (v2). An
  6. // executable entry point is defined via cmd/pingo.go. For more on this topic,
  7. // see the Readme.
  8. //
  9. // The pingo TUI is a fully fledged and extensible "bubble" (read: widget) that
  10. // can be fully implemented as an element in further terminal applications.
  11. //
  12. // NOTE: the bubbletea framework and subsequent "bubble" concept are beyond the
  13. // scope of this documentation. For more, see the Readme.
  14. //
  15. // Pingo defines a constructor function for the Model. It takes three arguments:
  16. //
  17. // - addresses([]string): the hosts to ping. The length must be greater than
  18. // or equal to 1
  19. //
  20. // - speed(time.Duration): the polling interval
  21. //
  22. // - chartHeight(int): the desired height of the resulting charts. This
  23. // argument is integral to ensuring desired rendering of charts, when
  24. // displaying multiple hosts.
  25. //
  26. // NOTE: if chartHeight is 0, the chart will render to Model.Height
  27. //
  28. // NOTE: chartHeight is ignored when only one address or host is provided
  29. //
  30. // For more, please please see InitialModel()
  31. //
  32. // Pingo defines two bubbletea.Cmd functions:
  33. //
  34. // - Model.Tick() tea.Cmd: emits a bubbletea.TickMsg after the time.Duration
  35. // specified via Model.UpdateSpeed
  36. //
  37. // NOTE: Model.Tick() is optional. If you choose not to use Model.Tick(),
  38. // it is recommended to enforce some minimum rate mechanism for calling
  39. // Poll(). Some servers maintain a ping rate limit, and is is possible to
  40. // exceed this rate trivially with the Poll() function. (Trust us, we know
  41. // from experience)
  42. //
  43. // NOTE: Model.Tick() is automatically emit by Model.Init() - therefore,
  44. // you can control the timing of polling by overloading the Init function.
  45. //
  46. // - Model.Poll() tea.Msg: used to asynchronously call all Model.Addresses.Poll()
  47. // functions.
  48. //
  49. // NOTE: Model.Poll() is automatically injected into the Model.Update()
  50. // life cycle after Model.Tick() resolves by Model.Update(). Functionally,
  51. // this means you can omit either Model.Tick() or Model.Poll(), respectively.
  52. //
  53. // For more, see the Readme or ./examples
  54. package pingo
  55. import (
  56. "fmt"
  57. "slices"
  58. tea "charm.land/bubbletea/v2"
  59. "charm.land/lipgloss/v2"
  60. "github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
  61. )
  62. // Style Definitions
  63. var (
  64. // footer styles
  65. titleStyle = lipgloss.NewStyle().
  66. Align(lipgloss.Center). // implies consumer functions will apply a width
  67. Italic(true).
  68. Faint(true)
  69. // footer style
  70. footerStyle = lipgloss.NewStyle().
  71. Align(lipgloss.Center). // implies consumer functions will apply a width
  72. Italic(true).
  73. Faint(true)
  74. // A style for chart headers
  75. headerStyle = lipgloss.NewStyle().
  76. Bold(true).
  77. Italic(true)
  78. // A style for info text
  79. infoStyle = lipgloss.NewStyle().
  80. Italic(true).
  81. Faint(true)
  82. // A style for the secondary colour
  83. secondaryColor = lipgloss.NewStyle().
  84. Foreground(lipgloss.Color("#7b2d26"))
  85. // A style for the primary colour
  86. // primaryColor = lipgloss.NewStyle().
  87. // Foreground(lipgloss.Color("#f0f3f5"))
  88. // A style for handling center-aligning
  89. blockStyle = lipgloss.NewStyle().
  90. Align(lipgloss.Center)
  91. // borderStyle = lipgloss.NewStyle().
  92. // BorderForeground(lipgloss.Color("8")).
  93. // // Padding(1, 2).
  94. // BorderStyle(lipgloss.NormalBorder())
  95. )
  96. type ( // tea.Msg signatures
  97. pollResultMsg struct {
  98. results []float64
  99. index int
  100. err error
  101. }
  102. )
  103. // Bubbletea model
  104. type Model struct {
  105. Addresses []Address // as defined in internal/tui/types.go
  106. ChartHeight int
  107. Height int
  108. Width int
  109. }
  110. func InitialModel(addresses []string, width, height, chartHeight int) Model {
  111. var model Model
  112. model.ChartHeight = chartHeight
  113. model.Width = width
  114. model.Height = height
  115. for _, address := range addresses {
  116. var addr Address
  117. addr.MaxResults = 80
  118. addr.Address = address
  119. model.Addresses = append(model.Addresses, addr)
  120. }
  121. return model
  122. }
  123. func (m Model) Init() tea.Cmd {
  124. return m.Poll()
  125. }
  126. func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
  127. var cmd tea.Cmd
  128. var cmds []tea.Cmd
  129. switch msg := msg.(type) {
  130. case pollResultMsg:
  131. m.Addresses[msg.index].Results = msg.results
  132. }
  133. for i := range m.Addresses {
  134. m.Addresses[i].MaxResults = m.Width
  135. }
  136. cmds = append(cmds, cmd)
  137. return m, tea.Batch(cmds...)
  138. }
  139. func (m Model) View() tea.View {
  140. var content string
  141. for _, address := range m.Addresses {
  142. if len(address.Results) == 0 {
  143. content = content + fmt.Sprintf("\n%s\tloading...", headerStyle.Render(address.Address))
  144. } else if m.Width != 0 && m.Height != 0 {
  145. if slices.Contains(address.Results, -1) {
  146. content = content + fmt.Sprintf("\n%s",
  147. blockStyle.Width(m.Width).Render(headerStyle.Render(
  148. secondaryColor.Render(address.Address),
  149. infoStyle.Render("(connection unstable)"),
  150. ),
  151. ))
  152. } else {
  153. content = content + fmt.Sprintf("\n%s",
  154. blockStyle.Width(m.Width).Render(headerStyle.Render(address.Address)))
  155. }
  156. // Linechart
  157. // set chartHeight - vertical margin
  158. chartHeight := m.Height - 2 // 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.Width, 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.Width, chartHeight-5)
  166. } else {
  167. slc = streamlinechart.New(m.Width, m.ChartHeight)
  168. }
  169. for _, v := range address.Results {
  170. slc.Push(v)
  171. }
  172. slc.Draw()
  173. content = content + fmt.Sprintf("\n%s", slc.View())
  174. }
  175. }
  176. content = content + "\n"
  177. var v tea.View
  178. v.SetContent(content)
  179. v.AltScreen = true
  180. return v
  181. }
  182. // Returns a batched set of tea.Cmd functions for each address.
  183. func (m Model) Poll() tea.Cmd {
  184. var cmds []tea.Cmd
  185. for i, element := range m.Addresses {
  186. cmds = append(cmds, func() tea.Msg {
  187. results, err := element.Poll()
  188. return pollResultMsg{results: results, err: err, index: i}
  189. })
  190. }
  191. return tea.Batch(cmds...)
  192. }