Skip to content

gh-133722: Add '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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

dougthor42
Copy link
Contributor

@dougthor42 dougthor42 commented May 9, 2025

Add a color arg (defaulting to False) to difflib.unified_diff. When True, ANSI color codes are injected to the diff lines so that the printed result looks like git diff --color:

>>> import difflib
>>> args = [['one', 'three'], ['two', 'three'], 'Original', 'Current']
>>> for line in difflib.unified_diff(*args, lineterm='', color=True):
...     print(line)
...     
--- Original
+++ Current
@@ -1,2 +1,2 @@
-one
+two
 three

image

Fixes #133722.

It's a bummer, I just missed the feature freeze window for 3.14 (it was yesterday! 😭), but c'est la vie!


📚 Documentation preview 📚: https://cpython-previews--133725.org.readthedocs.build/

@dougthor42 dougthor42 marked this pull request as ready for review May 9, 2025 03:40
@tomasr8
Copy link
Member

tomasr8 commented May 9, 2025

Thanks for the PR!

With the basic theming support in #133347 merged, perhaps we could define a separate difflib theme and use that here?

@@ -319,6 +322,10 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.

See :ref:`difflib-interface` for a more detailed example.

.. versionchanged:: 3.15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. versionchanged:: 3.15
.. versionchanged:: next
@hugovk
Copy link
Member

hugovk commented May 9, 2025

Thanks for the PR! I had a play with something similar but didn't get very far before the 3.14 freeze.

With the basic theming support in #133347 merged, perhaps we could define a separate difflib theme and use that here?

Yes, let's add theming support.

See this docstring for instructions.

For a couple of standalone examples of adding theme support to a module, see 230d658 and 5c5c3e1 from that PR.

@dougthor42
Copy link
Contributor Author

Ah, TIL! Thanks, I'll take a look at those docs and update accordingly. It'll be a few days a least before I can get to it, so for now I'll revert this back to draft.

@dougthor42 dougthor42 marked this pull request as draft May 9, 2025 17:14
@hugovk
Copy link
Member

hugovk commented May 9, 2025

Absolutely no rush for this, we have 12 months until the 3.15 feature freeze. Don't hesitate if you have questions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants