-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
191 lines (150 loc) · 4.44 KB
/
Copy pathcontroller.go
File metadata and controls
191 lines (150 loc) · 4.44 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Package bubblon enables a model-stack architecture for Bubble Tea apps.
package bubblon
import (
"errors"
tea "charm.land/bubbletea/v2"
)
// Closed is a message sent to the parent model indicating that the top
// model has been closed. The message is not sent if the model is replaced.
type Closed struct{}
// openMsg is an internal message wrapping a model to open.
type openMsg struct {
model tea.Model
}
// closeMsg is an internal message for closing the top model.
type closeMsg struct{}
// replaceMsg is an internal message for replacing the top model
// with a new one.
type replaceMsg struct {
model tea.Model
}
// replaceAllMsg is an internal message for closing all the models
// and opening a new one.
type replaceAllMsg struct {
model tea.Model
}
// failMsg is an internal message for setting the error in the controller
// before quitting the app.
type failMsg struct {
err error
}
// ErrNilModel is returned when attempting to initialize a Controller with a nil model.
var ErrNilModel = errors.New("model cannot be nil")
// Open is a command to push a new model onto the stack.
// The new model will become the active model receiving updates and rendering.
func Open(model tea.Model) tea.Cmd {
return Cmd(openMsg{model: model})
}
// Close is a command instructing bubblon to close the current model.
// A notification to the parent model is sent on closure.
func Close() tea.Msg {
return closeMsg{}
}
// Replace combines closing the current model and opening a new one in a single command.
func Replace(model tea.Model) tea.Cmd {
return Cmd(replaceMsg{model: model})
}
// ReplaceAll closes all the models and opens a new one in a single command.
func ReplaceAll(model tea.Model) tea.Cmd {
return Cmd(replaceAllMsg{model: model})
}
// Fail is a command to propagate the error to the controller and quit the app.
func Fail(err error) tea.Cmd {
return Cmd(failMsg{err: err})
}
// Controller implements a stack-based navigation model for Bubble Tea apps.
// It manages a stack of tea.Model instances, where only the top model receives
// updates and renders. In case of the Fail message, Err is set to the error
// from the message.
type Controller struct {
models []tea.Model
Err error
}
// Ensure Controller implements the tea.Model interface.
var _ tea.Model = Controller{}
// New creates a new Controller initialized with the given model.
// Returns an error if the model is nil.
func New(model tea.Model) (Controller, error) {
if model != nil {
return Controller{models: []tea.Model{model}, Err: nil}, nil
}
return Controller{}, ErrNilModel
}
// Init initializes the initial model, if one exists.
func (c Controller) Init() tea.Cmd {
if top := c.top(); top != nil {
return c.top().Init()
}
return nil
}
// Update handles incoming messages to update the state and
// delegate messages to the top model.
func (c Controller) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case openMsg:
if msg.model != nil {
c.push(msg.model)
return c, tea.Batch(msg.model.Init(), tea.RequestWindowSize)
}
case closeMsg:
c.pop()
if len(c.models) > 0 {
return c, tea.Batch(Cmd(Closed{}), tea.RequestWindowSize)
}
case replaceMsg:
c.pop()
return c.Update(openMsg(msg))
case replaceAllMsg:
for i := range c.models {
c.models[i] = nil
}
c.models = c.models[:0]
return c.Update(openMsg(msg))
case failMsg:
c.models = nil
c.Err = msg.err
return c, tea.Quit
default:
if top := c.top(); top != nil {
m, cmd := top.Update(msg)
c.models[len(c.models)-1] = m
return c, cmd
}
// Be able to kill the program if there are no models left
if key, ok := msg.(tea.KeyPressMsg); ok {
if key.Code == 'c' && key.Mod == tea.ModCtrl {
return c, tea.Interrupt
}
}
}
return c, nil
}
// View renders the view of the top model on the stack.
// Returns an empty string if there is no model.
func (c Controller) View() tea.View {
if top := c.top(); top != nil {
return c.top().View()
}
return tea.NewView("")
}
// Cmd is a helper function that wraps a tea.Msg as a tea.Cmd.
// Should be used only for static messages.
func Cmd(msg tea.Msg) tea.Cmd {
return func() tea.Msg { return msg }
}
func (c *Controller) push(m tea.Model) {
c.models = append(c.models, m)
}
func (c *Controller) pop() {
num := len(c.models)
if num > 0 {
c.models[num-1] = nil
c.models = c.models[:num-1]
}
}
func (c Controller) top() tea.Model {
if len(c.models) > 0 {
return c.models[len(c.models)-1]
}
return nil
}