|
1 | 1 | import difflib
|
2 | 2 | from typing import List, Iterator, Union
|
3 | 3 |
|
| 4 | +REMOVED_STYLE = "background-color: #fadad7; color: #b30000;" |
| 5 | +ADDED_STYLE = "background-color: #eaf2c2; color: #406619;" |
| 6 | + |
4 | 7 | def same_slicer(lst: List[str], start: int, end: int) -> List[str]:
|
5 | 8 | """Return a slice of the list, or a single element if start == end."""
|
6 | 9 | return lst[start:end] if start != end else [lst[start]]
|
@@ -33,24 +36,26 @@ def customSequenceMatcher(
|
33 | 36 | """
|
34 | 37 | cruncher = difflib.SequenceMatcher(isjunk=lambda x: x in " \t", a=before, b=after)
|
35 | 38 |
|
| 39 | + |
| 40 | + |
36 | 41 | for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
|
37 | 42 | if include_equal and tag == 'equal':
|
38 | 43 | yield before[alo:ahi]
|
39 | 44 | elif include_removed and tag == 'delete':
|
40 | 45 | if html_colour:
|
41 |
| - yield [f'<span style="background-color: #ffcecb;">{line}</span>' for line in same_slicer(before, alo, ahi)] |
| 46 | + yield [f'<span style="{REMOVED_STYLE}">{line}</span>' for line in same_slicer(before, alo, ahi)] |
42 | 47 | else:
|
43 | 48 | yield [f"(removed) {line}" for line in same_slicer(before, alo, ahi)] if include_change_type_prefix else same_slicer(before, alo, ahi)
|
44 | 49 | elif include_replaced and tag == 'replace':
|
45 | 50 | if html_colour:
|
46 |
| - yield [f'<span style="background-color: #ffcecb;">{line}</span>' for line in same_slicer(before, alo, ahi)] + \ |
47 |
| - [f'<span style="background-color: #dafbe1;">{line}</span>' for line in same_slicer(after, blo, bhi)] |
| 51 | + yield [f'<span style="{REMOVED_STYLE}">{line}</span>' for line in same_slicer(before, alo, ahi)] + \ |
| 52 | + [f'<span style="{ADDED_STYLE}">{line}</span>' for line in same_slicer(after, blo, bhi)] |
48 | 53 | else:
|
49 | 54 | yield [f"(changed) {line}" for line in same_slicer(before, alo, ahi)] + \
|
50 | 55 | [f"(into) {line}" for line in same_slicer(after, blo, bhi)] if include_change_type_prefix else same_slicer(before, alo, ahi) + same_slicer(after, blo, bhi)
|
51 | 56 | elif include_added and tag == 'insert':
|
52 | 57 | if html_colour:
|
53 |
| - yield [f'<span style="background-color: #dafbe1;">{line}</span>' for line in same_slicer(after, blo, bhi)] |
| 58 | + yield [f'<span style="{ADDED_STYLE}">{line}</span>' for line in same_slicer(after, blo, bhi)] |
54 | 59 | else:
|
55 | 60 | yield [f"(added) {line}" for line in same_slicer(after, blo, bhi)] if include_change_type_prefix else same_slicer(after, blo, bhi)
|
56 | 61 |
|
|
0 commit comments