MCPcopy Index your code
hub / github.com/cli/cli / colorDiffLines

Function colorDiffLines

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

Source from the content-addressed store, hash-verified

225)
226
227func colorDiffLines(w io.Writer, r io.Reader) error {
228 diffLines := bufio.NewReaderSize(r, lineBufferSize)
229 wasPrefix := false
230 needsReset := false
231
232 for {
233 diffLine, isPrefix, err := diffLines.ReadLine()
234 if err != nil {
235 if errors.Is(err, io.EOF) {
236 break
237 }
238 return fmt.Errorf("error reading pull request diff: %w", err)
239 }
240
241 var color []byte
242 if !wasPrefix {
243 if isHeaderLine(diffLine) {
244 color = colorHeader
245 } else if isAdditionLine(diffLine) {
246 color = colorAddition
247 } else if isRemovalLine(diffLine) {
248 color = colorRemoval
249 }
250 }
251
252 if color != nil {
253 if _, err := w.Write(color); err != nil {
254 return err
255 }
256 needsReset = true
257 }
258
259 if _, err := w.Write(diffLine); err != nil {
260 return err
261 }
262
263 if !isPrefix {
264 if needsReset {
265 if _, err := w.Write(colorReset); err != nil {
266 return err
267 }
268 needsReset = false
269 }
270 if _, err := w.Write([]byte{'\n'}); err != nil {
271 return err
272 }
273 }
274 wasPrefix = isPrefix
275 }
276 return nil
277}
278
279var diffHeaderPrefixes = []string{"+++", "---", "diff", "index"}
280

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