Sfoglia il codice sorgente

added an argument to control tab expansion

Leeor Aharon 7 anni fa
parent
commit
11d2e1556a
1 ha cambiato i file con 8 aggiunte e 5 eliminazioni
  1. 8
    5
      cdiff.py

+ 8
- 5
cdiff.py Vedi File

@@ -398,11 +398,11 @@ class DiffParser(object):
398 398
 
399 399
 class DiffMarker(object):
400 400
 
401
-    def markup(self, diffs, side_by_side=False, width=0):
401
+    def markup(self, diffs, side_by_side=False, width=0, tab_width=8):
402 402
         """Returns a generator"""
403 403
         if side_by_side:
404 404
             for diff in diffs:
405
-                for line in self._markup_side_by_side(diff, width):
405
+                for line in self._markup_side_by_side(diff, width, tab_width):
406 406
                     yield line
407 407
         else:
408 408
             for diff in diffs:
@@ -442,13 +442,13 @@ class DiffMarker(object):
442 442
                 else:
443 443
                     yield self._markup_common(' ' + old[1])
444 444
 
445
-    def _markup_side_by_side(self, diff, width):
445
+    def _markup_side_by_side(self, diff, width, tab_width):
446 446
         """Returns a generator"""
447 447
         wrap_char = colorize('>', 'lightmagenta')
448 448
 
449 449
         def _normalize(line):
450 450
             return line.replace(
451
-                '\t', ' ' * 8).replace('\n', '').replace('\r', '')
451
+                '\t', ' ' * tab_width).replace('\n', '').replace('\r', '')
452 452
 
453 453
         def _fit_with_marker(text, markup_fn, width, pad=False):
454 454
             """Wrap or pad input pure text, then markup"""
@@ -642,7 +642,7 @@ def markup_to_pager(stream, opts):
642 642
     diffs = DiffParser(stream).get_diff_generator()
643 643
     marker = DiffMarker()
644 644
     color_diff = marker.markup(diffs, side_by_side=opts.side_by_side,
645
-                               width=opts.width)
645
+                               width=opts.width, tab_width=opts.tab_width)
646 646
 
647 647
     for line in color_diff:
648 648
         pager.stdin.write(line.encode('utf-8'))
@@ -751,6 +751,9 @@ def main():
751 751
     parser.add_option(
752 752
         '-c', '--color', default='auto', metavar='M',
753 753
         help="""colorize mode 'auto' (default), 'always', or 'never'""")
754
+    parser.add_option(
755
+        '-t', '--tab-width', type='int', default=8, metavar='N',
756
+        help="""convert tab characters to this many spcaes (default: 8)""")
754 757
 
755 758
     # Hack: use OptionGroup text for extra help message after option list
756 759
     option_group = OptionGroup(