Repository navigation
Expand file tree
/
Copy pathpreview.go
More file actions
238 lines (196 loc) · 5.58 KB
/
Copy pathpreview.go
File metadata and controls
238 lines (196 loc) · 5.58 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
package main
import (
"fmt"
"regexp"
"strings"
"charm.land/bubbles/v2/key"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
)
var reverseStyle = lipgloss.NewStyle().Reverse(true).Render
func (m *model) handlePreviewKey(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
if paste, ok := msg.(tea.PasteMsg); ok && m.previewSearchInput.Focused() {
m.previewSearchInput, cmd = m.previewSearchInput.Update(paste)
return m, cmd
}
if msg, ok := msg.(tea.KeyPressMsg); ok {
if m.previewSearchInput.Focused() {
return m.handlePreviewSearchInput(msg)
}
switch {
case key.Matches(msg, keyMap.Quit),
key.Matches(msg, keyMap.Preview),
key.Matches(msg, escKey):
m.showPreview = false
return m, nil
case key.Matches(msg, keyMap.Print):
return m, m.print()
case key.Matches(msg, keyMap.GotoTop):
m.preview.GotoTop()
return m, nil
case key.Matches(msg, keyMap.GotoBottom):
m.preview.GotoBottom()
return m, nil
case key.Matches(msg, keyMap.HalfPageUp):
m.preview.HalfPageUp()
return m, nil
case key.Matches(msg, keyMap.HalfPageDown):
m.preview.HalfPageDown()
return m, nil
case key.Matches(msg, keyMap.ScrollUp):
m.preview.ScrollUp(1)
return m, nil
case key.Matches(msg, keyMap.ScrollDown):
m.preview.ScrollDown(1)
return m, nil
case key.Matches(msg, keyMap.PageUp):
m.preview.PageUp()
return m, nil
case key.Matches(msg, keyMap.PageDown):
m.preview.PageDown()
return m, nil
case key.Matches(msg, keyMap.Search):
m.previewSearchInput.CursorEnd()
m.previewSearchInput.SetWidth(m.termWidth - 2)
m.previewSearchInput.Focus()
return m, nil
case key.Matches(msg, keyMap.SearchNext):
m.selectPreviewSearchResult(m.previewSearchCursor + 1)
return m, nil
case key.Matches(msg, keyMap.SearchPrev):
m.selectPreviewSearchResult(m.previewSearchCursor - 1)
return m, nil
}
}
m.preview, cmd = m.preview.Update(msg)
return m, cmd
}
func (m *model) handlePreviewSearchInput(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch {
case msg.String() == "esc":
m.previewSearchInput.Blur()
m.previewSearchInput.SetValue("")
m.previewSearchResults = nil
m.previewSearchCursor = -1
m.preview.SetContent(m.wrapString(m.previewValue))
return m, nil
case msg.String() == "enter":
m.previewSearchInput.Blur()
found := m.doPreviewSearch(m.previewSearchInput.Value())
if !found {
m.previewSearchResults = nil
m.previewSearchCursor = -1
m.preview.SetContent(m.wrapString(m.previewValue))
}
return m, nil
default:
m.previewSearchInput, cmd = m.previewSearchInput.Update(msg)
}
return m, cmd
}
func (m *model) doPreviewSearch(pattern string) bool {
if pattern == "" {
return false
}
code, ci := regexCase(pattern)
if ci {
code = "(?i)" + code
}
re, err := regexp.Compile(code)
if err != nil {
return false
}
content := m.previewValue
matches := re.FindAllStringIndex(content, -1)
m.previewSearchResults = nil
if len(matches) == 0 {
return false
}
// Precalculate visual line numbers for each match, accounting for line wrapping
lines := strings.Split(content, "\n")
visualLineStarts := make([]int, len(lines))
cumulative := 0
for i, line := range lines {
visualLineStarts[i] = cumulative
wrapped := m.wrapString(line)
cumulative += strings.Count(wrapped, "\n") + 1
}
for _, match := range matches {
// Find original line number
origLineNum := strings.Count(content[:match[0]], "\n")
// Find position within the original line
lastNewline := strings.LastIndex(content[:match[0]], "\n")
var posInLine int
if lastNewline == -1 {
posInLine = match[0]
} else {
posInLine = match[0] - lastNewline - 1
}
// Calculate visual line: start of this original line + offset within wrapped line
visualLineNum := visualLineStarts[origLineNum]
// Add offset for wrapping within the line
if posInLine > 0 && m.termWidth > 0 && posInLine <= len(lines[origLineNum]) {
linePrefix := lines[origLineNum][:posInLine]
wrappedPrefix := m.wrapString(linePrefix)
visualLineNum += strings.Count(wrappedPrefix, "\n")
}
m.previewSearchResults = append(m.previewSearchResults, visualLineNum)
}
// Highlight all matches with Reverse style (once)
var result strings.Builder
lastEnd := 0
for _, match := range matches {
start, end := match[0], match[1]
if start > lastEnd {
result.WriteString(content[lastEnd:start])
}
result.WriteString(reverseStyle(content[start:end]))
lastEnd = end
}
if lastEnd < len(content) {
result.WriteString(content[lastEnd:])
}
m.preview.SetContent(m.wrapString(result.String()))
// Jump to first match
m.previewSearchCursor = 0
m.preview.SetYOffset(m.previewSearchResults[0])
return true
}
func (m *model) selectPreviewSearchResult(i int) {
if len(m.previewSearchResults) == 0 {
return
}
if i < 0 {
i = len(m.previewSearchResults) - 1
}
if i >= len(m.previewSearchResults) {
i = 0
}
m.previewSearchCursor = i
// Scroll to the cached line number
m.preview.SetYOffset(m.previewSearchResults[i])
}
func (m *model) previewSearchStatusBar() string {
if m.previewSearchInput.Focused() {
return m.previewSearchInput.View()
}
pattern := m.previewSearchInput.Value()
if pattern == "" {
return ""
}
re, ci := regexCase(pattern)
re = "/" + re + "/"
if ci {
re += "i"
}
if len(m.previewSearchResults) == 0 {
return flex(m.termWidth, re, "not found")
}
cursor := fmt.Sprintf("found: [%v/%v]", m.previewSearchCursor+1, len(m.previewSearchResults))
return flex(m.termWidth, re, cursor)
}
func (m *model) wrapString(value string) string {
return lipgloss.NewStyle().Width(m.termWidth).Render(value)
}