-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmodel.go
244 lines (198 loc) · 5.27 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package view
import (
"fmt"
"strings"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
"github.com/muesli/reflow/wordwrap"
)
var tickDuration = 250 * time.Millisecond
// LogMsg sends data to the log panel
type LogMsg string
type ErrMsg error
type TickMsg time.Time
type model struct {
height int
width int
left int
top int
viewport viewport.Model
statusbar StatusModel
messages []string
follow bool
logger logger.Logger
}
type KeyMap struct {
Up key.Binding
Down key.Binding
Quit key.Binding
Filter key.Binding
Level key.Binding
}
var DefaultKeyMap = KeyMap{
Up: key.NewBinding(
key.WithKeys("k", "up"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("j", "down"),
key.WithHelp("↓/j", "move down"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
Filter: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "filter logs"),
),
Level: key.NewBinding(
key.WithKeys("l"),
key.WithHelp("l", "log level"),
),
}
func initialModel() model {
status := NewStatus()
return model{
messages: []string{},
statusbar: status,
follow: true,
left: 1,
}
}
func (m model) Init() tea.Cmd {
// init child models
return tea.Batch(m.statusbar.Init(), m.tick())
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// when receiving a tick all components that require
// tick updates must be called
// tick also must be called to ensure that the tick
// continues
case TickMsg:
// update the status model so that the spinner updates
sm, sCmd := m.statusbar.Update(msg)
m.statusbar = sm
return m, tea.Batch(sCmd, m.tick())
case tea.MouseMsg:
switch msg.Type {
case tea.MouseWheelDown:
fallthrough
case tea.MouseWheelUp:
m.follow = false
var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)
return m, cmd
}
case tea.KeyMsg:
switch {
case key.Matches(msg, DefaultKeyMap.Filter):
m.follow = true
case key.Matches(msg, DefaultKeyMap.Up):
m.follow = false
case key.Matches(msg, DefaultKeyMap.Down):
// noop
case key.Matches(msg, DefaultKeyMap.Level):
if m.logger.IsDebug() {
m.logger.SetLevel(logger.LogLevelInfo)
} else {
m.logger.SetLevel(logger.LogLevelDebug)
}
case key.Matches(msg, DefaultKeyMap.Quit):
return m, tea.Quit
}
case tea.WindowSizeMsg:
// this message is always fired when first displaying the view
// then after every terminal resize
headerHeight := 2 //lipgloss.Height(m.headerView())
footerHeight := 2 //lipgloss.Height(m.footerView())
m.height = msg.Height
m.width = msg.Width
m.viewport = viewport.New(m.width-m.left, m.height-headerHeight-footerHeight-m.top)
return m, nil
case LogMsg:
// wrap the current message over multiple lines to ensure that it
// does not go beyond the bounds of the terminal
message := wrapMessage(string(msg), m.viewport.Width)
// append the new log lines to the current buffer
m.messages = appendMessage(m.messages, message)
// set the content on the viewport
m.viewport.SetContent(strings.Join(m.messages, "\n"))
if m.follow {
m.viewport.GotoBottom()
}
// render the log lines
var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)
return m, cmd
case StatusMsg:
var cmd tea.Cmd
m.statusbar, cmd = m.statusbar.Update(msg)
return m, cmd
// we handle errors just like any other message
case ErrMsg:
//m.err = msg
return m, nil
}
return m, nil
}
func (m model) View() string {
// if there is a 0 width do not draw
if m.width-m.left < 1 {
return ""
}
return lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().MarginLeft(m.left).Render(m.headerView()),
lipgloss.NewStyle().MarginLeft(m.left).Render(m.viewportView()),
lipgloss.NewStyle().MarginLeft(m.left).Render(m.footerView()),
)
}
func (m model) viewportView() string {
return m.viewport.View()
}
func (m model) headerView() string {
title := "Jumppad Dev Mode"
return lipgloss.JoinVertical(lipgloss.Top, title, "")
}
func (m model) footerView() string {
level := "debug"
if m.logger.IsDebug() {
level = "info"
}
follow := ", [f] follow logs "
if m.follow {
follow = " "
}
keys := lipgloss.NewStyle().Foreground(lipgloss.Color("37")).Render(fmt.Sprintf("[l] change log level to %s, [mouse wheel up/down] scroll logs%s", level, follow))
return lipgloss.JoinVertical(lipgloss.Top, keys, m.statusbar.View())
}
// tick ensures that there are regular heartbeats for components that need them
func (m model) tick() tea.Cmd {
return tea.Tick(tickDuration, func(t time.Time) tea.Msg {
return TickMsg(time.Now())
})
}
func wrapMessage(m string, width int) string {
message := wordwrap.String(m, width)
return strings.TrimSuffix(message, "\n")
}
func appendMessage(messages []string, message string) []string {
lines := strings.Split(message, "\n")
for i, l := range lines {
if i > 0 {
lines[i] = fmt.Sprintf(" %s", l)
}
}
message = strings.Join(lines, "\n")
messages = append(messages, message)
// if greater than 10000 pop first
if len(messages) > 10000 {
messages = messages[1:]
}
return messages
}