aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Shirayuki Nekomata <[email protected]>2019-11-05 10:27:27 +0700
committerGravatar Shirayuki Nekomata <[email protected]>2019-11-05 10:27:27 +0700
commite90b47e37587fedde765cf78bf27eca15202314d (patch)
tree8a1a7110e1f2a5240767deffcb722c0342921781
parentMerge before & after response, show only differences (diff)
un-monstrosify code
... I think?
-rw-r--r--bot/cogs/moderation/modlog.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/bot/cogs/moderation/modlog.py b/bot/cogs/moderation/modlog.py
index 92b399874..53ea4ebcb 100644
--- a/bot/cogs/moderation/modlog.py
+++ b/bot/cogs/moderation/modlog.py
@@ -641,20 +641,26 @@ class ModLog(Cog, name="ModLog"):
_before = before.clean_content
_after = after.clean_content
- groups = tuple((g[0], tuple(g[1]))
- for g in itertools.groupby(difflib.ndiff(_before.split(), _after.split()), key=lambda s: s[0]))
- for index, (name, values) in enumerate(groups):
- sub = ' '.join(s[2:].strip() for s in values)
- if name == '-':
+ # Getting the difference per words and group them by type - add, remove, same
+ # Note that this is intended grouping without sorting
+ diff = difflib.ndiff(_before.split(), _after.split())
+ diff_groups = tuple(
+ (diff_type, tuple(s[2:] for s in diff_words))
+ for diff_type, diff_words in itertools.groupby(diff, key=lambda s: s[0])
+ )
+
+ for index, (diff_type, words) in enumerate(diff_groups):
+ sub = ' '.join(words)
+ if diff_type == '-':
_before = _before.replace(sub, f"[{sub}](http://.z)")
- elif name == '+':
+ elif diff_type == '+':
_after = _after.replace(sub, f"[{sub}](http://.z)")
else:
- if len(values) > 2:
- new = (f"{values[0].strip() if index > 0 else ''}"
+ if len(words) > 2:
+ new = (f"{words[0] if index > 0 else ''}"
" ... "
- f"{values[-1].strip() if index < len(groups) - 1 else ''}")
+ f"{words[-1] if index < len(diff_groups) - 1 else ''}")
else:
new = sub
_before = _before.replace(sub, new)