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

Function colorDiffLines

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

Source from the content-addressed store, hash-verified

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

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