types.go 2.1 KB

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