-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.go
More file actions
422 lines (362 loc) · 8.29 KB
/
selection.go
File metadata and controls
422 lines (362 loc) · 8.29 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package main
import (
"encoding/base64"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/charmbracelet/x/ansi"
)
func base64Encode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
const (
selectionTabWidth = 8
selectionBgANSI = "\x1b[48;5;238m"
)
var ansiResetRe = regexp.MustCompile(`\x1b\[0?m`)
var loadTmuxBuffer = func(text string) error {
loadCmd := exec.Command("tmux", "load-buffer", "-")
loadCmd.Stdin = strings.NewReader(text)
return loadCmd.Run()
}
// Selection tracks drag selection state in the log viewport.
type Selection struct {
Active bool
Start selectionPoint
End selectionPoint
Anchor selectionPoint
View selectionRect
}
type selectionPoint struct {
Line int
Col int
}
func (p selectionPoint) Valid() bool {
return p.Line >= 0 && p.Col >= 0
}
func (p selectionPoint) Before(other selectionPoint) bool {
return p.Line < other.Line || (p.Line == other.Line && p.Col < other.Col)
}
type selectionRect struct {
X int
Y int
W int
H int
}
func (r selectionRect) Contains(x, y int) bool {
return x >= r.X && x < r.X+r.W && y >= r.Y && y < r.Y+r.H
}
func (r selectionRect) Clamp(x, y int) (int, int) {
if r.W > 0 {
if x < r.X {
x = r.X
}
if x >= r.X+r.W {
x = r.X + r.W - 1
}
}
if r.H > 0 {
if y < r.Y {
y = r.Y
}
if y >= r.Y+r.H {
y = r.Y + r.H - 1
}
}
return x, y
}
func NewSelection() *Selection {
s := &Selection{}
s.Clear()
return s
}
func (s *Selection) Clear() {
s.Active = false
s.Start = selectionPoint{-1, -1}
s.End = selectionPoint{-1, -1}
s.Anchor = selectionPoint{-1, -1}
s.View = selectionRect{}
}
func (s *Selection) HasSelection() bool {
return s.Start.Valid() && s.End.Valid()
}
func (s *Selection) PrepareDrag(line, col int, view selectionRect) {
s.Active = false
s.Start = selectionPoint{-1, -1}
s.End = selectionPoint{-1, -1}
s.Anchor = selectionPoint{Line: line, Col: col}
s.View = view
}
func (s *Selection) HandleDrag(line, col int) {
current := selectionPoint{Line: line, Col: col}
if !s.Anchor.Valid() {
s.Anchor = current
}
if !s.Start.Valid() {
s.Start = s.Anchor
s.End = s.Anchor
}
s.Active = true
if current.Before(s.Anchor) {
s.Start = current
s.End = s.Anchor
return
}
s.Start = s.Anchor
s.End = current
}
func (s *Selection) FinishDrag() bool {
if !s.Start.Valid() {
s.Clear()
return false
}
s.Active = false
return true
}
func (s *Selection) IsLineSelected(lineIdx int) bool {
if !s.HasSelection() {
return false
}
start, end := s.normalized()
return lineIdx >= start.Line && lineIdx <= end.Line
}
func (s *Selection) GetLineSelectionCols(lineIdx int) (startCol, endCol int) {
if !s.HasSelection() {
return -1, -1
}
start, end := s.normalized()
if lineIdx < start.Line || lineIdx > end.Line {
return -1, -1
}
if start.Line == end.Line {
return start.Col, end.Col
}
if lineIdx == start.Line {
return start.Col, -1
}
if lineIdx == end.Line {
return 0, end.Col
}
return 0, -1
}
func (s *Selection) SelectedText(lines []string, tabWidth int) string {
if !s.HasSelection() || len(lines) == 0 {
return ""
}
start, end := s.normalized()
if end.Line < 0 || start.Line >= len(lines) {
return ""
}
if start.Line < 0 {
start.Line = 0
}
if end.Line >= len(lines) {
end.Line = len(lines) - 1
}
if start.Line > end.Line {
return ""
}
selected := make([]string, 0, end.Line-start.Line+1)
for lineIdx := start.Line; lineIdx <= end.Line; lineIdx++ {
expanded := selectionDisplayLine(lines[lineIdx], tabWidth)
switch {
case start.Line == end.Line:
selected = append(selected, VisualSubstring(expanded, start.Col, end.Col+1))
case lineIdx == start.Line:
selected = append(selected, VisualSubstring(expanded, start.Col, -1))
case lineIdx == end.Line:
selected = append(selected, VisualSubstring(expanded, 0, end.Col+1))
default:
selected = append(selected, ansi.Strip(expanded))
}
}
return strings.Join(selected, "\n")
}
func selectionDisplayLine(line string, tabWidth int) string {
return ExpandTabs(line, tabWidth)
}
func (s *Selection) normalized() (selectionPoint, selectionPoint) {
start := s.Start
end := s.End
if end.Before(start) {
start, end = end, start
}
return start, end
}
func CopySelectedToClipboard(text string) error {
if text == "" {
return nil
}
// OSC 52: set system clipboard via terminal escape sequence.
// Works over SSH, through tmux, on Ghostty/iTerm2/most modern terminals.
encoded := base64Encode(text)
fmt.Fprintf(os.Stderr, "\033]52;c;%s\a", encoded)
// Also set tmux buffer as an independent copy target.
if os.Getenv("TMUX") != "" {
if err := loadTmuxBuffer(text); err != nil {
return nil
}
}
return nil
}
func ExpandTabs(line string, tabWidth int) string {
if tabWidth <= 0 || !strings.Contains(line, "\t") {
return line
}
var sb strings.Builder
sb.Grow(len(line))
state := ansi.NormalState
column := 0
for len(line) > 0 {
seq, width, n, newState := ansi.GraphemeWidth.DecodeSequenceInString(line, state, nil)
if n <= 0 {
sb.WriteString(line)
break
}
if seq == "\t" && width == 0 {
spaces := tabWidth - (column % tabWidth)
if spaces == 0 {
spaces = tabWidth
}
sb.WriteString(strings.Repeat(" ", spaces))
column += spaces
} else {
sb.WriteString(seq)
column += width
}
state = newState
line = line[n:]
}
return sb.String()
}
// VisualSubstring returns plain text for the visual column range [startCol, endCol).
// If endCol is -1 the range extends to the end of the line.
func VisualSubstring(s string, startCol, endCol int) string {
if s == "" {
return ""
}
var sb strings.Builder
state := ansi.NormalState
cumWidth := 0
for len(s) > 0 {
seq, width, n, newState := ansi.GraphemeWidth.DecodeSequenceInString(s, state, nil)
if n <= 0 {
break
}
if width > 0 {
charStart := cumWidth
charEnd := cumWidth + width
cumWidth = charEnd
inRange := false
if endCol == -1 {
inRange = charEnd > startCol
} else {
inRange = charStart < endCol && charEnd > startCol
}
if inRange {
sb.WriteString(seq)
}
if endCol >= 0 && cumWidth >= endCol {
break
}
}
state = newState
s = s[n:]
}
return ansi.Strip(sb.String())
}
func InjectSelectionBackground(s string) string {
result := selectionBgANSI + s
result = ansiResetRe.ReplaceAllString(result, "${0}"+selectionBgANSI)
return result + "\x1b[49m"
}
func InjectCharacterRangeBackground(line string, startCol, endCol int) string {
if startCol == 0 && endCol == -1 {
return InjectSelectionBackground(line)
}
var sb strings.Builder
sb.Grow(len(line) + 64)
state := ansi.NormalState
cumWidth := 0
inSelection := false
for len(line) > 0 {
seq, width, n, newState := ansi.GraphemeWidth.DecodeSequenceInString(line, state, nil)
if n <= 0 {
sb.WriteString(line)
break
}
if width > 0 {
charInRange := false
if endCol == -1 {
charInRange = cumWidth >= startCol
} else {
charInRange = cumWidth >= startCol && cumWidth <= endCol
}
if charInRange && !inSelection {
sb.WriteString(selectionBgANSI)
inSelection = true
} else if !charInRange && inSelection {
sb.WriteString("\x1b[49m")
inSelection = false
}
sb.WriteString(seq)
cumWidth += width
if endCol >= 0 && cumWidth > endCol && inSelection {
sb.WriteString("\x1b[49m")
inSelection = false
}
} else {
sb.WriteString(seq)
if inSelection && ansiResetRe.MatchString(seq) {
sb.WriteString(selectionBgANSI)
}
}
state = newState
line = line[n:]
}
if inSelection {
sb.WriteString("\x1b[49m")
}
return sb.String()
}
func VisualColAtRelativeX(expandedLine string, relX int) int {
if relX < 0 {
return 0
}
state := ansi.NormalState
cumWidth := 0
lastCharCol := 0
hasChars := false
for len(expandedLine) > 0 {
_, width, n, newState := ansi.GraphemeWidth.DecodeSequenceInString(expandedLine, state, nil)
if n <= 0 {
break
}
if width > 0 {
hasChars = true
if relX >= cumWidth && relX < cumWidth+width {
return cumWidth
}
lastCharCol = cumWidth
cumWidth += width
}
state = newState
expandedLine = expandedLine[n:]
}
if !hasChars {
return 0
}
if relX >= cumWidth {
return lastCharCol
}
return relX
}
// SelectionCopiedMsg is sent when text is copied to the tmux buffer or clipboard.
type SelectionCopiedMsg struct {
Text string
Time time.Time
Err error
}