Kaynağa Gözat

Merge branch 'auto-width'

Matthew Wang 10 yıl önce
ebeveyn
işleme
d7ee435203
3 değiştirilmiş dosya ile 34 ekleme ve 7 silme
  1. 2
    1
      README.rst
  2. 30
    5
      cdiff.py
  3. 2
    1
      tests/test_cdiff.py

+ 2
- 1
README.rst Dosyayı Görüntüle

@@ -69,7 +69,8 @@ Type ``cdiff -h`` to show usage::
69 69
       --version           show program's version number and exit
70 70
       -h, --help          show this help message and exit
71 71
       -s, --side-by-side  enable side-by-side mode
72
-      -w N, --width=N     set text width for side-by-side mode, default is 80
72
+      -w N, --width=N     set text width for side-by-side mode, 0 for auto
73
+                          detection, default is 80
73 74
       -l, --log           show log with changes from revision control
74 75
       -c M, --color=M     colorize mode 'auto' (default), 'always', or 'never'
75 76
 

+ 30
- 5
cdiff.py Dosyayı Görüntüle

@@ -490,10 +490,6 @@ class DiffMarker(object):
490 490
 
491 491
             return ''.join(out)
492 492
 
493
-        # Set up line width
494
-        if width <= 0:
495
-            width = 80
496
-
497 493
         # Set up number width, note last hunk might be empty
498 494
         try:
499 495
             (start, offset) = diff._hunks[-1]._old_addr
@@ -504,6 +500,19 @@ class DiffMarker(object):
504 500
             max1 = max2 = 0
505 501
         num_width = max(len(str(max1)), len(str(max2)))
506 502
 
503
+        # Set up line width
504
+        if width <= 0:
505
+            # Autodetection of text width according to terminal size
506
+            try:
507
+                # Each line is like "nnn TEXT nnn TEXT\n", so width is half of
508
+                # [terminal size minus the line number columns and 3 separating
509
+                # spaces
510
+                #
511
+                width = (terminal_size()[0] - num_width * 2 - 3) / 2
512
+            except Exception:
513
+                # If terminal detection failed, set back to default
514
+                width = 80
515
+
507 516
         # Setup lineno and line format
508 517
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
509 518
         right_num_fmt = colorize('%%(right_num)%ds' % num_width, 'yellow')
@@ -665,6 +674,21 @@ def decode(line):
665 674
     return '*** cdiff: undecodable bytes ***\n'
666 675
 
667 676
 
677
+def terminal_size():
678
+    """Returns terminal size. Taken from https://gist.github.com/marsam/7268750
679
+    but removed win32 support which depends on 3rd party extension.
680
+    """
681
+    width, height = None, None
682
+    try:
683
+        import struct, fcntl, termios
684
+        s = struct.pack('HHHH', 0, 0, 0, 0)
685
+        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
686
+        height, width = struct.unpack('HHHH', x)[0:2]
687
+    except (IOError, AttributeError):
688
+        pass
689
+    return width, height
690
+
691
+
668 692
 def main():
669 693
     signal.signal(signal.SIGPIPE, signal.SIG_DFL)
670 694
     signal.signal(signal.SIGINT, signal.SIG_DFL)
@@ -701,7 +725,8 @@ def main():
701 725
         help='enable side-by-side mode')
702 726
     parser.add_option(
703 727
         '-w', '--width', type='int', default=80, metavar='N',
704
-        help='set text width for side-by-side mode, default is 80')
728
+        help='set text width for side-by-side mode, 0 for auto detection, '
729
+             'default is 80')
705 730
     parser.add_option(
706 731
         '-l', '--log', action='store_true',
707 732
         help='show log with changes from revision control')

+ 2
- 1
tests/test_cdiff.py Dosyayı Görüntüle

@@ -255,7 +255,8 @@ class DiffMarkupTest(unittest.TestCase):
255 255
             '\x1b[0m\x1b[33m4\x1b[0m '
256 256
             '\x1b[32m\x1b[4m\x1b[32ma\x1b[0m\x1b[32mgain\x1b[0m\n')
257 257
 
258
-    def test_markup_side_by_side_neg_width(self):
258
+    # This test is not valid anymore
259
+    def __test_markup_side_by_side_neg_width(self):
259 260
         diff = self._init_diff()
260 261
         marker = cdiff.DiffMarker()
261 262
         out = list(marker._markup_side_by_side(diff, -1))