1- # gendiff/formaters/stylish.py
21def format_value (value , depth ):
32 indent = " " * (depth * 4 )
43
54 if isinstance (value , dict ):
65 formatted = ["{" ]
76 for k , v in value .items ():
8- formatted .append (f"{ indent } { k } : { format_value (v , depth + 1 )} " )
7+ formatted .append (
8+ f"{ indent } { k } : { format_value (v , depth + 1 )} "
9+ )
910 formatted .append (f"{ indent } }}" )
1011 return "\n " .join (formatted )
1112
@@ -16,40 +17,31 @@ def format_value(value, depth):
1617 if value is None :
1718 return "null"
1819
19- return str ( value )
20+ return f" { value } "
2021
2122
2223def format_stylish (diff , depth = 0 ):
2324 indent = " " * (depth * 4 )
2425 lines = ["{" ]
2526
26- for key , info in diff .items ():
27+ for key , info in sorted ( diff .items () ):
2728 status = info ["status" ]
29+ value = format_value (info .get ("value" ), depth + 1 )
30+ old_value = format_value (info .get ("old_value" ), depth + 1 )
31+ new_value = format_value (info .get ("new_value" ), depth + 1 )
2832
2933 if status == "nested" :
3034 children = format_stylish (info ["children" ], depth + 1 )
3135 lines .append (f"{ indent } { key } : { children } " )
3236 elif status == "added" :
33- lines .append (
34- f"{ indent } + { key } : { format_value (info ['value' ], depth + 1 )} "
35- )
37+ lines .append (f"{ indent } + { key } : { value } " )
3638 elif status == "removed" :
37- lines .append (
38- f"{ indent } - { key } : { format_value (info ['value' ], depth + 1 )} "
39- )
39+ lines .append (f"{ indent } - { key } : { value } " )
4040 elif status == "modified" :
41- lines .append (
42- f"{ indent } - { key } :"
43- f"{ format_value (info ['old_value' ], depth + 1 )} "
44- )
45- lines .append (
46- f"{ indent } + { key } :"
47- f"{ format_value (info ['new_value' ], depth + 1 )} "
48- )
41+ lines .append (f"{ indent } - { key } : { old_value } " )
42+ lines .append (f"{ indent } + { key } : { new_value } " )
4943 else : # unchanged
50- lines .append (
51- f"{ indent } { key } : { format_value (info ['value' ], depth + 1 )} "
52- )
44+ lines .append (f"{ indent } { key } : { value } " )
5345
54- lines .append (indent + " }" )
46+ lines .append (f" { indent } } }" )
5547 return "\n " .join (lines )
0 commit comments