Просмотр исходного кода

Added docs for tui, refactored Model to Peak

arianagiroux 6 дней назад
Родитель
Сommit
0470a3a697
4 измененных файлов с 45 добавлено и 57 удалено
  1. 3 4
      cmd/pingo.go
  2. 1 1
      cmd/pingo_test.go
  3. 34 45
      tui.go
  4. 7 7
      tui_test.go

+ 3 - 4
cmd/pingo.go

@@ -20,7 +20,7 @@ import (
 type md struct {
 	viewport viewport.Model
 	speed    time.Duration
-	p        pingo.Model
+	p        pingo.Peak
 }
 
 type timingMsg time.Time
@@ -74,8 +74,7 @@ func (m md) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	m.viewport, cmd = m.viewport.Update(msg)
 	cmds = append(cmds, cmd)
 
-	model, cmd := m.p.Update(msg)
-	m.p = model.(pingo.Model)
+	m.p, cmd = m.p.Update(msg)
 	cmds = append(cmds, cmd)
 
 	return m, tea.Batch(cmds...)
@@ -111,7 +110,7 @@ func main() {
 		return
 	}
 
-	p := tea.NewProgram(md{p: pingo.InitialModel(hosts, 20, 10, *chartHeight), speed: time.Duration(*speed)})
+	p := tea.NewProgram(md{p: pingo.InitialPeakBubble(hosts, 20, 10, *chartHeight), speed: time.Duration(*speed)})
 
 	if _, err := p.Run(); err != nil {
 		fmt.Printf("Alas, there's been an error: %v", err)

+ 1 - 1
cmd/pingo_test.go

@@ -11,7 +11,7 @@ import (
 )
 
 func spawnMD(hosts []string, width, height, chartHeight int) md {
-	return md{p: pingo.InitialModel(hosts, width, height, chartHeight)}
+	return md{p: pingo.InitialPeakBubble(hosts, width, height, chartHeight)}
 }
 
 func Test_md_Init(t *testing.T) {

+ 34 - 45
tui.go

@@ -12,7 +12,8 @@
 // NOTE: the bubbletea framework and subsequent "bubble" concept are beyond the
 // scope of this documentation. For more, see the Readme.
 //
-// Pingo defines a constructor function for the Model. It takes three arguments:
+// Pingo defines a constructor function for the Peak bubble. It takes three
+// arguments:
 //
 //   - addresses([]string): the hosts to ping. The length must be greater than
 //     or equal to 1
@@ -27,30 +28,14 @@
 //
 //     NOTE: chartHeight is ignored when only one address or host is provided
 //
-// For more, please please see InitialModel()
+// For more, please please see InitialPeakBubble()
 //
-// Pingo defines two bubbletea.Cmd functions:
-//
-//   - Model.Tick() tea.Cmd: emits a bubbletea.TickMsg after the time.Duration
-//     specified via Model.UpdateSpeed
-//
-//     NOTE: Model.Tick() is optional. If you choose not to use Model.Tick(),
-//     it is recommended to enforce some minimum rate mechanism for calling
-//     Poll(). Some servers maintain a ping rate limit, and is is possible to
-//     exceed this rate trivially with the Poll() function. (Trust us, we know
-//     from experience)
-//
-//     NOTE: Model.Tick() is automatically emit by Model.Init() - therefore,
-//     you can control the timing of polling by overloading the Init function.
+// Pingo defines the following bubbletea.Cmd functions:
 //
 //   - Model.Poll() tea.Msg: used to asynchronously call all Model.Addresses.Poll()
 //     functions.
 //
-//     NOTE: Model.Poll() is automatically injected into the Model.Update()
-//     life cycle after Model.Tick() resolves by Model.Update(). Functionally,
-//     this means you can omit either Model.Tick() or Model.Poll(), respectively.
-//
-// For more, see the Readme or ./examples
+// For an example implementation, see cmd/pingo.go
 package pingo
 
 import (
@@ -111,16 +96,17 @@ type ( // tea.Msg signatures
 	}
 )
 
-// Bubbletea model
-type Model struct {
+// Bubbletea bubble
+type Peak struct {
 	Addresses   []Address // as defined in internal/tui/types.go
 	ChartHeight int
 	Height      int
 	Width       int
 }
 
-func InitialModel(addresses []string, width, height, chartHeight int) Model {
-	var model Model
+// Instantiates a Peak bubble with the provided arguments.
+func InitialPeakBubble(addresses []string, width, height, chartHeight int) Peak {
+	var model Peak
 	model.ChartHeight = chartHeight
 	model.Width = width
 	model.Height = height
@@ -134,58 +120,60 @@ func InitialModel(addresses []string, width, height, chartHeight int) Model {
 	return model
 }
 
-func (m Model) Init() tea.Cmd {
-	return m.Poll()
+// Provides initial polling for the bubble. Returns the result of Peak.Poll
+func (p Peak) Init() tea.Cmd {
+	return p.Poll()
 }
 
-func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+// The update function for the Peak bubble.
+func (p Peak) Update(msg tea.Msg) (Peak, tea.Cmd) {
 	var cmd tea.Cmd
 	var cmds []tea.Cmd
 
 	switch msg := msg.(type) {
 	case pollResultMsg:
-		m.Addresses[msg.index].Results = msg.results
+		p.Addresses[msg.index].Results = msg.results
 	}
-	for i := range m.Addresses {
-		m.Addresses[i].MaxResults = m.Width
+	for i := range p.Addresses {
+		p.Addresses[i].MaxResults = p.Width
 	}
 	cmds = append(cmds, cmd)
 
-	return m, tea.Batch(cmds...)
+	return p, tea.Batch(cmds...)
 }
 
-func (m Model) View() tea.View {
+func (p Peak) View() tea.View {
 	var content string
-	for _, address := range m.Addresses {
+	for _, address := range p.Addresses {
 		if len(address.Results) == 0 {
 			content = content + fmt.Sprintf("\n%s\tloading...", headerStyle.Render(address.Address))
-		} else if m.Width != 0 && m.Height != 0 {
+		} else if p.Width != 0 && p.Height != 0 {
 			if slices.Contains(address.Results, -1) {
 				content = content + fmt.Sprintf("\n%s",
-					blockStyle.Width(m.Width).Render(headerStyle.Render(
+					blockStyle.Width(p.Width).Render(headerStyle.Render(
 						secondaryColor.Render(address.Address),
 						infoStyle.Render("(connection unstable)"),
 					),
 					))
 			} else {
 				content = content + fmt.Sprintf("\n%s",
-					blockStyle.Width(m.Width).Render(headerStyle.Render(address.Address)))
+					blockStyle.Width(p.Width).Render(headerStyle.Render(address.Address)))
 			}
 
 			// Linechart
 			// set chartHeight - vertical margin
-			// chartHeight := m.Height - 2 // m.getVerticalMargin()
+			// chartHeight := p.Height - 2 // p.getVerticalMargin()
 
 			var slc streamlinechart.Model
 
-			if m.ChartHeight == 0 && len(m.Addresses) == 1 { // catch user specified fullscreen
+			if p.ChartHeight == 0 && len(p.Addresses) == 1 { // catch user specified fullscreen
 				// render chart at fullscreen
-				slc = streamlinechart.New(m.Width, m.Height-1)
-			} else if m.ChartHeight == 0 && len(m.Addresses) > 1 { // catch user specified fullscreen
+				slc = streamlinechart.New(p.Width, p.Height-1)
+			} else if p.ChartHeight == 0 && len(p.Addresses) > 1 { // catch user specified fullscreen
 				// render chart at fullscreen minus a few lines to hint at scrolling
-				slc = streamlinechart.New(m.Width, m.Height-2)
+				slc = streamlinechart.New(p.Width, p.Height-2)
 			} else {
-				slc = streamlinechart.New(m.Width, m.ChartHeight-1)
+				slc = streamlinechart.New(p.Width, p.ChartHeight-1)
 			}
 
 			for _, v := range address.Results {
@@ -203,11 +191,12 @@ func (m Model) View() tea.View {
 	return v
 }
 
-// Returns a batched set of tea.Cmd functions for each address.
-func (m Model) Poll() tea.Cmd {
+// Returns a batched set of tea.Cmd that call Address.Poll functions for each
+// address.
+func (p Peak) Poll() tea.Cmd {
 	var cmds []tea.Cmd
 
-	for i, element := range m.Addresses {
+	for i, element := range p.Addresses {
 		cmds = append(cmds, func() tea.Msg {
 			results, err := element.Poll()
 			return pollResultMsg{results: results, err: err, index: i}

+ 7 - 7
tui_test.go

@@ -8,8 +8,8 @@ import (
 	"github.com/stretchr/testify/assert"
 )
 
-func spawnTestModel(addresses []string, points []float64, width, height, cHeight int) Model {
-	testModel := InitialModel(addresses, cHeight, width, height)
+func spawnTestModel(addresses []string, points []float64, width, height, cHeight int) Peak {
+	testModel := InitialPeakBubble(addresses, cHeight, width, height)
 	for i := range testModel.Addresses {
 		testModel.Addresses[i].Results = points
 	}
@@ -17,12 +17,12 @@ func spawnTestModel(addresses []string, points []float64, width, height, cHeight
 	return testModel
 }
 
-func Test_InitialModel(t *testing.T) {
+func Test_InitialPeakBubble(t *testing.T) {
 	addresses := []string{"127.0.0.1", "cantresolvethisever"}
 	chartHeight := 10
-	model := InitialModel(addresses, 80, 24, chartHeight)
+	model := InitialPeakBubble(addresses, 80, 24, chartHeight)
 
-	assert.Implements(t, new(tea.Model), model)
+	// assert.Implements(t, new(tea.Model), model)
 
 	assert.Equal(t, chartHeight, model.ChartHeight)
 	assert.Equal(t, 80, model.Width)
@@ -47,7 +47,7 @@ func Test_Model_Update_handle_pollMsg(t *testing.T) {
 
 	model, batch := testModel.Update(msg)
 	assert.IsType(t, *new(tea.Cmd), batch)
-	assert.Equal(t, msg.results, model.(Model).Addresses[0].Results)
+	assert.Equal(t, msg.results, model.Addresses[0].Results)
 }
 
 func Test_Model_View(t *testing.T) {
@@ -139,7 +139,7 @@ func Test_Model_View_has_big_charts(t *testing.T) {
 func Test_Model_cmd_Poll(t *testing.T) {
 	addresses := []string{"127.0.0.1", "cantresolvethisever"}
 	chartHeight := 10
-	model := InitialModel(addresses, chartHeight, 80, 24)
+	model := InitialPeakBubble(addresses, chartHeight, 80, 24)
 	batch := model.Poll()
 	msgs := []tea.Cmd(batch().(tea.BatchMsg)) // type assert for legibility
 	for i, cmd := range msgs {