-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
97 lines (86 loc) · 1.85 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package dig
import (
"fmt"
"os"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
)
type responseMsg struct {
domains []list.Item
quitting bool
}
type responseChannel struct {
domains []list.Item
}
type startModel struct {
sub chan responseChannel
responses int
spinner spinner.Model
quitting bool
domains []list.Item
}
func callNamecheap(sub chan responseChannel) tea.Cmd {
//this is where the activity is called
return func() tea.Msg {
for {
domains := GetDomains()
sub <- responseChannel{domains: domains}
}
}
}
func waitForActivity(sub chan responseChannel) tea.Cmd {
return func() tea.Msg {
return responseChannel(<-sub)
}
}
func (m startModel) Init() tea.Cmd {
return tea.Batch(
m.spinner.Tick,
callNamecheap(m.sub),
waitForActivity(m.sub),
)
}
func (m startModel) View() string {
s := fmt.Sprintf("Fetching your domains from namecheap %s", m.spinner.View())
if m.quitting {
// s += "\n"
}
return s
}
func (m startModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tea.KeyMsg:
m.quitting = true
return m, tea.Quit
case responseChannel:
response := msg.(responseChannel)
if response.domains != nil {
m.domains = response.domains
return m, tea.Quit
} else {
return m, waitForActivity(m.sub)
}
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
default:
return m, nil
}
}
func StartProgram() []list.Item {
// start spinner and get domains
model := startModel{
sub: make(chan responseChannel),
spinner: spinner.New(),
}
p := tea.NewProgram(model)
returnModel, err := p.Run()
if err != nil {
fmt.Println("could not start program", err)
os.Exit(1)
}
return returnModel.(startModel).domains
}