-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_slice.go
More file actions
158 lines (138 loc) · 3.33 KB
/
Copy pathtext_slice.go
File metadata and controls
158 lines (138 loc) · 3.33 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
package fasttui
import (
"strings"
"github.com/clipperhouse/uax29/v2/graphemes"
)
func SliceByColumn(line string, startCol int, length int, strict bool) string {
result := SliceWithWidth(line, startCol, length, strict)
return result.text
}
func SliceWithWidth(line string, startCol int, length int, strict bool) SliceResult {
if length <= 0 {
return SliceResult{text: "", width: 0}
}
endCol := startCol + length
var result strings.Builder
resultWidth := 0
currentCol := 0
i := 0
pendingAnsi := ""
for i < len(line) {
code, codeLen, ok := ExtractAnsiCode(line, i)
if ok {
if currentCol >= startCol && currentCol < endCol {
result.WriteString(code)
} else if currentCol < startCol {
pendingAnsi += code
}
i += codeLen
continue
}
textEnd := i
for textEnd < len(line) {
if _, _, ok := ExtractAnsiCode(line, textEnd); ok {
break
}
textEnd++
}
textPortion := line[i:textEnd]
g := graphemes.FromString(textPortion)
for g.Next() {
grapheme := g.Value()
w := GraphemeWidth(grapheme)
inRange := currentCol >= startCol && currentCol < endCol
fits := !strict || currentCol+w <= endCol
if inRange && fits {
if pendingAnsi != "" {
result.WriteString(pendingAnsi)
pendingAnsi = ""
}
result.WriteString(grapheme)
resultWidth += w
}
currentCol += w
if currentCol >= endCol {
break
}
}
i = textEnd
if currentCol >= endCol {
break
}
}
return SliceResult{text: result.String(), width: resultWidth}
}
func ExtractSegments(line string, beforeEnd int, afterStart int, afterLen int, strictAfter bool) (string, int, string, int) {
var before, after strings.Builder
beforeWidth := 0
afterWidth := 0
currentCol := 0
i := 0
pendingAnsiBefore := ""
afterStarted := false
afterEnd := afterStart + afterLen
tracker := NewAnsiCodeTracker()
for i < len(line) {
code, codeLen, ok := ExtractAnsiCode(line, i)
if ok {
tracker.Process(code)
if currentCol < beforeEnd {
pendingAnsiBefore += code
} else if currentCol >= afterStart && currentCol < afterEnd && afterStarted {
after.WriteString(code)
}
i += codeLen
continue
}
textEnd := i
for textEnd < len(line) {
if _, _, ok := ExtractAnsiCode(line, textEnd); ok {
break
}
textEnd++
}
textPortion := line[i:textEnd]
g := graphemes.FromString(textPortion)
for g.Next() {
grapheme := g.Value()
w := GraphemeWidth(grapheme)
if currentCol < beforeEnd {
if pendingAnsiBefore != "" {
before.WriteString(pendingAnsiBefore)
pendingAnsiBefore = ""
}
before.WriteString(grapheme)
beforeWidth += w
} else if currentCol >= afterStart && currentCol < afterEnd {
fits := !strictAfter || currentCol+w <= afterEnd
if fits {
if !afterStarted {
after.WriteString(tracker.GetActiveCodes())
afterStarted = true
}
after.WriteString(grapheme)
afterWidth += w
}
}
currentCol += w
// Determine break point and check once
breakPoint := beforeEnd
if afterLen > 0 {
breakPoint = afterEnd
}
if currentCol >= breakPoint {
break
}
}
i = textEnd
// Same consolidated break condition for outer loop
breakPoint := beforeEnd
if afterLen > 0 {
breakPoint = afterEnd
}
if currentCol >= breakPoint {
break
}
}
return before.String(), beforeWidth, after.String(), afterWidth
}