Replace delta if/elif/elif... with a dictionary lookup, and use "equal" to avoid awkward delta=None case

This commit is contained in:
Tom Rathborne 2022-02-11 11:13:11 +01:00
parent 6c527a8a7a
commit e75e2cd3e5

View file

@ -139,6 +139,7 @@ def cmp() -> None:
differences.append(diff)
elif args.all:
diff['delta'] = 'equal'
differences.append(diff)
if args.json:
@ -154,23 +155,30 @@ def cmp() -> None:
from .lib.nicelogger import Colors, support_color
c = Colors(support_color(sys.stdout))
diffstyles = {
'new': {
'symbol': '->',
'oldc': c.red
},
'old': {
'symbol': f'{c.red}<-{c.normal}',
'oldc': c.red
},
'added': {
'symbol': '++',
'oldc': c.red
},
'gone': {
'symbol': f'{c.red}--{c.normal}',
'oldc': c.green
},
'equal': {
'symbol': '==',
'oldc': c.green
}
}
for diff in differences:
delta = diff.get('delta', None)
style = diffstyles[diff['delta']]
if delta == 'new':
arrow = '->'
oldc = c.red
elif delta == 'old':
arrow = f'{c.red}<-{c.normal}'
oldc = c.red
if delta == 'added':
arrow = '++'
oldc = c.red
elif delta == 'gone':
arrow = f'{c.red}--{c.normal}'
oldc = c.green
else:
arrow = '=='
oldc = c.green
print(f'{diff["name"]} {oldc}{diff["oldver"]}{c.normal} {arrow} {c.green}{diff["newver"]}{c.normal}')
print(f'{diff["name"]} {style["oldc"]}{diff["oldver"]}{c.normal} {style["symbol"]} {c.green}{diff["newver"]}{c.normal}')