Skip to content

Commit 55f8c8e

Browse files
committed
stylish format _name
1 parent 31c99c4 commit 55f8c8e

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

gendiff/gen_diff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def difference(file1, file2):
1818
for i in keys12:
1919
if i in keys1 and i in keys2:
2020
if isinstance(file1[i], dict) and isinstance(file2[i], dict):
21-
result[i] = ('nested', generate_diff(file1[i], file2[i]))
21+
result[i] = ('nested', difference(file1[i], file2[i]))
2222
else:
2323
if file1[i] == file2[i]:
2424
result[i] = ('unchanged', file1[i])
@@ -42,4 +42,4 @@ def generate_diff(file1, file2, format_name='stylish'):
4242
diff = difference(file1, file2)
4343
match format_name:
4444
case 'stylish':
45-
return gen_diff_stylish(diff)
45+
return gen_diff_stylish(diff)

gendiff/gen_diff_stylish.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,48 @@ def stringify(value, lvl=1):
1818

1919

2020
def gen_diff_stylish(value, lvl=1):
21-
res = ''
22-
if isinstance(value, dict):
23-
res += '{\n'
24-
for el, val in value.items():
25-
match val[0]:
26-
case 'nested':
27-
res += f'{' ' * lvl}{el}: '
28-
res += gen_diff_stylish(val[1], lvl + 1) + '\n'
29-
case 'unchanged':
21+
res = '{\n'
22+
for el, val in value.items():
23+
match val[0]:
24+
case 'nested':
25+
res += f'{' ' * lvl}{el}: '
26+
res += gen_diff_stylish(val[1], lvl + 1) + '\n'
27+
case 'unchanged':
28+
if val[1] == '':
29+
res += (f'{' ' * lvl}{el}:'
30+
f'{stringify(val[1], lvl=lvl + 1)}') + '\n'
31+
else:
3032
res += (f'{' ' * lvl}{el}:'
3133
f' {stringify(val[1], lvl=lvl + 1)}') + '\n'
32-
case 'added':
34+
case 'added':
35+
if val[1] == '':
36+
res += (f'{' ' * (lvl - 1) + ' + '}{el}:'
37+
f'{stringify(val[1], lvl=lvl + 1)}') + '\n'
38+
else:
3339
res += (f'{' ' * (lvl - 1) + ' + '}{el}:'
3440
f' {stringify(val[1], lvl=lvl + 1)}') + '\n'
35-
case 'removed':
41+
case 'removed':
42+
if val[1] == '':
43+
res += (f'{' ' * (lvl - 1) + ' - '}{el}:'
44+
f'{stringify(val[1], lvl=lvl + 1)}') + '\n'
45+
else:
3646
res += (f'{' ' * (lvl - 1) + ' - '}{el}:'
3747
f' {stringify(val[1], lvl=lvl + 1)}') + '\n'
38-
case 'changed':
48+
case 'changed':
49+
if val[1] == '':
50+
res += (f'{' ' * (lvl - 1) + ' - '}{el}:'
51+
f'{stringify(val[1], lvl=lvl + 1)}') + '\n'
52+
res += (f'{' ' * (lvl - 1) + ' + '}{el}:'
53+
f' {stringify(val[2], lvl=lvl + 1)}') + '\n'
54+
elif val[2] == '':
55+
res += (f'{' ' * (lvl - 1) + ' - '}{el}:'
56+
f' {stringify(val[1], lvl=lvl + 1)}') + '\n'
57+
res += (f'{' ' * (lvl - 1) + ' + '}{el}:'
58+
f'{stringify(val[2], lvl=lvl + 1)}') + '\n'
59+
else:
3960
res += (f'{' ' * (lvl - 1) + ' - '}{el}:'
4061
f' {stringify(val[1], lvl=lvl + 1)}') + '\n'
4162
res += (f'{' ' * (lvl - 1) + ' + '}{el}:'
4263
f' {stringify(val[2], lvl=lvl + 1)}') + '\n'
43-
res += ' ' * (lvl - 1) + '}'
64+
res += ' ' * (lvl - 1) + '}'
4465
return res

ruff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ line-length = 80
66

77
[lint]
88
preview = true
9-
select = ["E", "F", "I", "C90"]
9+
select = ["E", "F", "I"]

0 commit comments

Comments
 (0)