MCPcopy Create free account
hub / github.com/cli/cli / formatMatch

Function formatMatch

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

Source from the content-addressed store, hash-verified

190}
191
192func formatMatch(t string, matches []search.Match, io *iostreams.IOStreams) []string {
193 cs := io.ColorScheme()
194
195 startIndices := map[int]struct{}{}
196 endIndices := map[int]struct{}{}
197 for _, m := range matches {
198 if len(m.Indices) < 2 {
199 continue
200 }
201 startIndices[m.Indices[0]] = struct{}{}
202 endIndices[m.Indices[1]] = struct{}{}
203 }
204
205 var lines []string
206 var b strings.Builder
207 var found, highlighting bool
208 // A match can end at a newline or at the end of the fragment, where no
209 // character carries the end index, so close any open highlight here.
210 endLine := func() {
211 if highlighting {
212 b.WriteString(cs.Reset())
213 highlighting = false
214 }
215 if found {
216 lines = append(lines, b.String())
217 }
218 found = false
219 b.Reset()
220 }
221 for i, c := range t {
222 if c == '\n' {
223 endLine()
224 continue
225 }
226 if _, ok := startIndices[i]; ok {
227 b.WriteString(cs.HighlightStart())
228 found = true
229 highlighting = true
230 } else if _, ok := endIndices[i]; ok {
231 b.WriteString(cs.Reset())
232 highlighting = false
233 }
234 b.WriteRune(c)
235 }
236 endLine()
237 return lines
238}

Callers 2

displayResultsFunction · 0.85
TestFormatMatchFunction · 0.85

Calls 4

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

Tested by 1

TestFormatMatchFunction · 0.68