Repository navigation
Expand file tree
/
Copy pathui.go
More file actions
267 lines (236 loc) · 7.14 KB
/
Copy pathui.go
File metadata and controls
267 lines (236 loc) · 7.14 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package ui
import (
"fmt"
"github.com/charmbracelet/bubbles/key"
"github.com/idursun/jjui/internal/config"
"github.com/idursun/jjui/internal/jj"
"github.com/idursun/jjui/internal/screen"
"github.com/idursun/jjui/internal/ui/bookmarks"
"github.com/idursun/jjui/internal/ui/context"
"github.com/idursun/jjui/internal/ui/git"
"github.com/idursun/jjui/internal/ui/helppage"
"github.com/idursun/jjui/internal/ui/oplog"
"github.com/idursun/jjui/internal/ui/preview"
"github.com/idursun/jjui/internal/ui/revset"
"github.com/idursun/jjui/internal/ui/undo"
"github.com/idursun/jjui/internal/ui/common"
"github.com/idursun/jjui/internal/ui/diff"
"github.com/idursun/jjui/internal/ui/revisions"
"github.com/idursun/jjui/internal/ui/status"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Model struct {
revisions *revisions.Model
oplog *oplog.Model
revsetModel revset.Model
previewModel *preview.Model
previewVisible bool
diff tea.Model
state common.State
error error
status *status.Model
output string
width int
height int
context context.AppContext
keyMap config.KeyMappings[key.Binding]
stacked tea.Model
}
func (m Model) Init() tea.Cmd {
return tea.Sequence(tea.SetWindowTitle("jjui"), m.revisions.Init())
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if _, ok := msg.(common.CloseViewMsg); ok && (m.diff != nil || m.stacked != nil || m.oplog != nil) {
m.diff = nil
m.stacked = nil
m.oplog = nil
return m, nil
}
var cmd tea.Cmd
if m.diff != nil {
m.diff, cmd = m.diff.Update(msg)
return m, cmd
}
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if m.revsetModel.Editing {
m.revsetModel, cmd = m.revsetModel.Update(msg)
m.state = common.Loading
return m, cmd
}
if m.revisions.IsFocused() {
m.revisions, cmd = m.revisions.Update(msg)
return m, cmd
}
if m.previewModel.IsFocused() {
m.previewModel, cmd = m.previewModel.Update(msg)
return m, cmd
}
if m.stacked != nil {
m.stacked, cmd = m.stacked.Update(msg)
return m, cmd
}
switch {
case key.Matches(msg, m.keyMap.Cancel) && m.state == common.Error:
m.state = common.Ready
m.error = nil
case key.Matches(msg, m.keyMap.Cancel) && m.stacked != nil:
m.stacked = nil
case key.Matches(msg, m.keyMap.OpLog.Mode):
m.oplog = oplog.New(m.context, m.width, m.height)
return m, m.oplog.Init()
case key.Matches(msg, m.keyMap.Revset) && m.revisions.InNormalMode():
m.revsetModel, _ = m.revsetModel.Update(revset.EditRevSetMsg{Clear: m.state != common.Error})
case key.Matches(msg, m.keyMap.Git.Mode) && m.revisions.InNormalMode():
m.stacked = git.NewModel(m.context, m.revisions.SelectedRevision(), m.width, m.height)
case key.Matches(msg, m.keyMap.Undo) && m.revisions.InNormalMode():
m.stacked = undo.NewModel(m.context)
cmds = append(cmds, m.stacked.Init())
case key.Matches(msg, m.keyMap.Bookmark.Mode) && m.revisions.InNormalMode():
m.stacked = bookmarks.NewModel(m.context, m.revisions.SelectedRevision(), m.width, m.height)
cmds = append(cmds, m.stacked.Init())
case key.Matches(msg, m.keyMap.Help):
cmds = append(cmds, common.ToggleHelp)
return m, tea.Batch(cmds...)
case key.Matches(msg, m.keyMap.Preview.Mode):
m.previewVisible = !m.previewVisible
cmds = append(cmds, common.SelectionChanged)
return m, tea.Batch(cmds...)
case key.Matches(msg, m.keyMap.Preview.ToggleFocus):
if !m.previewVisible {
cmds = append(cmds, common.SelectionChanged)
}
m.previewVisible = true
cmds = append(cmds, preview.Focus)
return m, tea.Batch(cmds...)
}
case common.ToggleHelpMsg:
if m.stacked == nil {
m.stacked = helppage.New(m.context)
if p, ok := m.stacked.(common.Sizable); ok {
p.SetHeight(m.height - 2)
p.SetWidth(m.width)
}
} else {
m.stacked = nil
}
return m, nil
case common.ShowDiffMsg:
m.diff = diff.New(string(msg), m.width, m.height)
cmds = append(cmds, m.diff.Init())
return m, tea.Batch(cmds...)
case common.CommandCompletedMsg:
m.output = msg.Output
case common.UpdateRevisionsFailedMsg:
m.state = common.Error
m.output = msg.Output
m.error = msg.Err
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
if m.previewVisible {
m.revisions.SetWidth(m.width / 2)
} else {
m.revisions.SetWidth(m.width)
}
m.revisions.SetHeight(m.height - 4)
if m.previewVisible {
m.previewModel.SetWidth(m.width / 2)
m.previewModel.SetHeight(m.height - 4)
}
if s, ok := m.stacked.(common.Sizable); ok {
s.SetWidth(m.width - 2)
s.SetHeight(m.height - 2)
}
m.status.SetWidth(m.width)
}
m.status, cmd = m.status.Update(msg)
cmds = append(cmds, cmd)
if m.stacked != nil {
m.stacked, cmd = m.stacked.Update(msg)
cmds = append(cmds, cmd)
}
if m.oplog != nil {
m.oplog, cmd = m.oplog.Update(msg)
cmds = append(cmds, cmd)
} else {
m.revisions, cmd = m.revisions.Update(msg)
cmds = append(cmds, cmd)
}
if m.previewVisible {
m.previewModel, cmd = m.previewModel.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
if m.diff != nil {
return m.diff.View()
}
topView := m.revsetModel.View()
if m.state == common.Error {
topView += fmt.Sprintf("\n%s\n", m.output)
}
topViewHeight := lipgloss.Height(topView)
if m.oplog != nil {
m.status.SetMode("oplog")
m.status.SetHelp(m.oplog)
} else {
m.status.SetHelp(m.revisions)
m.status.SetMode(m.revisions.CurrentOperation().Name())
}
footer := m.status.View()
footerHeight := lipgloss.Height(footer)
leftView := m.renderLeftView(footerHeight, topViewHeight)
previewView := ""
if m.previewVisible {
m.previewModel.SetWidth(m.width - lipgloss.Width(leftView))
m.previewModel.SetHeight(m.height - footerHeight - topViewHeight)
previewView = m.previewModel.View()
}
centerView := lipgloss.JoinHorizontal(lipgloss.Left, leftView, previewView)
if m.stacked != nil {
stackedView := m.stacked.View()
sx := (m.width - lipgloss.Width(stackedView)) / 2
sy := (m.height - lipgloss.Height(stackedView)) / 2
centerView = screen.Stacked(centerView, stackedView, sx, sy)
}
return lipgloss.JoinVertical(0, topView, centerView, footer)
}
func (m Model) renderLeftView(footerHeight int, topViewHeight int) string {
leftView := ""
w := m.width
if m.previewVisible {
w = m.width / 2
if m.previewModel.IsFocused() {
w = 4
}
}
if m.oplog != nil {
m.oplog.SetWidth(w)
m.oplog.SetHeight(m.height - footerHeight - topViewHeight)
leftView = m.oplog.View()
} else {
m.revisions.SetWidth(w)
m.revisions.SetHeight(m.height - footerHeight - topViewHeight)
leftView = m.revisions.View()
}
return leftView
}
func New(c context.AppContext) tea.Model {
defaultRevSet, _ := c.RunCommandImmediate(jj.ConfigGet("revsets.log"))
revisionsModel := revisions.New(c)
previewModel := preview.New(c)
statusModel := status.New(c)
return Model{
context: c,
keyMap: c.KeyMap(),
state: common.Loading,
revisions: &revisionsModel,
previewModel: &previewModel,
status: &statusModel,
revsetModel: revset.New(string(defaultRevSet)),
}
}