1
0

tui.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 Peak bubble. It takes three
  16. // arguments:
  17. //
  18. // - addresses([]string): the hosts to ping. The length must be greater than
  19. // or equal to 1
  20. //
  21. // - speed(time.Duration): the polling interval
  22. //
  23. // - chartHeight(int): the desired height of the resulting charts. This
  24. // argument is integral to ensuring desired rendering of charts, when
  25. // displaying multiple hosts.
  26. //
  27. // NOTE: if chartHeight is 0, the chart will render to Model.Height
  28. //
  29. // NOTE: chartHeight is ignored when only one address or host is provided
  30. //
  31. // For more, please please see InitialPeakBubble()
  32. //
  33. // Pingo defines the following bubbletea.Cmd functions:
  34. //
  35. // - Model.Poll() tea.Msg: used to asynchronously call all Model.Addresses.Poll()
  36. // functions.
  37. //
  38. // For an example implementation, see cmd/pingo.go
  39. package pingo
  40. import (
  41. "fmt"
  42. "slices"
  43. tea "charm.land/bubbletea/v2"
  44. "charm.land/lipgloss/v2"
  45. "github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
  46. )
  47. // Style Definitions
  48. var (
  49. // footer styles
  50. titleStyle = lipgloss.NewStyle().
  51. Align(lipgloss.Center). // implies consumer functions will apply a width
  52. Italic(true).
  53. Faint(true)
  54. // footer style
  55. footerStyle = lipgloss.NewStyle().
  56. Align(lipgloss.Center). // implies consumer functions will apply a width
  57. Italic(true).
  58. Faint(true)
  59. // A style for chart headers
  60. headerStyle = lipgloss.NewStyle().
  61. Bold(true).
  62. Italic(true)
  63. // A style for info text
  64. infoStyle = lipgloss.NewStyle().
  65. Italic(true).
  66. Faint(true)
  67. // A style for the secondary colour
  68. secondaryColor = lipgloss.NewStyle().
  69. Foreground(lipgloss.Color("#7b2d26"))
  70. // A style for the primary colour
  71. // primaryColor = lipgloss.NewStyle().
  72. // Foreground(lipgloss.Color("#f0f3f5"))
  73. // A style for handling center-aligning
  74. blockStyle = lipgloss.NewStyle().
  75. Align(lipgloss.Center)
  76. // borderStyle = lipgloss.NewStyle().
  77. // BorderForeground(lipgloss.Color("8")).
  78. // // Padding(1, 2).
  79. // BorderStyle(lipgloss.NormalBorder())
  80. )
  81. type ( // tea.Msg signatures
  82. pollResultMsg struct {
  83. results []float64
  84. index int
  85. err error
  86. }
  87. )
  88. // Bubbletea bubble
  89. type Peak struct {
  90. Addresses []Address // as defined in internal/tui/types.go
  91. ChartHeight int
  92. Height int
  93. Width int
  94. }
  95. // Instantiates a Peak bubble with the provided arguments.
  96. func InitialPeakBubble(addresses []string, width, height, chartHeight int) Peak {
  97. var model Peak
  98. model.ChartHeight = chartHeight
  99. model.Width = width
  100. model.Height = height
  101. for _, address := range addresses {
  102. var addr Address
  103. addr.MaxResults = 80
  104. addr.Address = address
  105. model.Addresses = append(model.Addresses, addr)
  106. }
  107. return model
  108. }
  109. // Provides initial polling for the bubble. Returns the result of Peak.Poll
  110. func (p Peak) Init() tea.Cmd {
  111. return p.Poll()
  112. }
  113. // The update function for the Peak bubble.
  114. func (p Peak) Update(msg tea.Msg) (Peak, tea.Cmd) {
  115. var cmd tea.Cmd
  116. var cmds []tea.Cmd
  117. switch msg := msg.(type) {
  118. case pollResultMsg:
  119. p.Addresses[msg.index].Results = msg.results
  120. }
  121. for i := range p.Addresses {
  122. p.Addresses[i].MaxResults = p.Width
  123. }
  124. cmds = append(cmds, cmd)
  125. return p, tea.Batch(cmds...)
  126. }
  127. func (p Peak) View() tea.View {
  128. var content string
  129. for _, address := range p.Addresses {
  130. if len(address.Results) == 0 {
  131. content = content + fmt.Sprintf("\n%s\tloading...", headerStyle.Render(address.Address))
  132. } else if p.Width != 0 && p.Height != 0 {
  133. if slices.Contains(address.Results, -1) {
  134. content = content + fmt.Sprintf("\n%s",
  135. blockStyle.Width(p.Width).Render(headerStyle.Render(
  136. secondaryColor.Render(address.Address),
  137. infoStyle.Render("(connection unstable)"),
  138. ),
  139. ))
  140. } else {
  141. content = content + fmt.Sprintf("\n%s",
  142. blockStyle.Width(p.Width).Render(headerStyle.Render(address.Address)))
  143. }
  144. // Linechart
  145. // set chartHeight - vertical margin
  146. // chartHeight := p.Height - 2 // p.getVerticalMargin()
  147. var slc streamlinechart.Model
  148. if p.ChartHeight == 0 && len(p.Addresses) == 1 { // catch user specified fullscreen
  149. // render chart at fullscreen
  150. slc = streamlinechart.New(p.Width, p.Height-1)
  151. } else if p.ChartHeight == 0 && len(p.Addresses) > 1 { // catch user specified fullscreen
  152. // render chart at fullscreen minus a few lines to hint at scrolling
  153. slc = streamlinechart.New(p.Width, p.Height-2)
  154. } else {
  155. slc = streamlinechart.New(p.Width, p.ChartHeight-1)
  156. }
  157. for _, v := range address.Results {
  158. slc.Push(v)
  159. }
  160. slc.Draw()
  161. content = content + fmt.Sprintf("\n%s", slc.View())
  162. }
  163. }
  164. var v tea.View
  165. v.SetContent(content)
  166. v.AltScreen = true
  167. return v
  168. }
  169. // Returns a batched set of tea.Cmd that call Address.Poll functions for each
  170. // address.
  171. func (p Peak) Poll() tea.Cmd {
  172. var cmds []tea.Cmd
  173. for i, element := range p.Addresses {
  174. cmds = append(cmds, func() tea.Msg {
  175. results, err := element.Poll()
  176. return pollResultMsg{results: results, err: err, index: i}
  177. })
  178. }
  179. return tea.Batch(cmds...)
  180. }