1
1
#!/usr/bin/env python3
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one or more
4
+ # contributor license agreements. See the NOTICE file distributed with
5
+ # this work for additional information regarding copyright ownership.
6
+ # The ASF licenses this file to You under the Apache License, Version 2.0
7
+ # (the "License"); you may not use this file except in compliance with
8
+ # the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
2
18
3
19
import os
4
20
import sys
@@ -86,6 +102,7 @@ def format_table_diff(
86
102
expected_rows : List [List [str ]],
87
103
actual_rows : List [List [str ]],
88
104
use_colors : bool = True ,
105
+ cell_width : int = CELL_WIDTH ,
89
106
) -> str :
90
107
"""Format a table diff with cell-level highlighting."""
91
108
output_lines = []
@@ -96,7 +113,7 @@ def format_table_diff(
96
113
)
97
114
output_lines .append ("=" * len (title ))
98
115
99
- col_widths = [CELL_WIDTH ] * len (header )
116
+ col_widths = [cell_width ] * len (header )
100
117
101
118
def format_row (cells : List [str ], prefix : str = "" , color : str = "" ) -> str :
102
119
"""Format a single row with proper alignment."""
@@ -171,7 +188,7 @@ def create_border(char: str = "-") -> str:
171
188
if expected_cell != actual_cell :
172
189
row_has_changes = True
173
190
diff_cell = highlight_cell_diff (
174
- expected_cell , actual_cell , use_colors , CELL_WIDTH
191
+ expected_cell , actual_cell , use_colors , cell_width
175
192
)
176
193
diff_row .append (diff_cell )
177
194
else :
@@ -205,15 +222,16 @@ def create_border(char: str = "-") -> str:
205
222
return "\n " .join (output_lines )
206
223
207
224
208
- def generate_word_diff (actual , expected ):
225
+ def generate_table_diff (actual , expected , cell_width = CELL_WIDTH ):
209
226
"""Generate a table-aware diff between actual and expected output."""
210
227
try :
211
228
expected_header , expected_rows = parse_table_content (expected )
212
229
actual_header , actual_rows = parse_table_content (actual )
213
230
214
231
if expected_header and actual_header :
215
- use_colors = hasattr (sys .stdout , "isatty" ) and sys .stdout .isatty ()
216
- return format_table_diff (expected_header , expected_rows , actual_rows , use_colors )
232
+ return format_table_diff (
233
+ expected_header , expected_rows , actual_rows , True , cell_width
234
+ )
217
235
except Exception :
218
236
pass
219
237
@@ -257,7 +275,7 @@ def format_type_table(results, header, column_width=30):
257
275
return "\n " .join (output_lines )
258
276
259
277
260
- def compare_files (file1_path , file2_path ):
278
+ def compare_files (file1_path , file2_path , cell_width = CELL_WIDTH ):
261
279
"""Compare two files and show the differences."""
262
280
if not os .path .exists (file1_path ):
263
281
print (f"Error: File '{ file1_path } ' does not exist" )
@@ -287,7 +305,7 @@ def compare_files(file1_path, file2_path):
287
305
print ("Files differ. Generating word-wise diff..." )
288
306
print ()
289
307
290
- diff_output = generate_word_diff (content2 , content1 )
308
+ diff_output = generate_table_diff (content2 , content1 , cell_width )
291
309
print (diff_output )
292
310
return False
293
311
@@ -296,10 +314,16 @@ def main():
296
314
parser = argparse .ArgumentParser (description = "Compare two table files using word-wise diff" )
297
315
parser .add_argument ("file1" , help = "First file (expected)" )
298
316
parser .add_argument ("file2" , help = "Second file (actual)" )
317
+ parser .add_argument (
318
+ "--cell-width" ,
319
+ type = int ,
320
+ default = CELL_WIDTH ,
321
+ help = f"Width of each table cell (default: { CELL_WIDTH } )" ,
322
+ )
299
323
300
324
args = parser .parse_args ()
301
325
302
- success = compare_files (args .file1 , args .file2 )
326
+ success = compare_files (args .file1 , args .file2 , args . cell_width )
303
327
sys .exit (0 if success else 1 )
304
328
305
329
0 commit comments