55import json
66import sys
77from collections import OrderedDict
8+ from difflib import unified_diff
89from typing import List
910from typing import Mapping
1011from typing import Optional
@@ -55,6 +56,13 @@ def parse_topkeys(s): # type: (str) -> List[str]
5556 return s .split (',' )
5657
5758
59+ def get_diff (source , target , file ): # type: (str, str, str) -> str
60+ source_lines = source .splitlines (True )
61+ target_lines = target .splitlines (True )
62+ diff = unified_diff (source_lines , target_lines , fromfile = file , tofile = file )
63+ return '' .join (diff )
64+
65+
5866def main (argv = None ): # type: (Optional[Sequence[str]]) -> int
5967 parser = argparse .ArgumentParser ()
6068 parser .add_argument (
@@ -96,7 +104,6 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
96104 default = [],
97105 help = 'Ordered list of keys to keep at the top of JSON hashes' ,
98106 )
99-
100107 parser .add_argument ('filenames' , nargs = '*' , help = 'Filenames to fix' )
101108 args = parser .parse_args (argv )
102109
@@ -113,10 +120,19 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
113120 )
114121
115122 if contents != pretty_contents :
116- print ('File {} is not pretty-formatted' .format (json_file ))
123+ print (
124+ 'File {} is not pretty-formatted' .format (json_file ),
125+ file = sys .stderr ,
126+ )
127+ sys .stderr .flush ()
117128
118129 if args .autofix :
119130 _autofix (json_file , pretty_contents )
131+ else :
132+ print (
133+ get_diff (contents , pretty_contents , json_file ),
134+ end = '' ,
135+ )
120136
121137 status = 1
122138 except ValueError :
0 commit comments