-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsemantic_prompt.go
More file actions
275 lines (235 loc) · 7.54 KB
/
Copy pathsemantic_prompt.go
File metadata and controls
275 lines (235 loc) · 7.54 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
272
273
274
275
package headlessterm
import (
"github.com/danielgatis/go-ansicode"
)
// PromptMark stores information about a semantic prompt mark (OSC 133).
// Used for prompt-based navigation in scrollback.
type PromptMark struct {
// Type is the mark type (PromptStart, CommandStart, CommandExecuted, CommandFinished).
Type ansicode.ShellIntegrationMark
// Row is the absolute row position (including scrollback offset).
// Negative values indicate scrollback lines (-1 is most recent scrollback line).
Row int
// ExitCode is the command exit code (only valid for CommandFinished marks, -1 otherwise).
ExitCode int
}
// SemanticPromptHandler handles semantic prompt events (OSC 133).
type SemanticPromptHandler interface {
// OnMark is called when a semantic prompt mark is received.
OnMark(mark ansicode.ShellIntegrationMark, exitCode int)
}
// NoopSemanticPromptHandler ignores all semantic prompt events.
type NoopSemanticPromptHandler struct{}
func (NoopSemanticPromptHandler) OnMark(mark ansicode.ShellIntegrationMark, exitCode int) {}
// Ensure NoopSemanticPromptHandler satisfies the interface
var _ SemanticPromptHandler = (*NoopSemanticPromptHandler)(nil)
// ShellIntegrationMark processes a semantic prompt mark (OSC 133).
// Records the mark position for prompt-based navigation.
// This method name is required by the ansicode.Handler interface.
func (t *Terminal) ShellIntegrationMark(mark ansicode.ShellIntegrationMark, exitCode int) {
if t.middleware != nil && t.middleware.SemanticPromptMark != nil {
t.middleware.SemanticPromptMark(mark, exitCode, t.semanticPromptMarkInternal)
return
}
t.semanticPromptMarkInternal(mark, exitCode)
}
func (t *Terminal) semanticPromptMarkInternal(mark ansicode.ShellIntegrationMark, exitCode int) {
t.mu.Lock()
defer t.mu.Unlock()
// Calculate absolute row (accounting for scrollback)
scrollbackLen := t.primaryBuffer.ScrollbackLen()
absoluteRow := t.cursor.Row + scrollbackLen
// Store the mark
t.promptMarks = append(t.promptMarks, PromptMark{
Type: mark,
Row: absoluteRow,
ExitCode: exitCode,
})
// Notify handler if set
if t.semanticPromptHandler != nil {
t.semanticPromptHandler.OnMark(mark, exitCode)
}
}
// PromptMarks returns all recorded prompt marks.
func (t *Terminal) PromptMarks() []PromptMark {
t.mu.RLock()
defer t.mu.RUnlock()
// Return a copy to prevent external modification
marks := make([]PromptMark, len(t.promptMarks))
copy(marks, t.promptMarks)
return marks
}
// PromptMarkCount returns the number of recorded prompt marks.
func (t *Terminal) PromptMarkCount() int {
t.mu.RLock()
defer t.mu.RUnlock()
return len(t.promptMarks)
}
// ClearPromptMarks removes all recorded prompt marks.
func (t *Terminal) ClearPromptMarks() {
t.mu.Lock()
defer t.mu.Unlock()
t.promptMarks = nil
}
// NextPromptRow returns the absolute row of the next prompt mark after the given absolute row.
// Returns -1 if no next prompt exists.
// If markType is specified (not -1), only returns marks of that type.
func (t *Terminal) NextPromptRow(currentAbsRow int, markType ansicode.ShellIntegrationMark) int {
t.mu.RLock()
defer t.mu.RUnlock()
for _, mark := range t.promptMarks {
if mark.Row > currentAbsRow {
if markType == -1 || mark.Type == markType {
return mark.Row
}
}
}
return -1
}
// PrevPromptRow returns the absolute row of the previous prompt mark before the given absolute row.
// Returns -1 if no previous prompt exists.
// If markType is specified (not -1), only returns marks of that type.
func (t *Terminal) PrevPromptRow(currentAbsRow int, markType ansicode.ShellIntegrationMark) int {
t.mu.RLock()
defer t.mu.RUnlock()
// Search backwards
for i := len(t.promptMarks) - 1; i >= 0; i-- {
mark := t.promptMarks[i]
if mark.Row < currentAbsRow {
if markType == -1 || mark.Type == markType {
return mark.Row
}
}
}
return -1
}
// GetPromptMarkAt returns the prompt mark at the given absolute row, or nil if none exists.
func (t *Terminal) GetPromptMarkAt(absRow int) *PromptMark {
t.mu.RLock()
defer t.mu.RUnlock()
for i := range t.promptMarks {
if t.promptMarks[i].Row == absRow {
mark := t.promptMarks[i]
return &mark
}
}
return nil
}
// SetSemanticPromptHandler sets the semantic prompt handler at runtime.
func (t *Terminal) SetSemanticPromptHandler(h SemanticPromptHandler) {
t.mu.Lock()
defer t.mu.Unlock()
t.semanticPromptHandler = h
}
// SemanticPromptHandlerValue returns the current semantic prompt handler.
func (t *Terminal) SemanticPromptHandlerValue() SemanticPromptHandler {
t.mu.RLock()
defer t.mu.RUnlock()
return t.semanticPromptHandler
}
// GetLastCommandOutput returns the output of the last executed command.
// It finds the text between the last CommandExecuted (C) mark and the last CommandFinished (D) mark.
// Returns empty string if no complete command output is available.
func (t *Terminal) GetLastCommandOutput() string {
t.mu.RLock()
defer t.mu.RUnlock()
if len(t.promptMarks) == 0 {
return ""
}
// Find the last CommandExecuted and CommandFinished marks
var lastExecuted, lastFinished *PromptMark
for i := len(t.promptMarks) - 1; i >= 0; i-- {
mark := &t.promptMarks[i]
if lastFinished == nil && mark.Type == ansicode.CommandFinished {
lastFinished = mark
}
if lastExecuted == nil && mark.Type == ansicode.CommandExecuted {
lastExecuted = mark
}
// Once we have both, check if they form a valid pair
if lastExecuted != nil && lastFinished != nil {
// CommandExecuted must come before CommandFinished
if lastExecuted.Row < lastFinished.Row {
break
}
// Invalid pair, continue searching
lastFinished = nil
lastExecuted = nil
}
}
if lastExecuted == nil || lastFinished == nil {
return ""
}
// Extract text between the two marks
return t.extractTextBetweenRows(lastExecuted.Row, lastFinished.Row)
}
// extractTextBetweenRows extracts text from startRow (inclusive) to endRow (exclusive).
// Rows are absolute (including scrollback offset).
func (t *Terminal) extractTextBetweenRows(startRow, endRow int) string {
scrollbackLen := t.primaryBuffer.ScrollbackLen()
var lines []string
// Start from the CommandExecuted row (inclusive) to CommandFinished row (exclusive)
for absRow := startRow; absRow < endRow; absRow++ {
var lineContent string
if absRow < scrollbackLen {
// Row is in scrollback
scrollbackLine := t.primaryBuffer.ScrollbackLine(absRow)
if scrollbackLine != nil {
lineContent = t.cellsToString(scrollbackLine)
}
} else {
// Row is in visible buffer
bufferRow := absRow - scrollbackLen
if bufferRow >= 0 && bufferRow < t.rows {
lineContent = t.activeBuffer.LineContent(bufferRow)
}
}
lines = append(lines, lineContent)
}
// Join lines, trimming trailing empty lines
result := ""
lastNonEmpty := -1
for i, line := range lines {
if line != "" {
lastNonEmpty = i
}
}
if lastNonEmpty < 0 {
return ""
}
for i := 0; i <= lastNonEmpty; i++ {
if i > 0 {
result += "\n"
}
result += lines[i]
}
return result
}
// cellsToString converts a slice of cells to a string.
func (t *Terminal) cellsToString(cells []Cell) string {
// Find the last non-space character
lastNonSpace := -1
for i := len(cells) - 1; i >= 0; i-- {
cell := &cells[i]
if cell.Char != ' ' && cell.Char != 0 && !cell.IsWideSpacer() {
lastNonSpace = i
break
}
}
if lastNonSpace < 0 {
return ""
}
runes := make([]rune, 0, lastNonSpace+1)
for i := 0; i <= lastNonSpace; i++ {
cell := &cells[i]
if cell.IsWideSpacer() {
continue
}
if cell.Char == 0 {
runes = append(runes, ' ')
} else {
runes = append(runes, cell.Char)
}
}
return string(runes)
}