generated from charmbracelet/bubbletea-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (154 loc) · 3.33 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"github.com/AlbertClo/pylon/color"
"github.com/AlbertClo/pylon/keybind"
"github.com/AlbertClo/pylon/layout"
"github.com/AlbertClo/pylon/types"
"github.com/AlbertClo/pylon/view"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"os"
)
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type Model struct {
spinner spinner.Model
counter int
message string
quitting bool
err error
keys keybind.Shortcuts
menuItems []types.MenuItem
selected int
windowSize struct {
width int
height int
}
}
func (m Model) GetCounter() int {
return m.counter
}
func (m Model) GetMessage() string {
return m.message
}
func (m Model) GetKeys() keybind.Shortcuts {
return m.keys
}
func (m Model) GetMenuItems() []types.MenuItem {
return m.menuItems
}
func (m Model) GetSelectedItem() int {
return m.selected
}
func getMenuItems() []types.MenuItem {
return []types.MenuItem{
{
Name: "Vite",
Start: "npm run dev",
Stop: "stop",
Running: false,
},
{
Name: "Server",
Start: "php artisan serve --host localhost --port 8000",
Stop: "stop",
Running: false,
},
{
Name: "Pail",
Start: "php artisan pail -vv",
Stop: "stop",
Running: false,
},
{
Name: "Debug",
Start: "php artisan dump-server",
Stop: "stop",
Running: false,
},
{
Name: "pwd",
Start: "pwd",
Stop: "stop",
Running: false,
},
}
}
func initialModel() Model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(color.Primary)
return Model{
spinner: s,
counter: 0,
message: "",
keys: keybind.New(),
menuItems: getMenuItems(),
selected: 0, // Start with first item selected
}
}
func (m Model) Init() tea.Cmd {
return m.spinner.Tick
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.Quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, m.keys.Up):
if m.selected > 0 {
m.selected--
} else {
m.selected = len(m.menuItems) - 1
}
return m, nil
case key.Matches(msg, m.keys.Down):
if m.selected < len(m.menuItems)-1 {
m.selected++
} else {
m.selected = 0
}
return m, nil
case key.Matches(msg, m.keys.StartStop):
if m.menuItems[m.selected].Running {
m.message = m.menuItems[m.selected].Stop
m.menuItems[m.selected].Running = false
} else {
m.message = m.menuItems[m.selected].Start
m.menuItems[m.selected].Running = true
}
return m, nil
}
case tea.WindowSizeMsg:
m.windowSize.width = msg.Width
m.windowSize.height = msg.Height
m.message = fmt.Sprintf("Window size: %d x %d", msg.Width, msg.Height)
return m, nil
case error:
m.err = msg
return m, nil
}
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
func (m Model) View() string {
if m.err != nil {
return m.err.Error()
}
return layout.New(m.windowSize.width, m.windowSize.height).
SetLeftContent(view.RenderLeftContent(m)).
SetRightContent(view.RenderRightContent(m)).
SetBottomContent(view.RenderBottomContent(m)).
Render()
}