types.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package pingo
  2. import (
  3. "time"
  4. "github.com/charmbracelet/lipgloss"
  5. )
  6. type Address struct {
  7. Address string
  8. results []float64
  9. max_results int
  10. }
  11. func (a *Address) Truncate() (truncated bool) {
  12. if len(a.results) > a.max_results {
  13. a.results = a.results[1:len(a.results)] // return modified slice missing first index
  14. return true
  15. }
  16. return false
  17. }
  18. // Wraps [Ping]
  19. func (a *Address) Ping() (delay float64, err error) {
  20. return Ping(a.Address)
  21. }
  22. // Poll the affiliated Address and appends it to a.results
  23. func (a *Address) Poll() (success bool, err error) {
  24. if delay, err := a.Ping(); err == nil {
  25. a.results = append(a.results, delay)
  26. a.Truncate() // enforce max length
  27. return true, nil
  28. } else {
  29. a.results = append(a.results, delay)
  30. a.Truncate() // enforce max length
  31. return false, err
  32. }
  33. }
  34. // Last returns the last result in [Address.results]. Returns -1 if no previous result
  35. func (a *Address) Last() (delay float64) {
  36. if len(a.results) > 0 {
  37. return a.results[len(a.results)-1]
  38. } else {
  39. return -1
  40. }
  41. }
  42. // [tea.Msg] signatures
  43. type tickMsg time.Time
  44. // A simple boolean flag sent when the program is ready to poll addresses
  45. type pollMsg bool
  46. // / A simple error message binding to conform to type [tea.Cmd]
  47. type errMsg struct{ err error }
  48. func (e errMsg) Error() string { return e.err.Error() }
  49. // Style Defintions
  50. // A style for chart headers
  51. var headerStyle = lipgloss.NewStyle().
  52. Bold(true).
  53. Italic(true)
  54. // A style for info text
  55. var infoStyle = lipgloss.NewStyle().
  56. Italic(true).
  57. Faint(true)
  58. // A style for the base colour
  59. // var baseColor = lipgloss.NewStyle().
  60. // Foreground(lipgloss.Color("#19535f"))
  61. // A style for the lower colour
  62. // var lowerColor = lipgloss.NewStyle().
  63. // Foreground(lipgloss.Color("#0b7a75"))
  64. // A style for the middle colour
  65. // var middleColor = lipgloss.NewStyle().
  66. // Foreground(lipgloss.Color("#d7c9aa"))
  67. // A style for the secondary colour
  68. var secondaryColor = lipgloss.NewStyle().
  69. Foreground(lipgloss.Color("#7b2d26"))
  70. // A style for the primary colour
  71. // var primaryColor = lipgloss.NewStyle().
  72. // Foreground(lipgloss.Color("#f0f3f5"))