MCPcopy
hub / github.com/cli/cli / formatMatch

Function formatMatch

pkg/cmd/search/code/code.go:189–226  ·  view source on GitHub ↗
(t string, matches []search.Match, io *iostreams.IOStreams)

Source from the content-addressed store, hash-verified

187}
188
189func formatMatch(t string, matches []search.Match, io *iostreams.IOStreams) []string {
190 cs := io.ColorScheme()
191
192 startIndices := map[int]struct{}{}
193 endIndices := map[int]struct{}{}
194 for _, m := range matches {
195 if len(m.Indices) < 2 {
196 continue
197 }
198 startIndices[m.Indices[0]] = struct{}{}
199 endIndices[m.Indices[1]] = struct{}{}
200 }
201
202 var lines []string
203 var b strings.Builder
204 var found bool
205 for i, c := range t {
206 if c == '\n' {
207 if found {
208 lines = append(lines, b.String())
209 }
210 found = false
211 b.Reset()
212 continue
213 }
214 if _, ok := startIndices[i]; ok {
215 b.WriteString(cs.HighlightStart())
216 found = true
217 } else if _, ok := endIndices[i]; ok {
218 b.WriteString(cs.Reset())
219 }
220 b.WriteRune(c)
221 }
222 if found {
223 lines = append(lines, b.String())
224 }
225 return lines
226}

Callers 1

displayResultsFunction · 0.85

Calls 4

ColorSchemeMethod · 0.80
HighlightStartMethod · 0.80
StringMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected