Browse Source

Working initial bubbletea implementation

arianagiroux 4 tuần trước cách đây
mục cha
commit
12811ae704
3 tập tin đã thay đổi với 58 bổ sung19 xóa
  1. 7 0
      internal/tui/cmds.go
  2. 42 7
      internal/tui/tui.go
  3. 9 12
      main.go

+ 7 - 0
internal/tui/cmds.go

@@ -0,0 +1,7 @@
+package tui
+
+type pollMsg bool
+
+type errMsg struct{ err error }
+
+func (e errMsg) Error() string { return e.err.Error() }

+ 42 - 7
internal/tui/tui.go

@@ -1,6 +1,8 @@
 package tui
 
 import (
+	"fmt"
+
 	tea "github.com/charmbracelet/bubbletea"
 )
 
@@ -8,7 +10,7 @@ import (
 //
 // BUG(state): how do declare pointer here?
 type Model struct {
-	Addresses []Address // as defined in [internal/tui/types.go]
+	Addresses []Address // as defined in internal/tui/types.go
 }
 
 func InitialModel(addresses []string) Model {
@@ -21,14 +23,47 @@ func InitialModel(addresses []string) Model {
 	return model
 }
 
-func (m *Model) Init() tea.Cmd {
-	return nil
+func (m Model) Init() tea.Cmd {
+	return m.Poll
+}
+
+func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+	switch msg.(type) {
+	// if case is KeyMsg (keypress)
+	case tea.KeyMsg:
+		return m, tea.Quit
+		// switch msg.String() {
+
+		// case "ctrl+c", "q": // These keys should exit the program.
+		// 	return m, tea.Quit
+		// }
+	case pollMsg:
+		m.Poll()
+	}
+	return m, m.Poll
 }
 
-func (m *Model) View() string {
-	return ""
+// A wrapper for the underlying [tui.Address.Poll] function. For each address in
+// [tui.Model.Addresses], run its respective Poll function and update [tui.Model]
+//
+// NOTE(async): this function fully blocks execution of the current thread.
+func (m Model) Poll() tea.Msg {
+	for i, element := range m.Addresses {
+		element.Poll()
+		m.Addresses[i] = element
+	}
+	return true
 }
 
-func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
-	return m, nil
+func (m Model) View() string {
+	output := "Results:\n"
+	for _, address := range m.Addresses {
+		last := address.Last()
+		if last == -1 {
+			output = output + fmt.Sprintf("%s: loading...\n", address.Address)
+		} else {
+			output = output + fmt.Sprintf("%s: %f\n", address.Address, last)
+		}
+	}
+	return output
 }

+ 9 - 12
main.go

@@ -2,26 +2,23 @@ package main
 
 import (
 	"fmt"
+	"os"
 	"pingo/internal/tui"
+
+	tea "github.com/charmbracelet/bubbletea"
 )
 
 func main() {
-	var model = tui.InitialModel([]string{"google.ca"})
-	fmt.Printf("%v\n", model)
+	var model = tui.InitialModel([]string{"google.ca", "resolve", "requiem",
+		"grimoire", "bastion"})
 
-	for _, element := range model.Addresses {
-		fmt.Printf("\t%v\n", element)
-		element.Poll()
-		fmt.Printf("\t%v\n", element)
+	p := tea.NewProgram(model)
+	if _, err := p.Run(); err != nil {
+		fmt.Printf("Alas, there's been an error: %v", err)
+		os.Exit(1)
 	}
-	fmt.Printf("%v\n", model)
 }
 
-// p := tea.NewProgram(tui.InitialModel())
-// if _, err := p.Run(); err != nil {
-// 	fmt.Printf("Alas, there's been an error: %v", err)
-// 	os.Exit(1)
-// }
 // flag.Parse()
 // hosts := flag.Args()