tui.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Model. It takes three arguments:
  12. //
  13. // - addresses([]string): the hosts to ping. The length must be greater than
  14. // or equal to 1
  15. //
  16. // - speed(time.Duration): the polling interval
  17. //
  18. // - chartHeight(int): the desired height of the resulting charts. This
  19. // argument is integral to ensuring desired rendering of charts, when
  20. // displaying multiple hosts.
  21. //
  22. // NOTE: if chartHeight is 0, the chart will render to Model.Height
  23. //
  24. // NOTE: chartHeight is ignored when only one address or host is provided
  25. //
  26. // For more, please please see InitialModel()
  27. //
  28. // Pingo defines two bubbletea.Cmd functions:
  29. //
  30. // - Model.Tick() tea.Cmd: emits a bubbletea.TickMsg after the time.Duration
  31. // specified via Model.UpdateSpeed
  32. //
  33. // NOTE: Model.Tick() is optional. If you choose not to use Model.Tick(),
  34. // it is recommended to enforce some minimum rate mechanism for calling
  35. // Poll(). Some servers maintain a ping rate limit, and is is possible to
  36. // exceed this rate trivially with the Poll() function. (Trust us, we know
  37. // from experience)
  38. //
  39. // NOTE: Model.Tick() is automatically emit by Model.Init() - therefore,
  40. // you can control the timing of polling by overloading the Init function.
  41. //
  42. // - Model.Poll() tea.Msg: used to asynchronously call all Model.Addresses.Poll()
  43. // functions.
  44. //
  45. // NOTE: Model.Poll() is automatically injected into the Model.Update()
  46. // life cycle after Model.Tick() resolves by Model.Update(). Functionally,
  47. // this means you can omit either Model.Tick() or Model.Poll(), respectively.
  48. //
  49. // For more, see the Readme or ./examples
  50. package pingo
  51. import (
  52. "fmt"
  53. "slices"
  54. "time"
  55. "charm.land/bubbles/v2/viewport"
  56. tea "charm.land/bubbletea/v2"
  57. "charm.land/lipgloss/v2"
  58. "github.com/NimbleMarkets/ntcharts/linechart/streamlinechart"
  59. )
  60. // Style Definitions
  61. var (
  62. // A style for chart headers
  63. headerStyle = lipgloss.NewStyle().
  64. Bold(true).
  65. Italic(true)
  66. // A style for info text
  67. infoStyle = lipgloss.NewStyle().
  68. Italic(true).
  69. Faint(true)
  70. // A style for the secondary colour
  71. secondaryColor = lipgloss.NewStyle().
  72. Foreground(lipgloss.Color("#7b2d26"))
  73. // A style for the primary colour
  74. // primaryColor = lipgloss.NewStyle().
  75. // Foreground(lipgloss.Color("#f0f3f5"))
  76. // A style for handling center-aligning
  77. blockStyle = lipgloss.NewStyle().
  78. Align(lipgloss.Center)
  79. // borderStyle = lipgloss.NewStyle().
  80. // BorderForeground(lipgloss.Color("8")).
  81. // // Padding(1, 2).
  82. // BorderStyle(lipgloss.NormalBorder())
  83. // footer styles
  84. titleStyle = lipgloss.NewStyle().
  85. Align(lipgloss.Center). // implies consumer functions will apply a width
  86. Italic(true).
  87. Faint(true)
  88. // footer style
  89. footerStyle = lipgloss.NewStyle().
  90. Align(lipgloss.Center). // implies consumer functions will apply a width
  91. Italic(true).
  92. Faint(true)
  93. )
  94. type ( // tea.Msg signatures
  95. tickMsg time.Time
  96. pollResultMsg struct {
  97. results []float64
  98. index int
  99. err error
  100. }
  101. )
  102. // Bubbletea model
  103. type Model struct {
  104. Addresses []Address // as defined in internal/tui/types.go
  105. viewport viewport.Model
  106. UpdateSpeed time.Duration
  107. ChartHeight int
  108. Height int
  109. Width int
  110. }
  111. func InitialModel(addresses []string, speed time.Duration, chartHeight int) Model {
  112. var model Model
  113. model.viewport.MouseWheelEnabled = true
  114. model.UpdateSpeed = speed
  115. model.ChartHeight = chartHeight
  116. for _, address := range addresses {
  117. var addr Address
  118. addr.MaxResults = 80
  119. addr.Address = address
  120. model.Addresses = append(model.Addresses, addr)
  121. }
  122. return model
  123. }
  124. func (m Model) Init() tea.Cmd {
  125. return m.Tick()
  126. }
  127. func (m Model) Tick() tea.Cmd {
  128. return tea.Tick(time.Millisecond*m.UpdateSpeed, func(t time.Time) tea.Msg {
  129. return tickMsg(t)
  130. })
  131. }
  132. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  133. var cmd tea.Cmd
  134. var cmds []tea.Cmd
  135. switch msg := msg.(type) {
  136. // if case is KeyMsg (keypress)
  137. case tea.WindowSizeMsg:
  138. if m.Width == 0 && m.Height == 0 {
  139. m.viewport = viewport.New(
  140. viewport.WithHeight(10),
  141. viewport.WithWidth(msg.Width),
  142. )
  143. }
  144. m.Width = msg.Width
  145. m.Height = msg.Height
  146. for i, address := range m.Addresses {
  147. address.MaxResults = m.Width
  148. m.Addresses[i] = address
  149. }
  150. m.viewport.SetHeight(m.Height - m.getVerticalMargin())
  151. m.viewport.SetWidth(m.Width)
  152. m.viewport.YPosition = 1
  153. case tea.KeyPressMsg:
  154. if k := msg.String(); k == "j" { // scroll down
  155. m.viewport.ScrollDown(1)
  156. } else if k == "k" { // scroll up
  157. m.viewport.ScrollUp(1)
  158. } else {
  159. if k == "ctrl+c" {
  160. cmds = append(cmds, tea.Quit)
  161. }
  162. }
  163. case tickMsg:
  164. cmds = append(cmds, m.Tick(), m.Poll())
  165. case pollResultMsg:
  166. m.Addresses[msg.index].Results = msg.results
  167. }
  168. m.viewport.SetContent(m.Render())
  169. m.viewport, cmd = m.viewport.Update(msg)
  170. cmds = append(cmds, cmd)
  171. // cmds = append(cmds, m.Poll)
  172. return m, tea.Batch(cmds...)
  173. }
  174. func (m Model) View() tea.View {
  175. content := fmt.Sprintf("%s%s\n%s", m.header(), m.viewport.View(), m.footer())
  176. var v tea.View
  177. v.SetContent(content)
  178. v.AltScreen = true
  179. return v
  180. }
  181. func (m Model) Render() string {
  182. var output string
  183. for _, address := range m.Addresses {
  184. if len(address.Results) == 0 {
  185. output = output + fmt.Sprintf("\n%s\tloading...", headerStyle.Render(address.Address))
  186. } else if m.Width != 0 && m.Height != 0 {
  187. if slices.Contains(address.Results, -1) {
  188. output = output + blockStyle.Width(m.Width).Render(headerStyle.Render(
  189. fmt.Sprintf("\n%s\t%s",
  190. secondaryColor.Render(address.Address),
  191. infoStyle.Render("(connection unstable)"),
  192. ),
  193. ))
  194. } else {
  195. output = output + fmt.Sprintf("\n%s",
  196. blockStyle.Width(m.Width).Render(headerStyle.Render(address.Address)))
  197. }
  198. // Linechart
  199. // set chartHeight - vertical margin
  200. chartHeight := m.Height - m.getVerticalMargin()
  201. var slc streamlinechart.Model
  202. if m.ChartHeight == 0 && len(m.Addresses) == 1 { // catch user specified fullscreen
  203. // render chart at fullscreen
  204. slc = streamlinechart.New(m.Width, chartHeight)
  205. } else if m.ChartHeight == 0 && len(m.Addresses) > 1 { // catch user specified fullscreen
  206. // render chart at fullscreen minus a few lines to hint at scrolling
  207. slc = streamlinechart.New(m.Width, chartHeight-5)
  208. } else {
  209. slc = streamlinechart.New(m.Width, m.ChartHeight)
  210. }
  211. for _, v := range address.Results {
  212. slc.Push(v)
  213. }
  214. slc.Draw()
  215. output = output + fmt.Sprintf("\n%s", slc.View())
  216. }
  217. }
  218. return output
  219. }
  220. func (m Model) header() string { return titleStyle.Width(m.Width).Render("pingo v0") }
  221. func (m Model) footer() string {
  222. return footerStyle.Width(m.Width).Render("j/k: down/up\t|\tq/ctrl-c/esc: quit")
  223. }
  224. func (m Model) getVerticalMargin() int { return lipgloss.Height(m.header() + m.footer()) }
  225. // Returns a batched set of tea.Cmd functions for each address.
  226. func (m Model) Poll() tea.Cmd {
  227. var cmds []tea.Cmd
  228. for i, element := range m.Addresses {
  229. cmds = append(cmds, func() tea.Msg {
  230. results, err := element.Poll()
  231. return pollResultMsg{results: results, err: err, index: i}
  232. })
  233. }
  234. return tea.Batch(cmds...)
  235. }