Skip to content

Commit

Permalink
add deletion background color
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Nov 8, 2023
1 parent 5057c18 commit c47d2fa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
7 changes: 6 additions & 1 deletion metamorph/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def __main__():

parser.add_argument("--colour",action='store_true' ,help="show coloured differences",default=True)
parser.add_argument("-nc","--no-colour",dest='colour',action='store_false' ,help="don't show coloured differences")

parser.add_argument("--background-colour",action='store_true' ,help="show coloured deletions",default=True)
parser.add_argument("-nbc","--no-background-colour",dest='background_colour',action='store_false' ,help="don't show coloured deletions")


parser.add_argument("-sd", "--show-diagrams", action='store_true', help="print all diagrams",default=False )
parser.add_argument("-sdi", "--show-diagram-init", action='store_false', help="print the diagram translation flow",default=True)
parser.add_argument("-hdi", "--hide-diagram-init", dest='show_diagram_init',action='store_false', help="don't print the diagram translation flow")
Expand Down Expand Up @@ -74,7 +79,7 @@ def __main__():
s = generate_alternatives(to_translate, conf)
for tmp_text in s:
if args.colour:
print(get_edits_string(to_translate,tmp_text))
print(get_edits_string(to_translate,tmp_text,args.background_colour))
else:
print(tmp_text)
print()
Expand Down
30 changes: 26 additions & 4 deletions metamorph/util.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import difflib
from termcolor import colored

def get_edits_string(old, new):
def get_edits_string(old:str, new:str, on_color :bool= True):
"""
Colorize the differences between two strings.
"""
result = ""
codes = difflib.SequenceMatcher(a=old, b=new).get_opcodes()
reds = []
if on_color:
for code in codes:
if code[0] == "replace":
reds += list(range(code[1],code[2]))
elif code[0] == "delete":
reds += list(range(code[1],code[2]))

li = 0

for code in codes:
if code[0] == "equal":
result += old[code[1]:code[2]]
for i in range(code[3],code[4]):
result += colored(new[i],color=None,on_color='on_red' if i in reds else None)
li=code[4]
elif code[0] == "insert":
result += colored(new[code[3]:code[4]],'green')
for i in range(code[3],code[4]):
result += colored(new[i],color='green',on_color='on_red' if i in reds else None)
li=code[4]
elif code[0] == "replace":
result += colored(new[code[3]:code[4]],'green')
for i in range(code[3],code[4]):
result += colored(new[i],color='green',on_color='on_red' if i in reds else None)
li=code[4]

mr = max(reds) if len(reds) > 0 else 0
#print(mr, li, reds)
if li < mr:
result += colored(" "*(mr-li),color=None,on_color='on_red')

return result

0 comments on commit c47d2fa

Please sign in to comment.