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

Function colorDiffLines

pkg/cmd/pr/diff/diff.go:247–297  ·  view source on GitHub ↗
(w io.Writer, r io.Reader)

Source from the content-addressed store, hash-verified

245)
246
247func colorDiffLines(w io.Writer, r io.Reader) error {
248 diffLines := bufio.NewReaderSize(r, lineBufferSize)
249 wasPrefix := false
250 needsReset := false
251
252 for {
253 diffLine, isPrefix, err := diffLines.ReadLine()
254 if err != nil {
255 if errors.Is(err, io.EOF) {
256 break
257 }
258 return fmt.Errorf("error reading pull request diff: %w", err)
259 }
260
261 var color []byte
262 if !wasPrefix {
263 if isHeaderLine(diffLine) {
264 color = colorHeader
265 } else if isAdditionLine(diffLine) {
266 color = colorAddition
267 } else if isRemovalLine(diffLine) {
268 color = colorRemoval
269 }
270 }
271
272 if color != nil {
273 if _, err := w.Write(color); err != nil {
274 return err
275 }
276 needsReset = true
277 }
278
279 if _, err := w.Write(diffLine); err != nil {
280 return err
281 }
282
283 if !isPrefix {
284 if needsReset {
285 if _, err := w.Write(colorReset); err != nil {
286 return err
287 }
288 needsReset = false
289 }
290 if _, err := w.Write([]byte{'\n'}); err != nil {
291 return err
292 }
293 }
294 wasPrefix = isPrefix
295 }
296 return nil
297}
298
299var diffHeaderPrefixes = []string{"+++", "---", "diff", "index"}
300

Callers 2

diffRunFunction · 0.85
Test_colorDiffLinesFunction · 0.85

Calls 5

isHeaderLineFunction · 0.85
isAdditionLineFunction · 0.85
isRemovalLineFunction · 0.85
ErrorfMethod · 0.65
WriteMethod · 0.65

Tested by 1

Test_colorDiffLinesFunction · 0.68