GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add a `color: bool` arg to `difflib.unified_diff` · Issue #133722 · python/cpython

Add a color: bool arg to difflib.unified_diff #133722

New issue
Copy link
New issue
Copy link
Closed
#133725
Closed
Add a color: bool arg to difflib.unified_diff#133722
#133725
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@dougthor42
dougthor42
opened on May 9, 2025
Issue body actions

Feature or enhancement

Proposal:

What are people's thoughts on adding a color: bool arg to difflib.unified_diff so that it injects ANSI terminal codes to color things in the way that git diff would?

New API:

difflib.unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
                     tofiledate='', n=3, lineterm='\n', color=False)

Open questions:

  1. Should the colors be configurable? Eg via dict colors={"equal": "\033[31;1;4m", "delete": "...", "insert": "...", "header": "...", "hunk": "..."}
    • The default would be the basic 4bit colors used by git diff.:
      • header: bold \033[1m
      • hunk lines: cyan \033[36m
      • removed: red \033[31m
      • added: green \033[32m
      • equal: no formatting
    • This might be useful for nonstandard terminal colors?
  2. Should this be added to other functions like ndiff and contextdiff?

I'd be happy to tackle this. Excluding tests, it looks to be O(20 LOC) changes.


Image

$ cat python mydiff.py 
cat: python: No such file or directory
import difflib
import pathlib

file1 = pathlib.Path("a.txt")
file2 = pathlib.Path("b.txt")
udiff = difflib.unified_diff(
    file1.read_text().splitlines(),
    file2.read_text().splitlines(),
    fromfile=str(file1),
    tofile=str(file2),
)
for line in udiff:
    print(line)
$ python mydiff.py 
--- a.txt

+++ b.txt

@@ -1,3 +1,3 @@

 1a
-2b
 3c
+4d

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

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

Activity

  1. dougthor42
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on May 9, 2025
  2. dougthor42
    added a commit that references this issue on May 9, 2025

    Add 'color' arg to difflib.unified_diff.

    This issue will close once commit fdc0fa0 is merged into the 'main' branch.
    fdc0fa0
  3. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 9, 2025
  4. hugovk
    closed this as completedin #133725on Aug 8, 2025
  5. hugovk
    added a commit that references this issue on Aug 8, 2025

    gh-133722: Add Difflib theme to `_colorize` and 'color' option to `di…

    34d7351
  6. Agent-Hellboy
    added a commit that references this issue on Aug 19, 2025

    pythongh-133722: Add Difflib theme to `_colorize` and 'color' option …

    2d1c812
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.