-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
141 lines (116 loc) · 2.3 KB
/
model.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"math/rand"
"time"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)
var (
data ipHolder
quit bool
gotData bool
uiStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
PaddingRight(10).PaddingLeft(10).
Align(lipgloss.Center).Faint(false)
)
func newModel() model {
return model{
spinner: spinner.Model{Spinner: randomSpinner()},
}
}
type model struct {
spinner spinner.Model
}
func (m model) Init() tea.Cmd {
go m.getData(&data, &gotData)
return m.spinner.Tick
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
if quit {
cmd = tea.Quit
} else {
m.spinner, cmd = m.spinner.Update(msg)
}
return m, cmd
}
func (m model) View() string {
var ui string
if gotData {
ui += dataString(data)
quit = true
} else {
ui += m.spinner.View() + " fetching"
}
return uiStyle.Render(ui) + "\n"
}
func (m model) getData(h *ipHolder, q *bool) {
*h = getIp()
*q = true
}
func randomSpinner() spinner.Spinner {
spinners := [...]spinner.Spinner{
//spinner.Dot,
//spinner.Jump,
//spinner.Line,
//spinner.Globe,
//spinner.Moon,
spinner.Meter,
//spinner.Pulse,
//spinner.Monkey,
//spinner.Points,
//spinner.Hamburger,
//spinner.MiniDot,
//spinner.Ellipsis,
gamePadSpinner(),
circleSliceSpinner(),
pleaseWaitSpinner(),
}
return spinners[rand.Intn(len(spinners))]
}
func termSize() [2]int {
x, y, err := term.GetSize(0)
if err != nil {
panic(err)
}
return [2]int{x, y}
}
func TermHeight() int {
return termSize()[1]
}
func TermWidth() int {
return termSize()[0]
}
func gamePadSpinner() spinner.Spinner {
return spinner.Spinner{
Frames: []string{"", "", "", ""},
FPS: time.Second / 4,
}
}
func circleSliceSpinner() spinner.Spinner {
return spinner.Spinner{
Frames: []string{"", "", "", "", "", "", "", ""},
FPS: time.Second / 8,
}
}
func pleaseWaitSpinner() spinner.Spinner {
return spinner.Spinner{
Frames: []string{
"please wait",
"Please wait",
"pLease wait",
"plEase wait",
"pleAse wait",
"pleaSe wait",
"pleasE wait",
"please Wait",
"please wAit",
"please waIt",
"please waiT",
},
FPS: time.Second / 11,
}
}