Format a standard diff line. Returns: a padded and colored version of the diff line with line numbers
(diff_line, padding, bold_delim=None)
| 286 | |
| 287 | |
| 288 | def _format_line(diff_line, padding, bold_delim=None): |
| 289 | """Format a standard diff line. |
| 290 | |
| 291 | Returns: |
| 292 | a padded and colored version of the diff line with line numbers |
| 293 | """ |
| 294 | # Color constants |
| 295 | # We only output colored lines if the coloring is enabled and we are not being |
| 296 | # piped or redirected |
| 297 | if colored.DISABLE_COLOR or not sys.stdout.isatty(): |
| 298 | GREEN = '' |
| 299 | GREEN_BOLD = '' |
| 300 | RED = '' |
| 301 | RED_BOLD = '' |
| 302 | CLEAR = '' |
| 303 | else: |
| 304 | GREEN = '\033[32m' |
| 305 | GREEN_BOLD = '\033[1;32m' |
| 306 | RED = '\033[31m' |
| 307 | RED_BOLD = '\033[1;31m' |
| 308 | CLEAR = '\033[0m' |
| 309 | |
| 310 | formatted = '' |
| 311 | st = diff_line.origin |
| 312 | line = st + diff_line.content.rstrip('\n') |
| 313 | old_lineno = diff_line.old_lineno |
| 314 | new_lineno = diff_line.new_lineno |
| 315 | |
| 316 | if st == ' ': |
| 317 | formatted = ( |
| 318 | str(old_lineno).ljust(padding) + str(new_lineno).ljust(padding) + line) |
| 319 | elif st == '+': |
| 320 | formatted = ' ' * padding + GREEN + str(new_lineno).ljust(padding) |
| 321 | if not bold_delim: |
| 322 | formatted += line |
| 323 | else: |
| 324 | bold_start, bold_end = bold_delim |
| 325 | formatted += ( |
| 326 | line[:bold_start] + GREEN_BOLD + line[bold_start:bold_end] + CLEAR + |
| 327 | GREEN + line[bold_end:]) |
| 328 | elif st == '-': |
| 329 | formatted = RED + str(old_lineno).ljust(padding) + ' ' * padding |
| 330 | if not bold_delim: |
| 331 | formatted += line |
| 332 | else: |
| 333 | bold_start, bold_end = bold_delim |
| 334 | formatted += ( |
| 335 | line[:bold_start] + RED_BOLD + line[bold_start:bold_end] + CLEAR + |
| 336 | RED + line[bold_end:]) |
| 337 | |
| 338 | return formatted + CLEAR |
| 339 | |
| 340 | |
| 341 | def _highlight(line1, line2): |