Skip to content

gh-133722: Add Difflib theme to _colorize and 'color' option to difflib.unified_diff #133725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5c5b248
Add test case
dougthor42 May 9, 2025
fdc0fa0
Add 'color' arg to difflib.unified_diff.
dougthor42 May 9, 2025
fcdd7ab
Update docs and ACKs
dougthor42 May 9, 2025
0e9b070
blurb
dougthor42 May 9, 2025
7c31749
fixup to follow convention
dougthor42 May 9, 2025
66475a2
Add 'Difflib' theme
dougthor42 May 11, 2025
dbf0547
fixup tests
dougthor42 May 11, 2025
a72012e
Switch to using themes. So easy!
dougthor42 May 11, 2025
2a3d818
use 'next' in versionchanged docs
dougthor42 May 11, 2025
252982e
turns out 'git diff' adds reset to the start and end of context lines
dougthor42 May 11, 2025
3422fa7
Use GNU unified diff terms
dougthor42 May 14, 2025
bffdd71
move class
dougthor42 May 14, 2025
3255866
kw-only the 'color' arg
dougthor42 May 14, 2025
c48a6ac
Doc formatting updates
dougthor42 May 14, 2025
8ca50fa
Sort the things that are safe to sort without kw_only=True
dougthor42 May 14, 2025
eb0e81e
Update what's new
dougthor42 May 14, 2025
f7b34c3
Merge remote-tracking branch 'upstream/main' into difflib-color-gh133722
dougthor42 May 14, 2025
fb092b0
fixup docs
dougthor42 May 14, 2025
734b0bc
fixup docs
dougthor42 May 20, 2025
8a10e40
Code review: docs, whatsnew, f-strings, news
dougthor42 May 20, 2025
57b80d1
force_colorized
dougthor42 May 20, 2025
c235425
kw_only
dougthor42 May 29, 2025
387cfe6
Merge branch 'main' into difflib-color-gh133722
dougthor42 May 29, 2025
833d86a
documentation updates per code review
dougthor42 Jun 11, 2025
25f9d80
Merge branch 'main' into difflib-color-gh133722
dougthor42 Jul 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Switch to using themes. So easy!
  • Loading branch information
dougthor42 committed May 11, 2025
commit a72012e17f7da6de0156a9f684963b2623c99edf
28 changes: 10 additions & 18 deletions Lib/difflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
'unified_diff', 'diff_bytes', 'HtmlDiff', 'Match']

from _colorize import can_colorize, get_theme
from heapq import nlargest as _nlargest
from collections import namedtuple as _namedtuple
from types import GenericAlias
Expand Down Expand Up @@ -1137,14 +1138,10 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
four
"""

# {tag: ANSI color escape code}
colors = {
"delete": "\033[31m", # red
"insert": "\033[32m", # green
"header": "\033[1m", # bold / increased intensity
"hunk": "\033[36m", # cyan
}
reset = "\033[m"
if color and can_colorize():
t = get_theme(force_color=True).difflib
else:
t = get_theme(force_no_color=True).difflib

_check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
started = False
Expand All @@ -1153,16 +1150,13 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
started = True
fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
todate = '\t{}'.format(tofiledate) if tofiledate else ''
_line = '--- {}{}{}'.format(fromfile, fromdate, lineterm)
yield colors["header"] + _line + reset if color else _line
_line = '+++ {}{}{}'.format(tofile, todate, lineterm)
yield colors["header"] + _line + reset if color else _line
yield '{}--- {}{}{}{}'.format(t.header, fromfile, fromdate, lineterm, t.reset)
yield '{}+++ {}{}{}{}'.format(t.header, tofile, todate, lineterm, t.reset)

first, last = group[0], group[-1]
file1_range = _format_range_unified(first[1], last[2])
file2_range = _format_range_unified(first[3], last[4])
_line = '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
yield colors["hunk"] + _line + reset if color else _line
yield '{}@@ -{} +{} @@{}{}'.format(t.hunk, file1_range, file2_range, lineterm, t.reset)

for tag, i1, i2, j1, j2 in group:
if tag == 'equal':
Expand All @@ -1171,12 +1165,10 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
continue
if tag in {'replace', 'delete'}:
for line in a[i1:i2]:
_line = '-' + line
yield colors["delete"] + _line + reset if color else _line
yield f'{t.delete}-{line}{t.reset}'
if tag in {'replace', 'insert'}:
for line in b[j1:j2]:
_line = '+' + line
yield colors["insert"] + _line + reset if color else _line
yield f'{t.insert}+{line}{t.reset}'


########################################################################
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy