tui.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Pingo defines an extensible TUI based on the bubbletea framework (v2). An
  2. // executable entry point is defined via cmd/pingo.go. For more on this topic,
  3. // see the Readme.
  4. //
  5. // The pingo TUI is a fully fledged and extensible "bubble" (read: widget) that
  6. // can be fully implemented as an element in further terminal applications.
  7. //
  8. // NOTE: the bubbletea framework and subsequent "bubble" concept are beyond the
  9. // scope of this documentation. For more, see the Readme.
  10. //
  11. // Pingo defines a constructor function for the Peak bubble. It takes three
  12. // arguments:
  13. //
  14. // - addresses([]string): the hosts to ping. The length must be greater than
  15. // or equal to 1
  16. //
  17. // - width(int): the width of the bubble
  18. //
  19. // - height(int): the height of the bubble
  20. //
  21. // - chartHeight(int): the desired height of the resulting charts. This
  22. // argument is integral to ensuring desired rendering of charts, when
  23. // displaying multiple hosts.
  24. //
  25. // NOTE: if chartHeight is 0, the chart will render to Model.Height
  26. //
  27. // NOTE: chartHeight is ignored when only one address or host is provided
  28. //
  29. // NOTE: if a specific chartHeight is specified, it is possible to exceeed
  30. // desired height.
  31. //
  32. // For more, please please see InitialPeakBubble()
  33. //
  34. // Pingo defines the following bubbletea.Cmd functions:
  35. //
  36. // - Peak.Poll() tea.Msg: used to asynchronously call all Model.Addresses.Poll()
  37. // functions.
  38. //
  39. // NOTE: the project does not implement a timing mechanism for polling Addresses.
  40. // It is expected that the developer implements a timing mechanism.
  41. //
  42. // CRITICAL NOTE: it is possible to exceed rate limits from remote targets. It is
  43. // recommended to ensure that you implement a minimum time between calls to Poll.
  44. // As timing may be handled in a number of ways, it exceeds the scope of this
  45. // project to provide one.
  46. //
  47. // For an example implementation, including a recommended timing mechanisim,
  48. // see cmd/pingo.go.
  49. //
  50. // TODO(chart): dynamically render charts to fit height on not set
  51. //
  52. // TODO(styling): allow end user to define their own styles when hacking.
  53. //
  54. // TODO(deprecate chart height): in future, specifying chart height's directly
  55. // will be deprecated in favour of a dynamically determined chart height. (see
  56. // "TODO(chart)")
  57. package pingo
  58. import (
  59. "fmt"
  60. "slices"
  61. tea "charm.land/bubbletea/v2"
  62. "charm.land/lipgloss/v2"
  63. "github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
  64. )
  65. // Style Definitions
  66. var (
  67. // footer styles
  68. titleStyle = lipgloss.NewStyle().
  69. Align(lipgloss.Center). // implies consumer functions will apply a width
  70. Italic(true).
  71. Faint(true)
  72. // footer style
  73. footerStyle = lipgloss.NewStyle().
  74. Align(lipgloss.Center). // implies consumer functions will apply a width
  75. Italic(true).
  76. Faint(true)
  77. // A style for chart headers
  78. headerStyle = lipgloss.NewStyle().
  79. Bold(true).
  80. Italic(true)
  81. // A style for info text
  82. infoStyle = lipgloss.NewStyle().
  83. Italic(true).
  84. Faint(true)
  85. // A style for the secondary colour
  86. secondaryColor = lipgloss.NewStyle().
  87. Foreground(lipgloss.Color("#7b2d26"))
  88. // A style for the primary colour
  89. // primaryColor = lipgloss.NewStyle().
  90. // Foreground(lipgloss.Color("#f0f3f5"))
  91. // A style for handling center-aligning
  92. blockStyle = lipgloss.NewStyle().
  93. Align(lipgloss.Center)
  94. // borderStyle = lipgloss.NewStyle().
  95. // BorderForeground(lipgloss.Color("8")).
  96. // // Padding(1, 2).
  97. // BorderStyle(lipgloss.NormalBorder())
  98. )
  99. type ( // tea.Msg signatures
  100. pollResultMsg struct {
  101. results []float64
  102. index int
  103. err error
  104. }
  105. )
  106. // Bubbletea bubble
  107. type Peak struct {
  108. Addresses []Address // as defined in internal/tui/types.go
  109. ChartHeight int // DEPRECATE
  110. Height int
  111. Width int
  112. }
  113. // Instantiates a Peak bubble with the provided arguments.
  114. func InitialPeakBubble(addresses []string, width, height, chartHeight int) Peak {
  115. var model Peak
  116. model.ChartHeight = chartHeight
  117. model.Width = width
  118. model.Height = height
  119. for _, address := range addresses {
  120. var addr Address
  121. addr.MaxResults = 80
  122. addr.Address = address
  123. model.Addresses = append(model.Addresses, addr)
  124. }
  125. return model
  126. }
  127. // Provides initial polling for the bubble. Returns the result of Peak.Poll
  128. func (p Peak) Init() tea.Cmd {
  129. return p.Poll()
  130. }
  131. // The update function for the Peak bubble.
  132. func (p Peak) Update(msg tea.Msg) (Peak, tea.Cmd) {
  133. var cmd tea.Cmd
  134. var cmds []tea.Cmd
  135. switch msg := msg.(type) {
  136. case pollResultMsg:
  137. p.Addresses[msg.index].Results = msg.results
  138. }
  139. for i := range p.Addresses {
  140. p.Addresses[i].MaxResults = p.Width
  141. }
  142. cmds = append(cmds, cmd)
  143. return p, tea.Batch(cmds...)
  144. }
  145. func (p Peak) View() tea.View {
  146. var content string
  147. for _, address := range p.Addresses {
  148. if len(address.Results) == 0 {
  149. content = content + fmt.Sprintf("\n%s\tloading...", headerStyle.Render(address.Address))
  150. } else if p.Width != 0 && p.Height != 0 {
  151. if slices.Contains(address.Results, -1) {
  152. content = content + fmt.Sprintf("\n%s",
  153. blockStyle.Width(p.Width).Render(headerStyle.Render(
  154. secondaryColor.Render(address.Address),
  155. infoStyle.Render("(connection unstable)"),
  156. ),
  157. ))
  158. } else {
  159. content = content + fmt.Sprintf("\n%s",
  160. blockStyle.Width(p.Width).Render(headerStyle.Render(address.Address)))
  161. }
  162. // Linechart
  163. // set chartHeight - vertical margin
  164. // chartHeight := p.Height - 2 // p.getVerticalMargin()
  165. var slc streamlinechart.Model
  166. if p.ChartHeight == 0 && len(p.Addresses) == 1 { // catch user specified fullscreen
  167. // render chart at fullscreen
  168. slc = streamlinechart.New(p.Width, p.Height-1)
  169. } else if p.ChartHeight == 0 && len(p.Addresses) > 1 { // catch user specified fullscreen
  170. // render chart at fullscreen minus a few lines to hint at scrolling
  171. slc = streamlinechart.New(p.Width, p.Height-2)
  172. } else {
  173. slc = streamlinechart.New(p.Width, p.ChartHeight-1) // DEPRECATE
  174. }
  175. for _, v := range address.Results {
  176. slc.Push(v)
  177. }
  178. slc.Draw()
  179. content = content + fmt.Sprintf("\n%s", slc.View())
  180. }
  181. }
  182. var v tea.View
  183. v.SetContent(content)
  184. v.AltScreen = true
  185. return v
  186. }
  187. // Returns a batched set of tea.Cmd that call Address.Poll functions for each
  188. // address.
  189. func (p Peak) Poll() tea.Cmd {
  190. var cmds []tea.Cmd
  191. for i, element := range p.Addresses {
  192. cmds = append(cmds, func() tea.Msg {
  193. results, err := element.Poll()
  194. return pollResultMsg{results: results, err: err, index: i}
  195. })
  196. }
  197. return tea.Batch(cmds...)
  198. }