-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathpreview.go
More file actions
271 lines (226 loc) · 7.2 KB
/
preview.go
File metadata and controls
271 lines (226 loc) · 7.2 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
268
269
270
271
package ui
import (
"claude-squad/session"
"fmt"
"strings"
"github.com/charmbracelet/bubbles/viewport"
"github.com/charmbracelet/lipgloss"
)
var previewPaneStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"})
type PreviewPane struct {
width int
height int
previewState previewState
isScrolling bool
viewport viewport.Model
}
type previewState struct {
// fallback is true if the preview pane is displaying fallback text
fallback bool
// text is the text displayed in the preview pane
text string
}
func NewPreviewPane() *PreviewPane {
return &PreviewPane{
viewport: viewport.New(0, 0),
}
}
func (p *PreviewPane) SetSize(width, maxHeight int) {
p.width = width
p.height = maxHeight
p.viewport.Width = width
p.viewport.Height = maxHeight
}
// setFallbackState sets the preview state with fallback text and a message
func (p *PreviewPane) setFallbackState(message string) {
p.previewState = previewState{
fallback: true,
text: lipgloss.JoinVertical(lipgloss.Center, FallBackText, "", message),
}
}
// Updates the preview pane content with the tmux pane content
func (p *PreviewPane) UpdateContent(instance *session.Instance) error {
switch {
case instance == nil:
p.setFallbackState("No agents running yet. Spin up a new instance with 'n' to get started!")
return nil
case instance.Status == session.Paused:
p.setFallbackState(lipgloss.JoinVertical(lipgloss.Center,
"Session is paused. Press 'r' to resume.",
"",
lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{
Light: "#FFD700",
Dark: "#FFD700",
}).
Render(fmt.Sprintf(
"The instance can be checked out at '%s' (copied to your clipboard)",
instance.Branch,
)),
))
return nil
}
var content string
var err error
// If in scroll mode but haven't captured content yet, do it now
if p.isScrolling && p.viewport.Height > 0 && len(p.viewport.View()) == 0 {
// Capture full pane content including scrollback history using capture-pane -p -S -
content, err = instance.PreviewFullHistory()
if err != nil {
return err
}
// Set content in the viewport
footer := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#808080", Dark: "#808080"}).
Render("ESC to exit scroll mode")
p.viewport.SetContent(lipgloss.JoinVertical(lipgloss.Left, content, footer))
} else if !p.isScrolling {
// In normal mode, use the usual preview
content, err = instance.Preview()
if err != nil {
return err
}
// Always update the preview state with content, even if empty
// This ensures that newly created instances will display their content immediately
if len(content) == 0 && !instance.Started() && instance.Status == session.Loading {
p.setFallbackState("Starting session...")
} else if len(content) == 0 && !instance.Started() {
p.setFallbackState("Please enter a name for the instance.")
} else {
// Update the preview state with the current content
p.previewState = previewState{
fallback: false,
text: content,
}
}
}
return nil
}
// Returns the preview pane content as a string.
func (p *PreviewPane) String() string {
if p.width == 0 || p.height == 0 {
return strings.Repeat("\n", p.height)
}
if p.previewState.fallback {
// Calculate available height for fallback text
availableHeight := p.height - 3 - 4 // 2 for borders, 1 for margin, 1 for padding
// Count the number of lines in the fallback text
fallbackLines := len(strings.Split(p.previewState.text, "\n"))
// Calculate padding needed above and below to center the content
totalPadding := availableHeight - fallbackLines
topPadding := 0
bottomPadding := 0
if totalPadding > 0 {
topPadding = totalPadding / 2
bottomPadding = totalPadding - topPadding // accounts for odd numbers
}
// Build the centered content
var lines []string
if topPadding > 0 {
lines = append(lines, strings.Repeat("\n", topPadding))
}
lines = append(lines, p.previewState.text)
if bottomPadding > 0 {
lines = append(lines, strings.Repeat("\n", bottomPadding))
}
// Center both vertically and horizontally
return previewPaneStyle.
Width(p.width).
Align(lipgloss.Center).
Render(strings.Join(lines, ""))
}
// If in copy mode, use the viewport to display scrollable content
if p.isScrolling {
return p.viewport.View()
}
// Normal mode display
// Calculate available height accounting for border and margin
availableHeight := p.height - 1 // 1 for ellipsis
lines := strings.Split(p.previewState.text, "\n")
// Truncate if we have more lines than available height
if availableHeight > 0 {
if len(lines) > availableHeight {
lines = lines[:availableHeight]
lines = append(lines, "...")
} else {
// Pad with empty lines to fill available height
padding := availableHeight - len(lines)
lines = append(lines, make([]string, padding)...)
}
}
content := strings.Join(lines, "\n")
rendered := previewPaneStyle.Width(p.width).Render(content)
return rendered
}
// ScrollUp scrolls up in the viewport
func (p *PreviewPane) ScrollUp(instance *session.Instance) error {
if instance == nil || instance.Status == session.Paused {
return nil
}
if !p.isScrolling {
// Entering scroll mode - capture entire pane content including scrollback history
content, err := instance.PreviewFullHistory()
if err != nil {
return err
}
// Set content in the viewport
footer := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#808080", Dark: "#808080"}).
Render("ESC to exit scroll mode")
contentWithFooter := lipgloss.JoinVertical(lipgloss.Left, content, footer)
p.viewport.SetContent(contentWithFooter)
// Position the viewport at the bottom initially
p.viewport.GotoBottom()
p.isScrolling = true
return nil
}
// Already in scroll mode, just scroll the viewport
p.viewport.LineUp(1)
return nil
}
// ScrollDown scrolls down in the viewport
func (p *PreviewPane) ScrollDown(instance *session.Instance) error {
if instance == nil || instance.Status == session.Paused {
return nil
}
if !p.isScrolling {
// Entering scroll mode - capture entire pane content including scrollback history
content, err := instance.PreviewFullHistory()
if err != nil {
return err
}
// Set content in the viewport
footer := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#808080", Dark: "#808080"}).
Render("ESC to exit scroll mode")
contentWithFooter := lipgloss.JoinVertical(lipgloss.Left, content, footer)
p.viewport.SetContent(contentWithFooter)
// Position the viewport at the bottom initially
p.viewport.GotoBottom()
p.isScrolling = true
return nil
}
// Already in copy mode, just scroll the viewport
p.viewport.LineDown(1)
return nil
}
// ResetToNormalMode exits scroll mode and returns to normal mode
func (p *PreviewPane) ResetToNormalMode(instance *session.Instance) error {
if instance == nil || instance.Status == session.Paused {
return nil
}
if p.isScrolling {
p.isScrolling = false
// Reset viewport
p.viewport.SetContent("")
p.viewport.GotoTop()
// Immediately update content instead of waiting for next UpdateContent call
content, err := instance.Preview()
if err != nil {
return err
}
p.previewState.text = content
}
return nil
}