Skip to content

Commit

Permalink
Finally, let's use the new rich diff in black-shades :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-007 committed Jan 4, 2022
1 parent 5b3b072 commit 39d00af
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 52 deletions.
9 changes: 1 addition & 8 deletions src/diff_shades/_diff.py
Expand Up @@ -105,7 +105,6 @@ def marker_style(self) -> dict[str, Union[dict[str, str], str]]:
}[self.theme]

def raw_unified_diff(self) -> Iterator[str]:
differ = difflib.Differ()
lines_lhs = self.lhs.splitlines()
lines_rhs = self.rhs.splitlines()
return unified_diff(
Expand Down Expand Up @@ -171,13 +170,7 @@ def build_unified_diff(self) -> RenderResult:

for line in diff:
if line.startswith("---"):
output_lines.append(
Panel(
line,
box=box.DOUBLE,
style="bold",
)
)
output_lines.append(Text(line, style="bold"))
elif line.startswith("@@"):
output_lines.append(
Panel(
Expand Down
6 changes: 3 additions & 3 deletions src/diff_shades/cli.py
Expand Up @@ -27,6 +27,7 @@

import diff_shades
import diff_shades.results
from diff_shades._diff import Diff
from diff_shades.analysis import (
GIT_BIN,
RESULT_COLORS,
Expand All @@ -36,7 +37,6 @@
)
from diff_shades.config import PROJECTS, Project
from diff_shades.output import (
color_diff,
make_analysis_summary,
make_comparison_summary,
make_project_details_table,
Expand Down Expand Up @@ -101,7 +101,7 @@ def compare_project_pair(
found_difference = True

diff = diff_two_results(r1, r2, file=f"{project.name}:{file}", diff_failure=True)
console.print(color_diff(diff), highlight=False)
console.print(diff)

return found_difference

Expand Down Expand Up @@ -355,7 +355,7 @@ def show(
console.print(escape(result.log), highlight=False)
elif result.type == "reformatted":
diff = result.diff(file_key)
console.print(color_diff(diff), highlight=False)
console.print(diff)

elif project_key and not file_key:
# TODO: implement a list view
Expand Down
17 changes: 0 additions & 17 deletions src/diff_shades/output.py
Expand Up @@ -8,7 +8,6 @@
from typing import Sequence, Tuple, cast

import rich
from rich.markup import escape
from rich.panel import Panel
from rich.progress import BarColumn, Progress, TimeElapsedColumn
from rich.table import Table
Expand All @@ -26,22 +25,6 @@
console = rich.get_console()


def color_diff(contents: str) -> str:
"""Inject rich markup into the diff."""
lines = escape(contents).split("\n")
for i, line in enumerate(lines):
if line.startswith("+++") or line.startswith("---"):
line = "[bold]" + line + "[/]"
elif line.startswith("@@"):
line = "[cyan]" + line + "[/]"
elif line.startswith("+"):
line = "[green]" + line + "[/]"
elif line.startswith("-"):
line = "[red]" + line + "[/]"
lines[i] = line
return "\n".join(lines)


def make_rich_progress() -> Progress:
return Progress(
"[progress.description]{task.description}",
Expand Down
33 changes: 9 additions & 24 deletions src/diff_shades/results.py
Expand Up @@ -2,7 +2,6 @@
# > Analysis data representation & processing
# ==========================================

import difflib
import hashlib
import json
import pickle
Expand Down Expand Up @@ -33,6 +32,7 @@
import platformdirs

import diff_shades
from diff_shades._diff import Diff
from diff_shades.config import Project

CACHE_DIR: Final = Path(platformdirs.user_cache_dir("diff-shades"))
Expand All @@ -42,30 +42,15 @@
ResultTypes = Literal["nothing-changed", "reformatted", "failed"]


def unified_diff(a: str, b: str, a_name: str, b_name: str) -> str:
"""Return a unified diff string between strings `a` and `b`."""
a_lines = a.splitlines(keepends=True)
b_lines = b.splitlines(keepends=True)
diff_lines = []
for line in difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5):
# Work around https://bugs.python.org/issue2142. See also:
# https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
if line[-1] == "\n":
diff_lines.append(line)
else:
diff_lines.append(line + "\n")
diff_lines.append("\\ No newline at end of file\n")
return "".join(diff_lines)


def calculate_line_changes(diff: str) -> Tuple[int, int]:
def calculate_line_changes(diff: Diff) -> Tuple[int, int]:
"""Return a two-tuple (additions, deletions) of a diff."""
additions = 0
deletions = 0
for line in diff.splitlines():
if line[0] == "+" and not line.startswith("+++"):
raw_diff = "\n".join(diff.raw_unified_diff())
for line in raw_diff.splitlines():
if line.startswith("+ "):
additions += 1
elif line[0] == "-" and not line.startswith("---"):
elif line.startswith("- "):
deletions += 1

return additions, deletions
Expand Down Expand Up @@ -108,8 +93,8 @@ def __post_init__(self) -> None:
object.__setattr__(self, "line_changes", changes)

@lru_cache(maxsize=None)
def diff(self, filepath: str) -> str:
return unified_diff(self.src, self.dst, f"a/{filepath}", f"b/{filepath}")
def diff(self, filepath: str) -> Diff:
return Diff(self.src, self.dst, f"a/{filepath}", f"b/{filepath}")


@dataclass(frozen=True)
Expand Down Expand Up @@ -193,7 +178,7 @@ def diff_two_results(
first_dst = r1.dst if r1.type == "reformatted" else r1.src
second_dst = r2.dst if r2.type == "reformatted" else r2.src

return unified_diff(first_dst, second_dst, f"a/{file}", f"b/{file}")
return Diff(first_dst, second_dst, f"a/{file}", f"b/{file}")


# fmt: off
Expand Down

0 comments on commit 39d00af

Please sign in to comment.