Parcourir la source

Simplified source code layout

arianagiroux il y a 1 semaine
Parent
commit
33231025d3
3 fichiers modifiés avec 67 ajouts et 93 suppressions
  1. 36 0
      ping.go
  2. 31 0
      tui.go
  3. 0 93
      types.go

+ 36 - 0
ping.go

@@ -60,3 +60,39 @@ func Ping(address string) (delay float64, err error) {
 	}
 	return -1, errors.New("could not resolve host: " + address)
 }
+
+type Address struct {
+	Address     string
+	results     []float64
+	max_results int
+}
+
+func (a Address) Truncate() []float64 {
+	if len(a.results) > a.max_results {
+		a.results = a.results[1:len(a.results)] // return modified slice missing first index
+	}
+
+	return a.results
+}
+
+// Wraps [Ping]
+func (a Address) Ping() (delay float64, err error) {
+	return Ping(a.Address)
+}
+
+// Poll  the affiliated Address and appends it to a.results
+func (a Address) Poll() (results []float64, err error) {
+	delay, err := a.Ping()
+	a.results = append(a.results, delay)
+	a.results = a.Truncate() // enforce max length
+	return a.results, err
+}
+
+// Last returns the last result in [Address.results]. Returns -1 if no previous result
+func (a Address) Last() (delay float64) {
+	if len(a.results) > 0 {
+		return a.results[len(a.results)-1]
+	} else {
+		return -1
+	}
+}

+ 31 - 0
tui.go

@@ -13,6 +13,37 @@ import (
 	"github.com/charmbracelet/lipgloss"
 )
 
+// Style Defintions
+
+var (
+	// A style for chart headers
+	headerStyle = lipgloss.NewStyle().
+			Bold(true).
+			Italic(true)
+
+	// A style for info text
+	infoStyle = lipgloss.NewStyle().
+			Italic(true).
+			Faint(true)
+
+	// A style for the secondary colour
+	secondaryColor = lipgloss.NewStyle().
+			Foreground(lipgloss.Color("#7b2d26"))
+
+	// A style for the primary colour
+	//  primaryColor = lipgloss.NewStyle().
+	// 	Foreground(lipgloss.Color("#f0f3f5"))
+)
+
+type ( // tea.Msg signatures
+	tickMsg       time.Time
+	pollResultMsg struct {
+		results []float64
+		index   int
+		err     error
+	}
+)
+
 // Bubbletea model
 type Model struct {
 	width       int

+ 0 - 93
types.go

@@ -1,93 +0,0 @@
-package pingo
-
-import (
-	"time"
-
-	"github.com/charmbracelet/lipgloss"
-)
-
-type Address struct {
-	Address     string
-	results     []float64
-	max_results int
-}
-
-func (a *Address) Truncate() (truncated bool) {
-	if len(a.results) > a.max_results {
-		a.results = a.results[1:len(a.results)] // return modified slice missing first index
-		return true
-	}
-
-	return false
-}
-
-// Wraps [Ping]
-func (a *Address) Ping() (delay float64, err error) {
-	return Ping(a.Address)
-}
-
-// Poll  the affiliated Address and appends it to a.results
-func (a *Address) Poll() (success bool, err error) {
-	if delay, err := a.Ping(); err == nil {
-		a.results = append(a.results, delay)
-		a.Truncate() // enforce max length
-		return true, nil
-	} else {
-		a.results = append(a.results, delay)
-		a.Truncate() // enforce max length
-		return false, err
-	}
-}
-
-// Last returns the last result in [Address.results]. Returns -1 if no previous result
-func (a *Address) Last() (delay float64) {
-	if len(a.results) > 0 {
-		return a.results[len(a.results)-1]
-	} else {
-		return -1
-	}
-}
-
-// [tea.Msg] signatures
-
-type tickMsg time.Time
-
-// A simple boolean flag sent when the program is ready to poll addresses
-type pollMsg bool
-
-// / A simple error message binding to conform to type [tea.Cmd]
-type errMsg struct{ err error }
-
-func (e errMsg) Error() string { return e.err.Error() }
-
-// Style Defintions
-
-// A style for chart headers
-var headerStyle = lipgloss.NewStyle().
-	Bold(true).
-	Italic(true)
-
-// A style for info text
-var infoStyle = lipgloss.NewStyle().
-	Italic(true).
-	Faint(true)
-
-// A style for the base colour
-// var baseColor = lipgloss.NewStyle().
-// 	Foreground(lipgloss.Color("#19535f"))
-
-// A style for the lower colour
-// var lowerColor = lipgloss.NewStyle().
-// 	Foreground(lipgloss.Color("#0b7a75"))
-
-// A style for the middle colour
-// var middleColor = lipgloss.NewStyle().
-// 	Foreground(lipgloss.Color("#d7c9aa"))
-
-// A style for the secondary colour
-var secondaryColor = lipgloss.NewStyle().
-	Foreground(lipgloss.Color("#7b2d26"))
-
-	// A style for the primary colour
-	// var primaryColor = lipgloss.NewStyle().
-	// 	Foreground(lipgloss.Color("#f0f3f5"))